Extension:GeSHiCodeTag

Info
project: GeSHiCodeTag

version: 1.5

update: 11/22/2006

author: Paul Nolasco

email: paulnolasco@gmail.com

site: http://www.wikics.org

description: A tag to create a syntax-highlighted code using GeSHi

Introduction
I've been searching for a good syntax-highlighting code extension for MediaWiki but unfortunately I can't find one that fit my needs. So I decided to just create one myself since I'll also be using it heavily on my site. I also wrote the code as readable and customizale as possible so you can easily extend its features. Feel free to email me for questions/comments/suggestions/errors. Hope you all like my first (and hopefully not the last) MediaWiki extension!

README
Features:
 * Two types of code tags: simple and advanced.
 * Ability to list all languages supported by GeSHi.
 * Ability to disable/enable the code tags
 * Extendability and readability of the code.

Requirements:
 * GeSHi

Tested on: (If you've tested it and it's working on a different version please add it here! Thanks!)
 * MediaWiki 1.9.3, 1.8.2, 1.7.1, 1.6.8, 1.5.7
 * GeSHi-1.0.7.17, 1.0.7.12
 * PHP 5.1.4, 5.1.2

Issues:
 * Mediawiki version less than 1.5 will only work in Simple Mode since older version doesn't accept argument on tags
 * Mediawiki version 1.5.7 - the "div.php" lang definition errantly highlights the mediawiki  copyrightwarning  system message

Instructions: NOTE: Be sure to change the $languagesPath depending on your directory
 * 1) Download Geshi, extract it and place the entire folder content under yourWikiSite/extensions/
 * 2) Copy the GeshiCodeTag source to notepad and save it as GeshiCodeTag.php
 * 3) Place GeshiCodeTag.php to yourWikiSite/extensions/
 * 4) Add the include tag right before $wgSiteName of yourWikiSite/LocalSettings.php
 * 5) That's it! Copy the "Tag Examples" below to see if it's working correctly

include("extensions/GeshiCodeTag.php");
 * 1) GeSHiCodeTag extension

Usage
There are two code tags that you can use:


 * 1) Simple Tag Format:      <'languages'>source code here
 * 2) Advanced Tag Format:

Tag Examples

- simple code tag echo "Hello World";

- advanced code tag

- advanced code tag with line numbers

- default code tag

- display a list of supported languages

DEMO: Here's the result of all the examples above http://www.wikics.org/GeshiCodeTag_Samples

GeSHiCodeTag
<?php


 * 1) GeshiCodeTag.php
 * By: Paul Nolasco
 * 1) Copyright 2006
 * 2) paulnolasco@gmail.com
 * 3) http://www.wikics.org/
 * 1) paulnolasco@gmail.com
 * 2) http://www.wikics.org/

// change directory accordingly include_once('GeSHi-1.0.7.12/geshi.php'); $languagesPath = "extensions/GeSHi-1.0.7.12/geshi";

// 1 - ENABLED, 0 - DISABLED $codeTag["simple"] = 1;                      // ex. echo $codeTag["advanced"]["mode"] = 1;            // ex. echo // extra options /*		strict mode - http://qbnz.com/highlighter/geshi-doc.html#using-strict-mode ex. " /> $codeTag["advanced"]["strict"] = 0;



$wgExtensionFunctions[] = "ExtensionCodeTag"; $wgExtensionCredits['parserhook'][] = array(        'name' => 'GeSHiCodeTag',         'author' => 'Paul Nolasco',         'url' => 'http://www.wikics.org/', ); $languages = array;

function ExtensionCodeTag {                      global $wgParser, $codeTag, $languages; ReadLanguages; if($codeTag["advanced"]["mode"]) $wgParser->setHook('code', 'AdvancedCodeTag'); if($codeTag["simple"]) foreach($languages as $lang) {                    $wgParser->setHook($lang,                                   create_function( '$source', '$geshi = new GeSHi($source,' . $lang . ', $languagesPath); return $geshi->parse_code;' ));               }       }

function ReadLanguages {              global $languages, $languagesPath; $dirHandle = opendir($languagesPath) or die("ERROR: Invalid directory path - [$languagesPath], Modify the value of \$languagesPath'"); $pattern = "^(.*)\.php$"; while ($file = readdir($dirHandle)) {                      if( eregi($pattern, $file) ) $languages[] = eregi_replace($pattern, "\\1", $file); }       closedir($dirHandle); }

function AdvancedCodeTag ($source, $settings){

global $languages, $languagesPath, $codeTag; $language = array_shift($settings);     // [arg1] $isNumbered = array_shift($settings);   // [arg2]

// [arg1] if($language == "list")                                              // list all languages supported return " List of supported languages for '''Geshi ". GESHI_VERSION. "''': "                  . implode(" ", $languages); if($language != "" && !in_array($language, $languages))              // list languages if invalid argument return " Invalid language argument, \"" . $language . "\", select one from the list: " . implode(" ", $languages); // set geshi $geshi = new GeSHi(trim($source), $language, $languagesPath); $geshi->enable_strict_mode($codeTag["advanced"]["strict"]);

// [arg2] if($isNumbered == "n")                                               // display line numbers $geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS); /*         Add more GeSHi features below http://qbnz.com/highlighter/geshi-doc.html */       return $geshi->parse_code; } ?>

ChangeLog

 * Removed strict mode in the default settings. --Paul516 18:31, 10 November 2006 (UTC)
 * Added strict mode by default to Advanced Mode of GeshiCodeTag --Paul516 04:31, 16 September 2006 (UTC)
 * Trims whitespace at beginning and end of the source code. --Paul516 09:00, 10 September 2006 (UTC)
 * Added feature: If a user enters an invalid language argument it'll return a list of the supported languages. --Paul516 05:59, 1 September 2006 (UTC)

Todo List

 * Create a Mediawiki toobar icon for GeshiCodeTag and show how to add it.

Discussion
Click here for any questions/comments/suggestions regarding GeSHiCodeTag