Syntax Highlighting for MediaWiki: GeSHiHighlight

From Augix' Wiki

Jump to: navigation, search

Contents

Document

http://www.mediawiki.org/wiki/Extension:GeSHiHighlight

Demo

The codes writing in MediaWiki:

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

The Highlighted codes:

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

Installation

  • Download GeSHi (found here: http://qbnz.com/highlighter/index.php)
  • You'd better delete its doc and contrib folder, put geshi.php and geshi folder directly in the extension folder of MediaWiki.
  • Copy & paste the following code to a file called GeSHiHighlight.php
  • Put GeSHiHighlight.php in your MediaWiki installations' extensions folder
    • this php file enables the tag, tag is much more convenient. this script also search the available syntax file in the geshi folder.
  • Add:
    include("extensions/GeSHiHighlight.php");
    to LocalSettings.php in your MediaWiki folder

GeSHiHighlight.php

<?php
# 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(rtrim(ltrim($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, "."));
			}
		}
 
		closedir($handle);
	}
	sort($lang_list); //sort the output, i like ordered lists in Wiki Version page :)
	return $lang_list;
} 
?>

for R

In this GeSHiHighlight library, there is a file name perl.php, based on this file, I wrote a file named R.php which help wiki to highlight a R script.

Todo

Improving Highlighter, for example: display line number.

Personal tools