MediaWiki/Syntax highlighting
Writing good code follows good coding standards. On top of coding standards, syntax highlighting reveals the code making it easier to read (and learn).
There is a Generic Syntax Highlighter or GeSHi for short which is available as a Debian package (sudo apt-get install php-geshi). However, if using Brion Vibber's extension (which is better than this alternate GeSHi Code Tag extension because Brion's just registers one new tag: 'source' and uses the extension parameter passing available since MW 1.5) you will need to get the more recent source code download of GeSHi.
Installation
- Download GeSHi source
- Download MW extension source
- Make sure MW extension includes geshi.php
- Add include to LocalSettings
Usage
Just use the <source> tag with the lang parameter to define the programming language. Other optional parameters are avaliable too. See the example below.
Parameters
- lang: Defines the language
- line: Corresponds to enable_line_numbers method on GeSHi
- start: Corresponds to start_line_numbers_at method on GeSHi
- enclose: Corresponds to set_header_type method on GeSHi
- strict: Corresponds to enable_strict_mode method on GeSHi
The effect and usage of these parameters can be consulted in GeSHi's documentation.
Since r22246, you can override the colors using [[MediaWiki:GeSHi.css]].
Example
1 <?php
2 if( !defined( 'MEDIAWIKI' ) )
3 die();
4
5 $wgExtensionFunctions[] = 'syntaxHighlightSetup';
6 $wgExtensionCredits['parserhook']['SyntaxHighlight_GeSHi'] = array(
7 'name' => 'SyntaxHighlight',
8 'author' => 'Brion Vibber',
9 'description' => 'Provides syntax highlighting using [http://qbnz.com/highlighter/ GeSHi Higlighter]',
10 'url' => 'http://www.mediawiki.org/wiki/Extension:SyntaxHighlight_GeSHi',
11 );
12 $wgHooks['LoadAllMessages'][] = 'syntaxHighlightLoadMessages';
13
14 function syntaxHighlightSetup() {
15 global $wgParser;
16 $wgParser->setHook( 'source', 'syntaxHighlightHook' );
17 }
18
19 ?>
Supported Languages
These are the languages known by GeSHi that can be used in the lang parameter:
|
|
|
|
See documentation for the latest list of additions
Documentation
See http://qbnz.com/highlighter/geshi-doc.html for the extensive documentation. GeSHi is used in a number of other software projects like