User talk:Ajqnic:GeSHiHighlight/Cleaned-up-a-little-bit-version

Latest comment: 18 years ago by Kingboyk

Note: The below code has been superseded by the official version which now includes the below adjustments plus a security fix.

I cleaned the script up a little by removing support for div, commenting out the purgecache line, and adding an ID array so that it is listed in Special:Version. Thought I may as well share it, since wiki isn't paper :-) --Kingboyk 05:50, 18 December 2005 (UTC)Reply

<?php
# GeSHiHighlight.php (confirmed working for MediaWiki 1.4 and GeSHi-1.0.7.3)
# 
# By: Andrew Nicol
# http://www.nanfo.com
# Example: http://tivoza.nanfo.com/wiki/index.php/EmuProxyZA_/_emuProxyZA.c
#
# This extention allows for the easy implementation of adding code 
# highlighting to you wiki pages, you can use it for highlighting both 
# files and code. 
#
# It is a modified version of the extention origionaly by 
#   Coffman, (www.wickle.com)           
# and the later modifications by
#   E. Rogan Creswick (aka: Largos), largos@ciscavate.org, ciscavate.org/wiki/
#
# To highlight code, select you language choice from the $langArray list below 
# and add it as follows (e.g. using php language):
#   <php>
#   $foo = 45;
#   for ( $i = 1; $i < $foo; $i++ )
#   {
#     echo "$foo<br />\n";
#     --$foo;
#   }
#   </php>
#
# To highlight an uploaded file, select you language choice from the $langArray 
# list below and add it as follows (e.g. using php language):
#   <php-file>images/b/b8/CodeExample.txt</php-file>
#
# You will need to upload GeSHi to your wiki for this extension to work, Geshi 
# is avaialbe at:
#   http://qbnz.com/highlighter/
# Once you have downloaded it, uncompress it and copy the files into a subdirectory
# named geshi in your extensions directory, you don't need to copy the doc or 
# contrib directoy (they are large an unnecessary).
#
# To activate the extension, include it from your LocalSettings.php
# with: include("extensions/GeSHiHighlight.php");
#
# This extention makes use of another one of my extentions which prevents page
# caching when desired, it is very usefull in this instance as when a using 
# highlighting on a file a cached page will show the old file without this
# extention. The purgePage extention is avvailable at:
#   http://meta.wikimedia.org/wiki/User:Ajqnic:purgePage
# To use the extention find the line below and uncomment it:
#   purgePage(); //Function in purgePage.php
#
# 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

/* Note: The entry for the DIV language in the language array causes problems with some <div> 
tags in the Wiki, i.e. the notice underneath the text area when editting a page. To solve 
this, go to line 64, find inside the array $langArray, the entry "div" and remove that. 
--Jameshales 10:34, 28 November 2005 (UTC) */

include_once('geshi/geshi.php');            

$wgExtensionFunctions[] = "wfSyntaxExtension";

$wgExtensionCredits['other'][''] = array(
        'name' => 'GeSHiHighlight',
        #'url' => 'insert a URL here and uncomment this line, e.g. so users can go to a help page',
        'description' => 'allows for the highlighting of various types of code including '.
           'PHP, HTML, XML, SQL & VB');
                                                                                                                                                              
function wfSyntaxExtension() {                                                                                                                                 
        global $wgParser;

        $langArray = array(     "actionscript-french", "actionscript", "ada", "apache", "applescript", 
                                "asm", "asp", "bash", "c", "caddcl", "cadlisp", "cpp", "csharp", 
                                "css", "c_mac", "d", "delphi", "diff", "dos", "eiffel", 
                                "freebasic", "gml", "html4strict", "ini", "inno", "java", "javascript", 
                                "lisp", "lua", "matlab", "mpasm", "nsis", "objc", "ocaml-brief", 
                                "ocaml", "oobas", "oracle8", "pascal", "perl", "php-brief", "php", 
                                "python", "qbasic", "ruby", "scheme", "sdlbasic", "smarty", "sql", 
                                "vb", "vbnet", "vhdl", "visualfoxpro", "xml");

        foreach ( $langArray as $lang ){                                                                                                                                                                                              
                $wgParser->setHook( $lang, create_function( '$text', 'return wfSyntaxCode("' . $lang . '", $text);'));
                $wgParser->setHook( $lang.'-file', create_function( '$file_name', 'return wfSyntaxFile("' . $lang . '", $file_name);'));
        }                                                                                                                                                      
}                                                                                                                                                              

function wfSyntaxCode($lang, $text) {
        $geshi = new GeSHi($text, $lang, "extensions/geshi/geshi"); 
        return wfSyntaxDefaults($geshi);
}

function wfSyntaxFile($lang, $file_name) {
        //purgePage(); //Function in purgePage.php
        
        $geshi = new GeSHi("//nothing", $lang, "extensions/geshi/geshi"); 
        $geshi->load_from_file($file_name, array($lang => array('txt')) ); 
        return wfSyntaxDefaults($geshi);
}

function wfSyntaxDefaults($geshi) {
        $geshi->enable_classes(); 
        $geshi->set_header_type(GESHI_HEADER_PRE); 
        $geshi->set_overall_class("code"); 
        $geshi->set_encoding("utf-8"); 
        $geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS); 
        return "<style>".$geshi->get_stylesheet()."</style>".$geshi->parse_code();        
        return $geshi->parse_code(); 
}

?>
Return to the user page of "Ajqnic:GeSHiHighlight/Cleaned-up-a-little-bit-version".