Friday, July 18, 2008

Display source code in MediaWiki

You have three options by default:
  1. Display code inline like script.sh, by using <code>script.sh</code>.
  2. Blocks of code can be wrapped with <pre>insert your code here</pre>. This works multi-line but doesn't allow formatting.
  3. Indent your text to enable a <pre> like block, then apply standard '''bold''' and ''italic'' formatting.
The above options generally work quite well, but if you end up with lots of code from different languages and more than a few lines it would be handy to have syntax highlighting. Thankfully an extension to MediaWiki can do this. Use SyntaxHighlight_GeSHi to colour away; it is also used on Wikipedia.

You will need root access to your server and subversion installed, then follow the simple instructions on the extension website. Download the extension and GeSHi then add it to your LocalSettings.php.

Now you have a fourth option, wrap your code with <source lang="X">code here</source> where X is php, java, bash, ruby or one of the other nearly 50 supported langauges!