Click here to Skip to main content
15,896,278 members
Articles / Web Development / ASP.NET

General Purpose Colorizer

Rate me:
Please Sign up or sign in to vote.
4.62/5 (8 votes)
7 Oct 2007LGPL34 min read 33.6K   294   30  
A rule driven engine for colorizing HTML, CSS, JavaScript, etc.
<?xml version="1.0" encoding="utf-8" ?>
<rules>
   <rule from="html" pattern="&lt;!--\[hilite1\]--&gt;" add="hilite1" transient="noemit"/>
   <rule from="hilite1" pattern="&lt;!--\[/hilite1\]--&gt;" remove="true" transient="noemit"/>
   <rule from="html" pattern="&lt;!--\[hilite2\]--&gt;" add="hilite2" transient="noemit"/>
   <rule from="hilite2" pattern="&lt;!--\[/hilite2\]--&gt;" remove="true" transient="noemit"/>
   <rule from="html" pattern="&lt;!--\[hilite3\]--&gt;" add="hilite3" transient="noemit"/>
   <rule from="hilite3" pattern="&lt;!--\[/hilite3\]--&gt;" remove="true" transient="noemit"/>
   <rule from="html" pattern="&lt;!--\[hilite4\]--&gt;" add="hilite4" transient="noemit"/>
   <rule from="hilite4" pattern="&lt;!--\[/hilite4\]--&gt;" remove="true" transient="noemit"/>
   <rule from="html" pattern="&lt;!--" transient="htmlComment" to="htmlComment" />
   <rule from="htmlComment" pattern="--&gt;" transient="htmlComment" to="html" />

   <rule from="html,css,js" pattern="&lt;/" transient="htmlStartTag" to="htmlCloseTagName" />
   <rule from="html,css" pattern="&lt;!?" transient="htmlStartTag" to="htmlOpenTagName" />

   <rule from="htmlTag" pattern="/?&gt;" transient="htmlEndTag" pop="true" />

   <rule from="htmlOpenTagName" pattern=" |&gt;" to="htmlTag" push="=style:css,script:js,html"/>
   <rule from="htmlCloseTagName" pattern=" |&gt;" to="htmlTag" push="html"/>
   <rule from="htmlTag" pattern="=" transient="htmlAttribEq" />
   <rule from="htmlTag" pattern="[_A-Za-z0-9\-]+" transient="htmlAttribName" />
   <rule from="htmlTag" pattern="&quot;" transient="htmlValueDelim" to="htmlAttribValue" />
   <rule from="htmlAttribValue" pattern="&quot;" transient="htmlValueDelim" to="htmlTag" />


   <rule from="css" pattern="\{" to="cssAttribList" />
   <rule from="cssAttribList" pattern=":" transient="cssAttribList" to="cssAttribValue" />
   <rule from="cssAttribList" pattern="[_A-Za-z0-9\-]+" transient="cssAttribName" />
   <rule from="cssAttribValue" pattern="[^;}]+" transient="cssAttribValue" to="cssAttribList" />
   <rule from="cssAttribList" pattern="\}" transient="cssAttribList" to="css" />
   <rule from="css,cssAttribList,cssAttribValue" pattern="/\*\[hilite1\]\*/" add="cssHilite1" transient="noemit"/>
   <rule from="cssHilite1" pattern="/\*\[/hilite1\]\*/" transient="noemit" remove="true" />
   <rule from="css,cssAttribList,cssAttribValue" pattern="/\*\[hilite2\]\*/" add="cssHilite2" transient="noemit"/>
   <rule from="cssHilite2" pattern="/\*\[/hilite2\]\*/" transient="noemit" remove="true" />
   <rule from="css,cssAttribList,cssAttribValue" pattern="/\*\[hilite3\]\*/" add="cssHilite3" transient="noemit"/>
   <rule from="cssHilite3" pattern="/\*\[/hilite3\]\*/" transient="noemit" remove="true" />
   <rule from="css,cssAttribList,cssAttribValue" pattern="/\*\[hilite4\]\*/" add="cssHilite4" transient="noemit"/>
   <rule from="cssHilite4" pattern="/\*\[/hilite4\]\*/" transient="noemit" remove="true" />


   <rule from="js" pattern="\bbreak\b|\belse\b|\bnew\b|\bvar\b|\bcase\b|\bfinally\b|\breturn\b|\bvoid\b|\bcatch\b|\bfor\b|\bswitch\b|\bwhile\b|\bcontinue\b|\bfunction\b|\bthis\b|\bwith\b|\bdefault\b|\bif\b|\bthrow\b|\bdelete\b|\bin\b|\btry\b|\bdo\b|\binstanceof\b|\btypeof\b" transient="jsKeyword" />
   <rule from="js" pattern="&quot;[^&quot;]*&quot;" transient="jsLiteral" />
   <rule from="js" pattern="//.*$" transient="jsComment" />
   <rule from="js" pattern="/\*\[hilite1\]\*/" add="jsHilite1" transient="noemit"/>
   <rule from="jsHilite1" pattern="/\*\[/hilite1\]\*/" transient="noemit" remove="true" />
   <rule from="js" pattern="/\*\[hilite2\]\*/" add="jsHilite2" transient="noemit"/>
   <rule from="jsHilite2" pattern="/\*\[/hilite2\]\*/" transient="noemit" remove="true" />
   <rule from="js" pattern="/\*\[hilite3\]\*/" add="jsHilite3" transient="noemit"/>
   <rule from="jsHilite3" pattern="/\*\[/hilite3\]\*/" transient="noemit" remove="true" />
   <rule from="js" pattern="/\*\[hilite4\]\*/" add="jsHilite4" transient="noemit"/>
   <rule from="jsHilite4" pattern="/\*\[/hilite4\]\*/" transient="noemit" remove="true" />
   <rule from="js" pattern="/\*" to="jsComment" />
   <rule from="jsComment" pattern="\*/" transient="jsComment" to="js" />


</rules>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Web Developer
Ireland Ireland
Declan Brennan is Chief Architect for Sekos Technology (www.sekos.com). Over the years he has worked in a huge range of technologies and environments and still gets a great thrill from the magic of computers.

Comments and Discussions