Project original started as a syntax highlighter when I was in a terminal and didn't need/want to pop open vim. Somehow morphed into supporting HTML output into now having HTML output as its main goal.
You can download this project in either zip or tar formats.
You can also clone the project with Git by running:
$ git clone git://github.com/shawncplus/lzHilight
man lzhighlight
Setting the default theme: To set the default theme just symlink your preferred theme to a folder named syntax/ in the base of the repo:
ln -s themes/wombat/ syntax
Override default: You can use the -e option to override the default
highlight -i somefile.php -e skittles_dark
To add a syntax to the highlighter you need to create two files: syntax.syn
and syntax.php. syntax.syn will be a newline separated file containing the
tokens and associated color (see man console_codes) OR HTML colors
Example:
T_STRING 1;32 T_ELSEIF #FF00FF
The tokens in syntax.syn must match the tokens produced by <syntax>.php. The .syn files can link together by using the #LINK directive ie.,
T_STRING 1;32 T_ELSEIF 31 #LINK html.syn
The color can also be another token which links multiple tokens together:
T_STRING bgreen // use human readable color T_ENCAPS_STRING T_STRING // link T_ENCAPS_STRING to T_STRING
You can also apply bold/italics to a token using %:
COMMENT #5D8D8F%i // italic OP #8AC6F2%b // bold
Background colors are supported but only applied to HTML output:
ERROR #960050%bi #1E0010 // bold, italic and custom bgcolor
Inside the tokenizers you can write methods to handle tokens directly if there is extra processing that needs to take place (making URLs links, etc) To do this, simply put the name of the function as the color:
HTML_STRING HtmlLexer::handleString
Also, you can specify whether or not it should happen only on HTML or console output. When you do this you must specify a fallback color to use.
HTML_STRING HtmlLexer::handleString|STRING HTMLONLY
The function must always come first followed by a pipe and a color, which like above, can be another token name.
The <syntax>.php file at minimum must contain the camelized class <syntax>Lexer ie., PhpLexer. This class's tokenize method returns an array of tokenizations of the code passed to the function as a string. The array should follow the format of:
array( 0 => array( 'token' => 'T_STRING', 'string' => "'this is some string in the code'" ), 1 => array( 'token' => 'T_ELSEIF', 'string' => 'elseif' ) )
See the bundled tokenizers/php.php and syntax/php.syn for more examples.
Themes exist in the (duh) themes/ directory. Each theme has a directory under themes/ with the name of the theme, ie., themes/wombat/. Inside this directory is a <lang>.syn file for each syntax the theme supports.
The simplest way to inherit from a theme is to simply create symlinks to another theme and override the default.syn file. However, there may be entries in that theme's <lang>.syn file that does not fallthrough to the default.syn so be careful.
If you can't get it working or you found a bug, etc. then send an email to shawn AT shawnbiddle DOT com with the subject 'Syntax Highlighter'