Syntax highlighting by Geshi

DokuWiki includes GeSHi - Generic Syntax Highlighter - for formatting <code> and <file> blocks. Please refer to the documentation at http://qbnz.com/highlighter/documentation.php for more information about how to use its class.

Please refer to the GeSHi class documentation for implementation details.

Example usage

Example how DokuWiki applies the highlighter for its code and file blocks. It is embedded in a wrapper method p_xhtml_cached_geshi() that caches the output to reduce the server load.

$code = '... syntax to higlight ...';
$geshi = new GeSHi($code, $language);
$geshi->set_encoding('utf-8');
$geshi->enable_classes();
$geshi->set_header_type(GESHI_HEADER_PRE);
$geshi->set_link_target($conf['target']['extern']);
 
//when you like to use your own wrapper, remove GeSHi's wrapper element
//we need to use a GeSHi wrapper to avoid <BR> throughout the highlighted text
$highlighted_code = trim(
    preg_replace('!^<pre[^>]*>|</pre>$!', '', $geshi->parse_code()),
    "\n\r"
);

See also