GeSHi

From University of Washington - Ubicomp Research Page
Revision as of 00:10, 10 October 2006 by 128.208.4.68 (talk)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

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.

To use GeSHi you simply type the name of the language you would like to use in brackets before/after the code, like so <myLanguage> for i=1 blah return </myLanguage>.


See the GeshiHighlight.php section below to see Geshi in action. The code from GeshiHighlight.php was simply pasted in between a block with:

<php> 
... the code goes here ...
</php>

The following languages are currently supported:

"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"

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>

Installation

To install the plugin you need to:

  • Create a new file called GeSHiHighlight.php and paste the code from the above section into this file; everything between <?php and ?>
    • 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.
  • Install GeSHI by going to GeSHi homepage and downloading the latest version. Extract it to your extensions/geshi directory. Make sure that when you extract GeSHi you end up with a geshi directory inside your extensions directory.
  • 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)


Your extensions directory should look something like this:

myWiki/extensions/geshi/ ] ls
contrib  docs  geshi  geshi.php

For example if have the tree command you would see something similar to this: <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>