From MediaWiki.org
Jump to: navigation, search
MediaWiki extensions manual
Crystal Clear action run.png

Release status: unknown

Implementation Tag
Description This extension allows syntax highlighting using GeSHi
Author(s) User:Largos
License No license specified
Download see below
Example Ciscavate:MediaWiki Extensions

Translate the GeSHiHighlight extension if it is available at translatewiki.net

Check usage and version matrix; code metrics

Note: There is a new GeSHi-highlighting extension for MediaWiki that I tried called GeSHiCodeTag and it works pretty well! And the guy also documented it very well.

The advantages of GeSHiHighlight over GeSHiColor are 1) it handles more langugages, 2) is still being maintained, and 3) seems to work better in general, although I can't vouch for that personally.

History (brief)[edit]

The pre tags in MediaWiki are nice, but they don't quite cut it for source code. With that in mind, I went questing for a syntax highlighting extension, and found "SyntaxHighlight" (http://www.wickle.com/wikis/index.php/Syntax_Highlight_extension) by Coffman, but I disagree with the use of two tags--one to specify you are in a code block, and another to specify the language:

 <code><perl/> foreach(@myarr){print $_;}</code>

To get arround that, I hacked Coffman's 1.2 release to accept language tags:

<perl>foreach(@myarr){print $_;}</perl> 
<java>public class foo{ /* .... */ }</java>

That version is available here: ...... but I'm not planning on updating it again.

There is another drawback--SyntaxHighlight uses enscript, which (IMO) produces horrible results---better than just pre when dealing with source code, but I want more than comments, strings and keywords to be fontified.

Enter GeSHi.

MediaWiki and GeSHi[edit]

GeSHi (found here: http://qbnz.com/highlighter/index.php ) is a syntax highlighing library written in php. I'm new to php (and by new, I mean I wrote my first line of php code about 4 hours prior writing this--see above) and I was amazed at how easy it was to use GeSHi (this says nothing about my love/hate for php in general...).

Why GeSHi?

  • Better highlighting. Flat-out kicks enscript's ass. I'm not fond of the default colors, but that can be dealt with later.
  • php. enscript, being an external program was writing files with md5hash names, which caused update issues when I was testing, command line params changed causing issues, not quite as elegant. (Using an external program was also my first inclination when thinking about this problem--I actually almost discarded GeSHi because of this narrow-mindedness.)
  • It seems to be actively developed. The release I'm using now is only about a week old.

So, where's the source? Here:

# GeSHiHighlight.php
# By: E. Rogan Creswick (aka: Largos)
# creswick@gmail.com
# wiki.ciscavate.org
# License: GeSHi Highlight is released under the Gnu Public License (GPL), and comes with no warranties.
# The text of the GPL can be found here: http://www.gnu.org/licenses/gpl.html
# Loosely based on SyntaxHighlight.php by Coffman, (www.wickle.com)

# you want to change the below two lines
require_once("geshi.php"); // i asume geshi.php is in the same directory as GeSHiHighlight.php (that is 'extensions' dir)

define("GESHI_PATH","extensions/geshi");// definition where are stored geshi language parsing files

# ok, end of editing :)

class SyntaxSettings {};
$wgSyntaxSettings = new SyntaxSettings; 
$wgExtensionFunctions[] = "wfSyntaxExtension"; 
function wfSyntaxExtension() { 
	global $wgParser;
	$langArray = geshi_list_languages(GESHI_PATH);
# $langArray = array("actionscript","ada","apache","asm","asp","bash",
# "caddcl","cadlisp","c","cpp","css","delphi",
# "html4strict","java","javascript","lisp", "lua",
# "nsis","oobas","pascal","perl","php-brief","php",
# "python","qbasic","sql","vb","visualfoxpro","xml");
	foreach ( $langArray as $lang ){
		if ($lang == "" || $lang == "div") continue; 
		$wgParser->setHook( $lang, 
		create_function( '$text', '$geshi = new GeSHi(trim($text,"\n\r"), "' ."$lang". '", GESHI_PATH);
		return $geshi->parse_code();')); 

* function: geshi_list_languages
* -------------------------
* List supported languages by reading the files in the geshi/geshi subdirectory
* (added by JeffK -- Jeff, any more contact info?) -- I haven't tested the code is is, will do that shortly. -Rogan
function geshi_list_languages ( $path = 'geshi/' )
	$lang_list = array();
	if ($handle = opendir($path)) {
		while (false !== ($file = readdir($handle))) {	// Loop over the directory. 
			if(is_dir($file)) continue;					// Drop anything that is a directory, cause we want files only
			if( ".php" == substr($file, strrpos($file, "."),4)) // process only .php files
			$lang_list[]= substr($file, 0, strrpos($file, "."));

	sort($lang_list); //sort the output, i like ordered lists in Wiki Version page :)
	return $lang_list;

Yep, that's it. Bang. Pretty syntax highlighting with 28 language tags, for 28 languages (disclaimer: I haven't tested all these, and I have suspicions about html4strict working out of the box, but still.)

WARNING - Geshi not only adds tags like <php>, <java> etc. but also the tags <div> and <css>. If you use divs in the wiki content, Geshi will highlight them and your pages may look incorrect. To fix this, after downloading GeSHi and unpacking it, rename files like geshi/div.php and geshi/css.php to something like geshi/divgeshi.php and geshi/cssgeshi.php

Now everything will look fine again. Naturally, if you do want to use the geshi div/css highlighters, you now have to enclose then in <cssgeshi></cssgeschi> instead of <css></css>

Note: For version, the script above needed to be modified so that the require_once line referenced geshi/geshi.php and and GESHI_PATH variable needs to be set to extensions/geshi/geshi.



2nd November 2007[edit]

By HappyDog

  • Removed 'div' from the list of languages, as this ends up parsing any standard HTML <div> tags as code.
  • Fixed PHP notice-level error caused by missing quote marks around the $lang paramter in runtime-created function.


  • Install GeSHi per the GeSHi install instructions (link above)
  • Cut & paste the above code to a file called GeSHiHighlight.php
  • Put GeSHiHighlight.php in your MediaWiki installations' extensions folder
  • Add:
    to LocalSettings.php
  • Note: I had trouble getting this to work unless I put this line after the line that defines the $wgSitename variable in LocalSettings.php.

Further Links[edit]