Extension:SyntaxHighlight GeSHi

From MediaWiki.org

Jump to: navigation, search
Manual on MediaWiki Extensions
List of MediaWiki Extensions
SyntaxHighlight_GeSHi

Release status: stable

Implementation Tag
Description Allows source code to be syntax highlighted on the wiki pages.
Author(s) Brion Vibber, Tim Starling and Rob Church
Version r24298 (July 21, 2007)
MediaWiki 1.10.0+
Download Download snapshot

Subversion [Help]
Browse source code
README

Hooks used ShowRawCssJs

ParserFirstCallInit

This extension adds the <source> tag to present formatted source code.

Contents

[edit] Installation

  • Step 1:

Download the extension from the Subversion repository and save it into the extensions/SyntaxHighlight_GeSHi directory of your wiki. If you have shell access to your server, you may do the following to download the extension:

cd extensions
svn co http://svn.wikimedia.org/svnroot/mediawiki/trunk/extensions/SyntaxHighlight_GeSHi
  • Step 2:

(Recommended) Download 1.0.X stable release of Subversion repository in SourceForge.net:

# change to the directory of the SyntaxHighlight_GeSHi extension
cd SyntaxHighlight_GeSHi
# check out a current version of GeSHi into a directory called geshi
svn co http://geshi.svn.sourceforge.net/svnroot/geshi/branches/RELEASE_1_0_X_STABLE/geshi-1.0.X/src/ geshi

The geshi.php file can also be downloaded from GeSHi, and unzip it inside the extensions/SyntaxHighlight_GeSHi directory. (Note: the zip file creates a sub-directory called geshi/, and places the file geshi.php there - Also note that GeSHi 1.1.2alpha3 does not work with this extension!)


(Note: You need both - the mediawiki plugin files and the geshi files from sourceforge to make it work!)

  • Step 3.

Add this line to your LocalSettings.php:

require_once("extensions/SyntaxHighlight_GeSHi/SyntaxHighlight_GeSHi.php");
  • step 4. (Optional)

If needed, change the following line in SyntaxHighlight_GeSHi.class.php to suit the path of your geshi.php file

require( 'geshi/geshi.php' );

Sometimes you'll need to add the absolute path to geshi (/var/www/html/.../geshi/geshi.php)

[edit] Usage

On the wiki page, you can now use "source" elements:

<source lang="php">
<?php
    $v = "string";    // sample initialization
?>
html text
<?
    echo $v;         // end of php code
?>
</source>

[edit] Parameters

The effect and usage of these parameters can be consulted in GeSHi's documentation.

Since r22246, you can override the colors using [[MediaWiki:GeSHi.css]].

[edit] Examples

[edit] C#

<source lang="csharp">
// Hello World in Microsoft C# ("C-Sharp").

using System;

class HelloWorld
{
    public static int Main(String[] args)
    {
        Console.WriteLine("Hello, World!");
        return 0;
    }
}
</source>

Results in:

// Hello World in Microsoft C# ("C-Sharp").
 
using System;
 
class HelloWorld
{
    public static int Main(String[] args)
    {
        Console.WriteLine("Hello, World!");
        return 0;
    }
}

[edit] PHP

<source lang="php">
<?php
  // Hello World in PHP
  echo 'Hello World!';
?>
</source>

Results in:

<?php
  // Hello World in PHP
  echo 'Hello World!';
?>

[edit] Python

<source lang="python">
# Hello World in Python

def main():
    print "Hello World!"

if __name__ == '__main__':
    main()
</source>

Results in:

# Hello World in Python
 
def main():
    print "Hello World!"
 
if __name__ == '__main__':
    main()

[edit] Fortran

bugs:

  • comments for fortran77 do not work (line starts with C)
<source lang="fortran">
C *
C *testing this srcipt
C *
c * comments for fortran77 do not work!

hello = "Hello World!"

if hello .eq. 'test' then
 !do nothing
else
 !do something
 x = 1
endif
</source>

Results in:

C *
C *testing this srcipt
C *
c * comments for fortran77 do not work!
 
hello = "Hello World!"
 
if hello .eq. 'test' then
 !do nothing
else
 !do something
 x = 1
endif

[edit] Supported languages

These are the languages known by GeSHi that can be used in the lang parameter:

Code Language
abap ABAP
actionscript ActionScript
ada Ada
apache Apache Log
applescript AppleScript
asm ASM
asp Active Server Pages (ASP)
autoit AutoIt
bash Bash
basic4gl Basic4GL
blitzbasic Blitz BASIC
bnf Backus-Naur Form
c C
c_mac C (Mac)
caddcl AutoCAD DCL
cadlisp AutoLISP
cfdg CFDG
cfm ColdFusion
cpp-qt C++ (Qt toolkit)
cpp C++
csharp C#
css Cascading Style Sheets (CSS)
d D
delphi Delphi
diff Diff
div DIV
dos DOS batch file
dot DOT
eiffel Eiffel
fortran Fortran
freebasic FreeBASIC
genero Genero
gml Game Maker Language (GML)
groovy Groovy
haskell Haskell
html4strict HTML
idl Uno IDL
ini INI
inno Inno
io Io
java Java
java5 Java(TM) 2 Platform Standard Edition 5.0
javascript JavaScript
latex LaTeX
Code Language
lisp Lisp
lua Lua
m68k Motorola 68000 Assembler
matlab MATLAB M
mirc mIRC scripting language
mpasm Microchip Assembler
mysql MySQL
nsis Nullsoft Scriptable Install System (NSIS)
objc Objective-C
ocaml-brief OCaml
ocaml OCaml
oobas OpenOffice.org Basic
oracle8 Oracle 8 SQL
pascal Pascal
per per
perl Perl
php-brief PHP
php PHP
plsql PL/SQL
python Python
qbasic QBasic/QuickBASIC
rails Rails
reg Windows Registry
robots robots.txt
ruby Ruby
sas SAS
scheme Scheme
sdlbasic SdlBasic
smalltalk Smalltalk
smarty Smarty
sql SQL
tcl Tcl
text Plain text
thinbasic thinBasic
tsql Transact-SQL
vb Visual Basic
vbnet Visual Basic .NET
vhdl VHDL
visualfoxpro Visual FoxPro
winbatch Winbatch
xml XML
xpp X++
z80 ZiLOG Z80 Assembler

[edit] See also

This extension is being used on one or more of Wikimedia's wikis. It means that the extension is stable and works well enough to be used by such high traffic websites. A full list of the extensions installed on a particular wiki is produced by Special:Version on that wiki.

Personal tools