[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kwrite-devel
Subject:    New Syntax Highlighting For Template-Toolkit Files
From:       Sean Kellogg <skellogg () u ! washington ! edu>
Date:       2006-10-02 2:53:04
Message-ID: 200610011953.04738.skellogg () u ! washington ! edu
[Download RAW message or body]

Having used Kate to edit TT files (http://www.template-toolkit.org/) for 
almost four years now, I've just sort of quitely made the best of the HTML 
syntax highlighter.  A few weeks ago I decided to bit the bullet and piece 
together my own highlighter to make work a bit easier.

I'm now satasfied that it works in most situations, although I have a list of 
things I'd like to figure out how to do.  I'm attaching it here (as well as a 
sample template file for testing).  If it is up to code and there is 
interest, please feel free to include it with the other highlighters.

I don't forsee abandoning Kate or TT anytime soon, so I'm happy to continue 
maintaining the file for the forseeable future.

All that being said, I do have a question that a Kate expert might be able to 
answer.  TT is used in conjunction with HTML, so I've included the HTML rules 
via an IncludeRules directive.  Works great, whenever the user opens a HTML 
tag the system switches into the HTML syntax without fail.  Trick is, you can 
use TT directives inside of an HTML tag.

Example:
<td [% IF true %]class='foo'[% END %]> stuff </td>

Trouble is, the HTML parser gets to the TT directives and highlights the whole 
thing as if it where HTML.  Now, I realize I could go into the HTML file and 
put in a TT detection line, but I'd like to solve the problem in the TT file.  
Thoughts?

-Sean

-- 
Sean Kellogg
c: 831.818.6940    e: skellogg@u.washington.edu
w: http://blog.probonogeek.org/

So, let go
 ...Jump in
  ...Oh well, what you waiting for?
   ...it's all right
    ...'Cause there's beauty in the breakdown

["tmpl.xml" (text/xml)]

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE language SYSTEM "language.dtd">
<!--
	Copyright (C) 2006 Sean Kellogg <skellogg@u.washington.edu>
		
	This library is free software; you can redistribute it and/or
	modify it under the terms of the GNU Library General Public
	License version 2 as published by the Free Software Foundation.
	
	This library is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
	Library General Public License for more details.
	
	You should have received a copy of the GNU Library General Public License
	along with this library; see the file COPYING.LIB.  If not, write to
	the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
	Boston, MA 02110-1301, USA.
	
	*** NOTICE ***
	This file is based on the perl template file maintained by Anders Lund \
<anders@alweb.dk>

	*** TODO ***
	
	* Introduce variable handling into template directives
	* Include directives and filters which are included with the default TT software
	* Figure out how to break out of an HTML context when entering into a TT context
	* Implement proper region marking for [% FOO %] ... [% END %] style directives
	* Implement proper vmethod syntax while inside interpolated strings
	* Recognize difference between functions and data
	
-->

<language name="Template-Toolkit" version="1.0" kateversion="2.4" section="Markup" \
extensions="*.tmpl" mimetype="text/html"  author="Sean Kellogg \
(skellogg@u.washington.edu)" license="LGPL" priority="10">  
	<highlighting>
		<list name="TTdirectives">
			<item> BLOCK </item>
			<item> CALL </item>
			<item> CASE </item>
			<item> CATCH </item>
			<item> CLEAR </item>
			<item> DEBUG </item>
			<item> DEFAULT </item>
			<item> ELSE </item>
			<item> ELSIF </item>
			<item> END </item>
			<item> FILTER </item>
			<item> FINAL </item>
			<item> FOREACH </item>
			<item> GET </item>
			<item> IF </item>
			<item> IN </item>
			<item> INCLUDE </item>
			<item> INSERT </item>
			<item> LAST </item>
			<item> MACRO </item>
			<item> META </item>
			<item> NEXT </item>
			<item> PERL </item>
			<item> PROCESS </item>
			<item> RAWPERL </item>
			<item> RETURN </item>
			<item> SET </item>
			<item> STOP </item>
			<item> SWITCH </item>
			<item> TAGS </item>
			<item> THROW </item>
			<item> TRY </item>
			<item> UNLESS </item>
			<item> USE </item>
			<item> WHILE </item>
			<item> WRAPPER </item>
		</list>
		<list name="TTvmethods">
			<!-- scalar virtual functions -->
			<item>chunk</item>
			<item>defined</item>
			<item>hash</item>
			<item>length</item>
			<item>list</item>
			<item>match</item>
			<item>repeat</item>
			<item>replace</item>
			<item>search</item>
			<item>size</item>
			<item>split</item>
			<!-- hash virtual functions -->
			<item>each</item>
			<item>exists</item>
			<item>import</item>
			<item>item</item>
			<item>keys</item>
			<item>list</item>
			<item>nsort</item>
			<item>sort</item>
			<item>value</item>
			<!-- list virtual functions -->
			<item>first</item>
			<item>grep</item>
			<item>grep</item>
			<item>join</item>
			<item>last</item>
			<item>max</item>
			<item>merge</item>
			<item>pop</item>
			<item>push</item>
			<item>reverse</item>
			<item>shift</item>
			<item>size</item>
			<item>slice</item>
			<item>splice</item>
			<item>unique</item>
			<item>unshift</item>
		</list>
		<list name="TToperators">
			<item> = </item>
			<item> == </item>
			<item> != </item>
			<item> &lt; </item>
			<item> &gt; </item>
			<item> =&lt; </item>
			<item> =&gt; </item>
			<item> _ </item>
		</list>
		
		<contexts>
			<context name="Start" attribute="Normal Text" lineEndContext="#stay">
				<StringDetect attribute="TT Markup" context="TTmarkup" String="[%" />
				<IncludeRules context="##HTML" includeAttrib="true"/>
			</context>
			
			<context name="TTmarkup" attribute="Normal Text" lineEndConts="#stay">
				
				<!-- Detect TT specific keywords -->
				<RegExpr attribute="TT Files" context="#stay" String="\b[\w]+\.tmpl" />
				<keyword attribute="TT Directives" context="#stay" String="TTdirectives" />
				<keyword attribute="TT Operators" context="#stay" String="TToperators" />
				<keyword attribute="TT Vmethods" context="#stay" String="TTvmethods" />
				<RegExpr attribute="TT Data Type" context="#stay" String="[a-zA-Z]*" />
				
				<!-- Detect end of TT Directive -->
				<StringDetect attribute="TT Markup" context="#pop" String="%]" />
				
				<!-- Detect strings -->
				<DetectChar attribute="TT Operators" context="ip_string" char="&quot;" \
beginRegion="String" />  <DetectChar attribute="TT Operators" context="string" \
char="'"  beginRegion="String" />  
				<!-- Detect types of numbers -->
				<HlCOct attribute="Octal" context="#stay" />
				<HlCHex attribute="Hex" context="#stay" />
				<Float attribute="Float" context="#stay" />
				<Int attribute="Decimal" context="#stay" />

			</context>
		
			
			<!-- Context for finding variables inside of strings -->
			<context name="find_variable" attribute="String (int)" lineEndContext="#pop" \
fallthrough="true" fallthroughContext="#pop" >  <keyword attribute="TT Vmethods" \
context="#pop" String="TTvmethods" />  <RegExpr attribute="TT Data Type" \
context="#pop" String="\$[\w_\.]+" />  </context>
			
			<!-- Context for strings -->
			<context name="ipstring_internal" attribute="String (int)" lineEndContext="#stay">
				<RegExpr attribute="String (int)" context="find_variable" String="\$\S" \
lookAhead="true" />  </context>
			<context name="ip_string" attribute="String (int)" lineEndContext="#stay">
				<DetectChar attribute="TT Operator" context="#pop" char="&quot;" \
endRegion="String"/>  <IncludeRules context="ipstring_internal" />
  		    </context>
			<context name="string" attribute="String" lineEndContext="#stay">
				<DetectChar attribute="TT Operator" context="#pop" char="'" endRegion="String" />
			</context>
		
		</contexts>
		
		<itemDatas>
			<itemData name="Normal Text"	defStyleNum="dsNormal" />
			
			<!-- TT specific styles -->
			<itemData name="TT Markup"		defStyleNum="dsFloat" />
			<itemData name="TT Data Type"	defStyleNum="dsDataType" />
			<itemData name="TT Vmethods"	defStyleNum="dsFunction" />
			<itemData name="TT Directives"	defStyleNum="dsKeyword" />
			<itemData name="TT Operators"	defStyleNum="dsKeyword" />
			<itemData name="TT Files"		defStyleNum="dsOthers" />

			<!-- Perl style string styles -->
			<itemData name="String"			defStyleNum="dsString" color="#FF6C6C" \
selColor="#FF6C6C" bold="0" italic="0" />  <itemData name="String \
(int)"	defStyleNum="dsString" />  
			<!-- Standard number style defaults -->
			<itemData name="Decimal"		defStyleNum="dsDecVal" />
			<itemData name="Octal"			defStyleNum="dsBaseN" />
			<itemData name="Hex"			defStyleNum="dsBaseN" />
			<itemData name="Float"			defStyleNum="dsFloat" />
  		</itemDatas>
		
	</highlighting>
	<general>
		<keywords casesensitive="1" />
		<comments>
			<comment name="multiLine" start="&glt;!--" end="--&lt;" />
		</comments>
	</general>
</language>


["viewOpinions.tmpl" (text/plain)]

[% INCLUDE header.tmpl title = 'View Opinions' %]

[% WRAPPER box.tmpl title = "View Opinons of $admin.memberTitle $member.lastname" %]

	[% IF member.opinions.defined %]

		<table class='dataTable'>
			<tr>
				<th>Member</th>
				<th>Your Opinion</th>
				<th>Their Reputation</th>
			</tr>

		[% SET switch = 0 %]
		[% FOREACH opinion = member.opinions %]
			<tr>
				<td [% 'class="offRow"' IF switch %]>[% INCLUDE member.tmpl member = \
opinion.member %]</td>  <td [% 'class="offRow"' IF switch %] style='text-align: \
center;'>[% opinion.opinionText %]</td>  <td [% 'class="offRow"' IF switch %] \
style='text-align: center;'>[% opinion.member.reputationText %]</td>  </tr>
			[% IF switch %][% SET switch = 0 %][% ELSE %][% SET switch = 1 %][% END %]
		[% END %]
		</table>

	[% END %]

	<table cellpadding=0 cellspacing=0 align="center">
		<tr>[% INCLUDE button.tmpl text="<a class='headingLink' \
href='viewProfile?userID=$member.userID'>Back to Member Profile</a>" %]</tr>  \
</table>

[% END %]

[% INCLUDE footer.tmpl %]



_______________________________________________
KWrite-Devel mailing list
KWrite-Devel@kde.org
https://mail.kde.org/mailman/listinfo/kwrite-devel


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic