Fork me on GitHub

lzHilight by shawncplus

Terminal/HTML Code Highlighting

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.

Features Out of the Box


In Action


Console Highlighting

Console Highlighting

HTML Highlighting

Live usage via Symfony filter

Getting Started


Requirements

How to get it

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

Installing

  1. chmod a+x install.sh
  2. ./install.sh (sudo if you want to install for all users)

Usage

man lzhighlight

Themes

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

Extending


Syntax Files

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

Handler Methods

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.

Tokenizers

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

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.

Theme Inheritance

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.

Further Info


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'

Back to top