###Install the client-side highlighter###
Download dp.SyntaxHighlighter.
Uncompress its content under a new `/skins/common/SyntaxHighlighter` folder in your MediaWiki installation (don’t forget to make sure the files can be read by the web server; for instance, on Linux you may use `chown apache.apache -R *`).
In the skin template you are using for your MediaWiki site, insert the necessary code as required. In my example, I use the default `/skins/MonoBook.php` template into which I added the following:
Just before the closing `` tag:
Just before the closing
tag:
>_Note that you must include a reference to each source file corresponding to the type of programming language you want to highlight.
Have a look under the `/skins/common/SyntaxHighlighter/Scripts/` folder to see which languages you can highlight; there are a lot more than the few I use on my site._
###Install the WikiMedia extension###
I’ve created a small extension to WikiMedia to allow us to enclose any source code in a new `` tag.
Click on the _View Plain_ option below and copy-paste the following code into a new file that you will save under `/extensions/syntaxhighlighter.php` (again, make sure this is readable by the webserver).
function wfSyntaxHighlighterExtension() {
global $wgParser;
$wgParser->setHook( “codesyntax”, “renderCodeSyntax” );
}
// The callback function for converting the input text to HTML output
function renderCodeSyntax( $input, $argv, &$parser ) {
$output = “<pre style=’border:0;padding:0;margin:0;’>”
.”<textarea name=’code’ class='”.$argv[“lang”].”‘>”
.Xml::escapeTagsOnly($input)
.”</textarea></pre>” ;
return $output;
}
?>
Add the following line to the end of your `LocalSettings.php` file, right before the closing `?>` tag.
###Usage###
To highlight code in your MediaWiki pages, just enclose your source code with the new `` tag. This tag takes a `lang` attribute to specify the options that normally would be listed in the `class` attribute in the _dp.SyntaxHighlighter_ documentation.
Thanks a lot for putting this up, I’ve added it to my own wiki and it worked brilliantly (just had to change a bit of the code for the new version of dp syntaxhighlighter). It also shows nicely just how simple it is to write extensions for MediaWiki.
Santiago
Hi! I got some problems trying to start this up in my wiki, Probably ’cause of that changes Bryn talked about. I get this error : dp is not defined index.php?title=EDITOR&action=edit()()index.ph…tion=edit (lĂnea 295) [Break on this error] dp.SyntaxHighlighter.HighlightAll(‘code’); Could you help me? thank you very much for your effort!
Comments
Thanks a lot for putting this up, I’ve added it to my own wiki and it worked brilliantly (just had to change a bit of the code for the new version of dp syntaxhighlighter). It also shows nicely just how simple it is to write extensions for MediaWiki.
Hi! I got some problems trying to start this up in my wiki, Probably ’cause of that changes Bryn talked about. I get this error : dp is not defined index.php?title=EDITOR&action=edit()()index.ph…tion=edit (lĂnea 295) [Break on this error] dp.SyntaxHighlighter.HighlightAll(‘code’); Could you help me? thank you very much for your effort!
Comments are closed.