You are using a browser which doesn't fully support Cascading Style Sheets. This site will look much better in a browser that supports web standards, but its content is accessible to any browser or Internet device.
An improved filter for code highlightning
The highlight filter in the chapter above was pretty simple: it just colorizes Perl style comments. This significantly helps to make a code snippet more readable, and has the added advantage of manual highlightning, which means one can use additional PerlPoint tags or macros to emphasize parts of the code. In the following example, the filter is used and a function call is marked red:
// see this code: ||co|| # call a function \RED<function(>@parameters\RED<)>
Nevertheless, one might want to see a highlightning not only of comments, but of the entire syntax - similar to the highlightning in code editors.
This can be achieved as well. The following example uses
Syntax::Highlight::Engine::Kate
for the highlightning part:
\EMBED{lang=perl} # load modules use Syntax::Highlight::Engine::Kate; # a generic function to build a highlighter object for a certain language sub buildHighlighter { # supply a highlighter object return new Syntax::Highlight::Engine::Kate( language => $_[0], format_table => { Alert => ['\\BLUE<', '>'], BaseN => ['\F{color="#007f00"}<', '>'], BString => ['\\F{color="#c9a7ff"}<', '>'], Char => ['\\F{color="#ff00ff"}<', '>'], Comment => ['\\F{color="green"}<', '>'], DataType => ['\\F{color="#0000ff"}<', '>'], DecVal => ['\\F{color="#00007f"}<', '>'], Error => ['\\F{color="#ff0000"}<\\B<\\I<', '>>>'], Float => ['\\F{color="#00007f"}<', '>'], Function => ['\\F{color="#007f00"}<', '>'], IString => ['\\F{color="#ff0000"}<', '>'], Keyword => ["\\B<", '>'], Normal => ['', ''], Operator => ['\\F{color="#ffa500"}<', '>'], Others => ['\\F{color="#b03060"}<', '>'], RegionMarker => ['\\F{color="#96b9ff"}\\I<', '>>'], Reserved => ['\\F{color="#9b30ff"}<\\B<', '>>'], String => ['\\F{color="#ff0000"}<', '>'], Variable => ['\\F{color="#0000ff"}<\\B<', '>>'], Warning => ['\\F{color="#0000ff"}<\\B<\\I<', '>>>'], }, ); } # define a Perl highlighter sub formatPerl { # build a Perl highlighter - it is important to have # a new object as objects used before would remember # the code they processed my $hl=buildHighlighter('Perl'); # highlight $hl->highlightText($main::_pfilterText); } # declare shortcut *cp=\&formatPerl; # supply "PerlPoint" ''; \END_EMBED
Note that we build the highlighter object each time we produce a paragraph. This is necessary as the highlighter objects assume code is continued when called multiple times, which can lead to unexpected results. (On another note, the highligther is rather picky about the syntax - make sure the examples processed are syntactically correct. But this is exactly what an editor would do, wouldn't it?)
The second trick here is that we defined an "object builder" for the highlightning object. It defined the colors and formattings one prefers and applies them to the constructor of a formatter for a language specified by parameter. So, if you mix several languages in your document, defining a filter for another language (that is supported by
Syntax::Highlight::Engine::Kate
is very simple:
\EMBED{lang=perl} # declare a makefile highlighter sub formatMakefile { # build a Makefile highlighter - it is important to have # a new object as objects used before would remember # the code they processed my $hl=buildHighlighter('Makefile'); # highlight $hl->highlightText($main::_pfilterText); } # declare shortcut *cm=\&formatMakefile; # supply "PerlPoint" ''; \END_EMBED
For whatever language, the new filters can be used as usual:
Look at this example:
||cp|c|| # do important things
doImportantThings();
# function definition
sub doImportantThings
{
# just do it
do {$things->important};
}
And this is the result:
# do important things doImportantThings(); # function definition sub doImportantThings { # just do it do {$things->important}; }
The only downcase with this approach is that one cannot add own PerlPoint tags or macros to the example, as the highlighter could not handle them.