GeSHi

From University of Washington - Ubicomp Research Page
Revision as of 00:53, 10 October 2006 by Jonathan Lester (talk | contribs)
Jump to navigationJump to search

Overview

GeSHi or the Generic Syntax Highlighter is a PHP program which enables you to have your code highlighted in color, like you would see in Emacs, Visual Studio, Matlab, etc. Once you have it installed, it's really simple to use the code highlighting feature while writing your pages. Simply place the name of the language you would like to use in brackets before/after the code, like so <myLanguage> for cur=1 blah return </myLanguage>.

Example

    If we have some Matlab code we want to include in our page we would just need to place it within a Matlab block, like so:
    <Matlab>for cur=1
       blah 
    return 
    </Matlab> 
    

    Which would give you the following: <matlab>for cur=1

      blah 
    

    return </matlab> Note: The first line of code was placed on the same line as the Matlab block to avoid a leading carriage return.

    See the GeshiHighlight.php section in the installation instructions for another example of code highlighting with PHP.

Supported Languages

    The following languages are currently supported by GeSHi:
    "actionscript-french"  "actionscript"     "ada"            "apache"
    "applescript"          "asm"              "asp"            "bash"
    "blitzbasic"           "caddcl"           "cadlisp"        "c_mac"
    "c"                    "cpp"              "csharp"         "css"
    "delphi"               "diff"             "div"            "dos"
    "d"                    "eiffel"           "freebasic"      "gml"
    "html4strict"          "ini"              "inno"           "java"
    "javascript"           "lisp"             "lua"            "matlab"
    "mpasm"                "mysql"            "nsis"           "objc"
    "ocaml-brief"          "ocaml"            "oobas"          "oracle8"
    "pascal"               "perl"             "php-brief"      "php"
    "python"               "qbasic"           "ruby"           "scheme"
    "sdlbasic"             "smarty"           "sql"            "vbnet"
    "vb"                   "vhdl"             "visualfoxpro"   "xml"
    


Installation

To install the GeSHi plugin you need to:

  1. Download the latest version of GeSHi from the GeSHi homepage
  2. Extract the .tar.gz into your extensions directory (you should end up with extensions/geshi)
  3. Create a new file named GeSHiHighlight.php your extensions directory. And paste in the code from the GeshiHighlight.php section (everything between <?php ?>)
    • If you need to change the location of your GeSHi installation be sure to change extensions/geshi/geshi/, on lines 12 and 48, to the appropriate directory.
  4. Ensure that your directory structure looks similar to Directory Structure
  5. Edit your wiki's LocalSettings.php file and add the line
<php>include_once( 'extensions/GeSHiHighlight.php' );</php>
I typically add this above the $wgSiteName line.
  • Make sure that the filename and path match your configuration (i.e. if you didn't do the fancy GeSHi capitalization or put it in another directory)
  • Done, if you want to change the colors a particular language is using you can edit the definitions inside the geshi directory.


Your extensions directory should now look something like this (see Directory Structure for more detailed output) <bash>myWiki/extensions/geshi/ ] ls contrib docs geshi geshi.php </bash>

Directory Structure

    For example if have the tree command you would see something similar to this inside your extensions directory: <bash>tree extensions/ extensions/ |-- GeSHiHighlight.php `-- geshi |-- contrib | |-- cssgen.php | `-- example.php |-- docs | |-- BUGS | |-- CHANGES | |-- COPYING | |-- README | |-- THANKS | |-- TODO | |-- api | | |-- __filesource | | | |-- fsource_core__contribcssgen.php.html | | | | ... <snip> ... | | | `-- fsource_core__geshixml.php.html | | |-- blank.html | | |-- classtrees_core.html | | |-- core | | | |-- GeSHi.html | | | |-- _contrib_cssgen_php.html | | | | ... <snip> ... | | | `-- _geshi_xml_php.html | | |-- elementindex.html | | |-- elementindex_core.html | | |-- errors.html | | |-- index.html | | |-- li_core.html | | |-- media | | | |-- banner.css | | | |-- images | | | | |-- AbstractClass.png | | | | | ... <snip> ... | | | | `-- up_button.png | | | |-- lib | | | | `-- classTree.js | | | `-- stylesheet.css | | |-- packages.html | | `-- todolist.html | |-- geshi-doc.html | `-- geshi-doc.txt |-- geshi | |-- actionscript-french.php | | ... <snip> ... | `-- xml.php `-- geshi.php 10 directories, 241 files </bash>

GeshiHighlight.php

    The code for adding GeSHi to MediaWiki is shown below (with some slight modifications) and can also be found at http://www.wickle.com/wiki/index.php/Geshi_Syntax_Highlight_extension and http://meta.wikimedia.org/wiki/User_talk:Coffman/Geshi_Syntax_Highlight_extension. <php><?php
    1. GeshiHighlight.php
    2. By: E. Rogan Creswick (aka: Largos)
    3. largos@ciscavate.org
    4. ciscavate.org/wiki/
    5. Loosely based on SyntaxHighlight.php by Coffman, (www.wickle.com)
    6. Code arranged and packaged by Coffman (www.wickle.com) ;) 10-nov-2004
    7. Bug fix by Douglas McLaughlin (www.stricq.com) 9-Oct-2005
    include('extensions/geshi/geshi.php'); global $lang; class GeshiSyntaxSettings { }; $wgGeshiSyntaxSettings = new GeshiSyntaxSettings; $wgExtensionFunctions[] = "wfGeshiSyntaxExtension"; function wfGeshiSyntaxExtension() { global $wgParser;
    1. $langArray = array("actionscript","ada","apache","asm","asp","bash",
    2. "caddcl","cadlisp","c","cpp","css","delphi",
    3. "html4strict","java","javascript","lisp", "lua",
    4. "nsis","oobas","pascal","perl","php-brief","php",
    5. "python","qbasic","sql","vb","visualfoxpro","xml");
    $langArray = array( "actionscript-french", "actionscript", "ada", "apache", "applescript", "asm", "asp", "bash", "blitzbasic", "caddcl", "cadlisp", "c_mac", "c", "cpp", "csharp", "css", "delphi", "diff", "div", "dos", "d", "eiffel", "freebasic", "gml", "html4strict", "ini", "inno", "java", "javascript", "lisp", "lua", "matlab", "mpasm", "mysql", "nsis", "objc", "ocaml-brief", "ocaml", "oobas", "oracle8", "pascal", "perl", "php-brief", "php", "python", "qbasic", "ruby", "scheme", "sdlbasic", "smarty", "sql", "vbnet", "vb", "vhdl", "visualfoxpro", "xml" ); foreach ($langArray as $lang){ $func = create_function('$text','$geshi = new GeSHi($text,"'.$lang.'","extensions/geshi/geshi/"); return $geshi->parse_code();');
    1. If GeSHI doesn't work then try commenting out the previous line and uncomment this next line to
    2. see the errors that GeSHI is encountering. This will output the error stream rather than the parsed stream
    3. $geshi->error();');
    $wgParser->setHook($lang,$func); } } ?> </php>