Extension:GeSHiHighlight (Ajqnic)

From MediaWiki.org

Jump to: navigation, search
Manual on MediaWiki Extensions
List of MediaWiki Extensions
GeSHiHighlight (Ajqnic)

Release status: unknown

Implementation Tag
Description
Author(s) Andrew Nicol
MediaWiki 1.4+
Download see below
Example [1]

Note: This extension is best used in conjunction with the purgePage extension.

This extension works in MediaWiki 1.9.

<?php
# GeSHiHighlight.php (confirmed working for MediaWiki 1.4 - 1.6.5 and GeSHi-1.0.7.3)
# Updated: 08 September 2006
# 
# By: Andrew Nicol
# http://www.nanfo.com
# Example: http://tivoza.nanfo.com/wiki/index.php/EmuProxyZA_/_emuProxyZA.c
#
# This extension 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 extension originally by 
#   Coffman, (www.wickle.com)           
# and the later modifications by
#   E. Rogan Creswick (aka: Largos), creswick_at_gmail.com, wiki.ciscavate.org
#
# 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>CodeExample.txt</php-file>
#
# You will need to upload GeSHi to your wiki for this extension to work, Geshi 
# is available at:
#   http://qbnz.com/highlighter/
# Once you have downloaded it, uncompress it and copy the files into a sub-directory
# named geshi in your extensions directory, you don't need to copy the doc or 
# contrib directory (they are large an unnecessary).
#
# To activate the extension, include it from your LocalSettings.php
# with: include("extensions/GeSHiHighlight.php");
#
# This extension makes use of another one of my extensions which prevents page
# caching when desired, it is very useful in this instance as when a using 
# highlighting on a file a cached page will show the old file without this
# extension. The purgePage extension is available at:
#   http://meta.wikimedia.org/wiki/User:Ajqnic:purgePage
# If you would rather remove the need for the extension find the line below 
# and just comment it out:
#   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
 
include_once('geshi/geshi.php');
 
$wgExtensionFunctions[] = "wfSyntaxExtension";
 
$wgExtensionCredits['other'][''] = array(
        'name' => 'GeSHiHighlight',
        'url' => 'http://www.mediawiki.org/wiki/Extension:GeSHiHighlight_(Ajqnic)',
        'description' => 'Allows for the highlighting of various types of code including php, html, xml, sql, c, pascal, etc');
 
function wfSyntaxExtension() {                                                                                                                                 
        global $wgParser, $wgVersion;
 
        $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");
 
        if ( version_compare( $wgVersion, "1.5" ) >= 0 ) { //If version 1.5 or above, $attrib param is included
                foreach ( $langArray as $lang ){                                                                                                                                                                                              
                        $wgParser->setHook( $lang, create_function( '$text,$attrib', 'return wfSyntaxCode("' . $lang . '", $text, $attrib);'));
                        $wgParser->setHook( $lang.'-file', create_function( '$file_name,$attrib', 'return wfSyntaxFile("' . $lang . '", $file_name, $attrib);'));
                }
        } else {
                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, $attrib = array()) {
        $geshi = new GeSHi(rtrim(ltrim($text,"\n\r")), $lang, "extensions/geshi/geshi"); 
        return wfSyntaxDefaults($geshi, $attrib);
}
 
function wfSyntaxFile($lang, $file_name, $attrib = array()) {
        global $wgUploadPath;
 
        purgePage(); //Function in purgePage.php (you may comment this out if you don't wish to make use of purgePage)
 
        //Determine the uploaded file_name path 
        $file_name = basename($file_name);
        $path = basename($wgUploadPath);
        $hash = md5( $file_name );
        $file_name = "{$path}/" . $hash{0} . "/" . substr( $hash, 0, 2 ) . "/{$file_name}";
 
        //Process the file
        if (is_readable($file_name)) {          
                //$geshi = new GeSHi("//nothing", $lang, "extensions/geshi/geshi");
                //$geshi->load_from_file($file_name, array($lang => array('txt')) );
                $handle = fopen($file_name, "r");
                $contents = fread($handle, filesize($file_name));
                fclose($handle);
                $geshi = new GeSHi($contents, $lang, "extensions/geshi/geshi");
                return wfSyntaxDefaults($geshi, $attrib);
        } else {
                return "GeSHiHighlight: File not Found!";
        }
}
 
function wfSyntaxDefaults($geshi, $attrib) {
                global $wgGeSHi_LineNumbers;
 
        // Original default value - kept for backwards compatibility reasons.
                $LineNumbers = GESHI_NORMAL_LINE_NUMBERS;
 
        // You can set $wgGeSHi_LineNumbers in LocalSettings.php to override the default.
        // Set it to true to enable line numbers, or false to disable them.
        // Alternatively you can use the built-in GeSHi constants, which will also allow fancy line numbers.
        // See http://qbnz.com/highlighter/geshi-doc.html#line-numbers for more info.
                if (isset($wgGeSHi_LineNumbers)) {
                        if ($wgGeSHi_LineNumbers === true)
                                $LineNumbers = GESHI_NORMAL_LINE_NUMBERS; 
                        elseif ($wgGeSHi_LineNumbers === false)
                                $LineNumbers = GESHI_NO_LINE_NUMBERS;
                        else
                                $LineNumbers = $wgGeSHi_LineNumbers;
                }
 
        // You can set the showlinenumbers parameter to override the default value
        // set above on a per-code-snippet basis.
        // Valid values are: "true", "false" and "fancy"
                if (isset($attrib['linenumbers'])) {
                        switch ($attrib['linenumbers']) {
                                case "fancy":
                                        $LineNumbers = GESHI_FANCY_LINE_NUMBERS;
                                        break;
                                case "true":
                                        $LineNumbers = GESHI_NORMAL_LINE_NUMBERS; 
                                        break;
                                case "false":
                                        $LineNumbers = GESHI_NO_LINE_NUMBERS;
                                        break;
                        }
                }
 
        $geshi->enable_classes(); 
        $geshi->set_header_type(GESHI_HEADER_PRE); 
        $geshi->set_overall_class("code"); 
        $geshi->set_encoding("utf-8"); 
        $geshi->enable_line_numbers($LineNumbers); 
        return "<style>".$geshi->get_stylesheet()."</style>".$geshi->parse_code();        
        return $geshi->parse_code(); 
}
 
?>


[edit] Change Log

2 Nov 2007 - HappyDog:

  • Added code to trim $text if it comes from the wiki, so that blank lines are removed from the start, and all whitespace is removed from the end.
  • Added new config variable $wgGeSHi_LineNumbers so that you can over-ride the default line-numbering style for the whole wiki.
  • Added new attribute "linenumbers" which can be set to "true", "false" or "fancy" to override the default wiki setting for line numbers on a per-code-snippet basis.

21 Nov 2006 - Largos:

  • Updated my (Rogan Creswick's) contact info in the file.

9 Sep 2006 - AjqNic:

8 Sep 2006 - AjqNic:

  • Removed support for 'div' code due to the tag conflicting with HTML div
  • Fixed the security bug when viewing files, now only uploaded files may be viewed
  • Added support for the $attrib parameter, required by MediaWiki 1.5 and above

[edit] Related Extensions

If you would like just a PHP syntax highlighter, you can use PHPHightlight.

Personal tools