Extension:SyntaxHighlight GeSHi

From MediaWiki.org
Jump to: navigation, search
This extension is bundled with MediaWiki 1.21 and above. Thus you do not have to download it again, but you still need to enable the extension.
MediaWiki extensions manual
Crystal Clear action run.png

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
Latest version continuous updates
MediaWiki 1.24.+ (older versions compatible with MW 1.11.+ are available)
Database changes No
License GNU General Public License 2.0 or later
  • $wgSyntaxHighlightDefaultLang
  • $wgSyntaxHighlightKeywordLinks
Hooks used


Translate the SyntaxHighlight GeSHi extension if it is available at translatewiki.net

Check usage and version matrix; code metrics


Open tasks · Report a bug

The SyntaxHighlight GeSHi extension allows to display formatted source code with the <syntaxhighlight> tag.

This extension also adds coloring according to the code language settings. Like the <pre> tags and the <poem> tags, the tags shows the coding exactly as it was typed, preserving white space.

This extension also can create line numbers.

Usage[edit | edit source]

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

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


    $v = "string";    // sample initialization
html text
    echo $v;         // end of php code

Alternative <source> tag[edit | edit source]

Before rev:50696, the extension used the tag <source>. This is still supported, but <syntaxhighlight> avoids conflicts if your source code itself contains <source> (for example XML).

Parameters[edit | edit source]

Defines what programming language the source code is using. This affects how the extension highlights the source code. See the section "Supported languages" in this page for details of supported languages.
Type of line numbering to use. If you do not provide this parameter, then lines will not be numbered. Corresponds to the enable_line_numbers flag in GeSHi.
Equivalent to line="GESHI_FANCY_LINE_NUMBERS"
line start="n"
Use together with the parameter "line". Define the start number of the line. line start="55" will number lines starting at 55, then 56, 57, and so on. Corresponds to start_line_numbers_at method on GeSHi
Specifies which line is highlighted. Note that the parameter line start="??" doesn't affect how it counts the lines. You can specify more lines by separating them with commas (e.g. highlight="1,4,8").
Specifies what container is used to enclose the source code. Takes values "pre" (default value), "div", "none". Corresponds to set_header_type method on GeSHi. "div" causes text to wrap, which is helpful if source code is extending off the edge of the window and requires scrolling; "none" lets you put small code snippets inline.
Type the name of the parameter to enable the strict mode. Corresponds to enable_strict_mode method on GeSHi.

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.

More Usage[edit | edit source]

When line numbering is added with line, long code lines will be wrapped. See the example below. When text is the selected language, and numbering is used, the behaviour resembles the use of pre tags with numbering and long-line wrapping.

The following example shows how to color an HTML code listing:

<syntaxhighlight lang="html4strict" line start="100" highlight="5" enclose="div">
HTML module goes here...

A typical result is just:

  1. <!--This is a comment. Comments are not displayed in the browser-->
  2. <table align=center style="background: ivory;color:maroon;font-style:italic;font-family:arial;font-weight:bold;font-size:10pt;">
  3. <tr><th> Heading 1 </th><th> Heading 2 </th></tr>
  4. <tr>
  5. <td style="padding:10px;"> This is cell 1 text </td>
  6. <td style="padding:10px;"> This is cell 2 text </td>
  7. </tr>
  8. </table>

Supported languages[edit | edit source]

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

Note that installed GeSHi version (can be revealed by previewing e.g. <syntaxhighlight lang="-"></syntaxhighlight>) might not be the most recent version; see bugzilla:10967 for Wikimedia sites.

The GeSHi sources for each language can be found in VCS on SourceForge. Remember to go and select the version that the MediaWiki installation uses.

Code Language
4cs 4CS
6502acme MOS 6502 (6510) ACME Cross Assembler
6502kickass MOS 6502 (6510) Kick Assembler
6502tasm MOS 6502 (6510) TASM/64TASS
68000devpac Motorola 68000 - HiSoft Devpac ST 2 Assembler
abap ABAP
actionscript ActionScript
actionscript3 ActionScript3
ada Ada
algol68 ALGOL 68
apache Apache Configuration
applescript AppleScript
apt_sources Apt sources
arm ARM Assembler
asm Assembly
asp Active Server Pages (ASP)
asymptote Asymptote
autoconf Autoconf
autohotkey AutoHotkey
autoit AutoIt
avisynth AviSynth
awk AWK
bascomavr BASCOM AVR
bash Bash
basic4gl Basic4GL
bf Brainfuck
bibtex BibTeX
blitzbasic Blitz BASIC
bnf Backus–Naur Form
boo Boo
c C
c_loadrunner C Loadrunner
c_mac C (Mac)
caddcl AutoCAD DCL
cadlisp AutoLISP
cfdg CFDG
cfm ColdFusion Markup Language
chaiscript ChaiScript
cil Common Intermediate Language (CIL)
clojure Clojure
cmake CMake
cobol COBOL
coffeescript CoffeeScript
cpp C++
cpp-qt C++ (Qt toolkit)
csharp C#
css Cascading Style Sheets (CSS)
cuesheet Cue sheet
d D
dcl DIGITAL Command Language
dcpu16 DCPU-16
dcs Data Conversion System
delphi Delphi
diff Diff
div DIV
dos DOS batch file
dot DOT
e E
ecmascript ECMAScript
eiffel Eiffel
email Email (mbox \ eml \ RFC format)
epc Enerscript
erlang Erlang
euphoria Euphoria
f1 Formula One
falcon Falcon
fo FO
fortran Fortran
freebasic FreeBASIC
freeswitch FreeSWITCH
fsharp Fsharp
gambas Gambas
gdb GDB
genero Genero
genie Genie
gettext gettext
glsl OpenGL Shading Language (GLSL)
gml Game Maker Language (GML)
gnuplot gnuplot
go Go
groovy Groovy
gwbasic GW-BASIC
haskell Haskell
haxe Haxe
hicest HicEst
hq9plus HQ9+
html4strict HTML
html5 HTML5
icon Icon
idl Uno IDL
ini INI
inno Inno
intercal INTERCAL
io Io
j J
java Java
java5 Java(TM) 2 Platform Standard Edition 5.0
javascript JavaScript
kixtart KiXtart
klonec Klone C
klonecpp Klone C++
latex LaTeX
lb Liberty BASIC
ldif LDAP Data Interchange Format
lisp Lisp
llvm LLVM
locobasic Locomotive BASIC
logtalk Logtalk
lolcode LOLCODE
lotusformulas Formula language
Code Language
lotusscript LotusScript
lscript LightWave 3D
lsl2 Linden Scripting Language
lua Lua
magiksf Magik
m68k Motorola 68000 Assembler
make make
mapbasic MapBasic
matlab MATLAB M
mirc mIRC scripting language
mmix MMIX
modula2 Modula-2
modula3 Modula-3
mpasm Microchip Assembler
mxml MXML
mysql MySQL
nagios Nagios
netrexx NetRexx
newlisp NewLISP
nsis Nullsoft Scriptable Install System (NSIS)
oberon2 Oberon-2
objc Objective-C
objeck Objeck
ocaml OCaml
ocaml-brief OCaml
octave Octave
oobas OpenOffice.org Basic
oorexx Object REXX
oracle11 Oracle 11 SQL
oracle8 Oracle 8 SQL
oxygene Oxygene
oz Oz
parasail ParaSail
parigp PARI/GP
pascal Pascal
pcre Perl Compatible Regular Expressions
per per
perl Perl
perl6 Perl 6
pf PF
php PHP
php-brief PHP (deprecated – no colors, odd framing)
pic16 PIC assembly language
pike Pike
pixelbender Pixel Bender
pli PL/I
plsql PL/SQL
postgresql PostgreSQL
povray Persistence of Vision Raytracer
powerbuilder PowerBuilder
powershell Windows PowerShell
proftpd ProFTPD
progress OpenEdge Advanced Business Language
prolog Prolog
properties Properties file
providex ProvideX
purebasic PureBasic
pycon Python
pys60 PyS60
python Python
q Q
qbasic QBasic/QuickBASIC
rails Rails
rebol Rebol
reg Windows Registry
rexx Rexx
robots robots.txt
rpmspec RPM Spec files
rsplus R
ruby Ruby
sas SAS
scala Scala
scheme Scheme
scilab Scilab
sdlbasic SdlBasic
smalltalk Smalltalk
smarty Smarty
spark SPARK
sparql SPARQL
sql SQL
stonescript StoneScript (Scripting language for ShiVa3D)
systemverilog SystemVerilog
tcl Tcl
teraterm Tera Term
text Plain text
thinbasic thinBasic
tsql Transact-SQL
typoscript TypoScript
unicon Unicon
upc Unified Parallel C
urbi URBI
uscript UnrealScript
vala Vala
vb Visual Basic
vbnet Visual Basic .NET
vedit VEDIT
verilog Verilog
vhdl VHDL
vim Vim script
visualfoxpro Visual FoxPro
visualprolog Visual Prolog
whitespace Whitespace
whois Whois
winbatch Winbatch
xml XML
xorg_conf Xorg.conf
yaml YAML
xpp Microsoft Dynamics AX
z80 ZiLOG Z80 Assembler
zxbasic ZXBasic

Extra features[edit | edit source]

Default Source Language[edit | edit source]

Added in rev:50693.

If the site mainly quotes the source code of a specific programming language, it would be helpful to set a default language.

To do so, add a new variable to LocalSettings.php, just after the require_once line. We set C programming language as an example:

require_once "$IP/extensions/SyntaxHighlight_GeSHi/SyntaxHighlight_GeSHi.php";
$wgSyntaxHighlightDefaultLang = "c";

Default Links for Keywords[edit | edit source]

To do so, add a new variable to LocalSettings.php, just after the require_once line. We set link to work as an example:

require_once "$IP/extensions/SyntaxHighlight_GeSHi/SyntaxHighlight_GeSHi.php";
$wgSyntaxHighlightKeywordLinks = true;

Installation[edit | edit source]

The MediaWiki extension requires the GeSHi project files from SourceForge to work! To make this easier to install and maintain, the extension in git includes the Geshi project in a subdirectory named geshi.


  • GeSHi 1.1.2alpha3 does not work with the MediaWiki extension.
  • You can delete the extensions/SyntaxHighlight_GeSHi/geshi/docs directory to save a few megabytes.

Step 1: downloading[edit | edit source]

Use a Git tool to clone https://github.com/GeSHi/geshi-1.0 in the extensions directory of your wiki. For example using the Git command-line tool (from within wiki/extensions/):

git clone https://github.com/GeSHi/geshi-1.0.git

If you accidentally do this in the wrong directory, you will need to move clone from the current location to wiki/extensions/, like this:

mv SyntaxHighlight_GeSHi /path/to//wiki/extensions/


Or you can use the ExtensionDistributor.

Step 2: Installation[edit | edit source]

Add this line to the end of your LocalSettings.php:

require_once "$IP/extensions/SyntaxHighlight_GeSHi/SyntaxHighlight_GeSHi.php";
GeSHi installation (optional)

If you didn't put GeSHi in the extensions/SyntaxHighlight_GeSHi/geshi directory, then change the following line in SyntaxHighlight_GeSHi.class.php to suit the path of your geshi.php file

require "geshi/geshi.php";

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

Here's a shortcut using the PHP dirname() function with the magic __FILE__ constant:

require (dirname(__FILE__).'/geshi/geshi.php');

VisualEditor integration[edit | edit source]

The plugin enables direct editing with VisualEditor. A popup is opened when a user wants to edit source or syntaxhighlight sections. For this to work, VisualEditor must be installed and configured from the latest git version, same for Parsoid. The feature randomly does not work with older Parsoid versions.

Configuration[edit | edit source]

If you want the dashed border like for <pre> tags you have to add them again.

Method 1: CSS file[edit | edit source]

This method requires r52346 or higher of this extension.

Add to MediaWiki:Geshi.css, MediaWiki:Monobook.css or MediaWiki:Common.css pages:

div.mw-geshi {
  padding: 1em; 
  margin: 1em 0; 
  border: 1px dashed #2f6fab;
  background-color: #f9f9f9;

This will give all GeSHi output (except for enclose="none") a dashed border almost identical to <pre> in monobook/main.css

Method 2: Inline CSS[edit | edit source]

You can also edit the SyntaxHighlight_GeSHi.class.php around line 215 (Line 264 in SVN trunk 82481). Look for this:

$css[] = "\tline-height: normal; border: 0px none white;";

and change it to:

$css[] = "\tline-height: normal; border: 1px dashed #2f6fab;";

On a Unix-like system simply type this command in the directory containing "SyntaxHighlight_GeSHi.class.php":

sed -i 's/$css\[\] = "\\tline-height: normal; border: 0px none white;";/
$css\[\] = "\\tline-height: normal; border: 1px dashed #2f6fab;";/g' \

Link to Extension Talk (2008): Extension talk:SyntaxHighlight GeSHi/Archive 2008#Problem with CSS: Default style for pre is overwritten

See also rev:52346.
  • If you already add the extension to LocalSettings.php before changing this, comment it out via "#" and refresh the page, then remove the # and refresh one more time.

Default DIV Based Rendering[edit | edit source]

Long strings will cause the page width to blow up when rendered on a web page. The solution to this is to use the enclose="div" tag, this can be enabled by default by editing SyntaxHighlight_GeSHi.class.php near line 156. Look for the following:

if ( isset( $args['enclose'] ) ) {
  if ( $args['enclose'] === 'div' ) {
    $enclose = GESHI_HEADER_DIV;
  } elseif ( $args['enclose'] === 'none' ) {
    $enclose = GESHI_HEADER_NONE;

and append an else case to the if statement:

if ( isset( $args['enclose'] ) ) {
  if ( $args['enclose'] === 'div' ) {
    $enclose = GESHI_HEADER_DIV;
  } elseif ( $args['enclose'] === 'none' ) {
    $enclose = GESHI_HEADER_NONE;
} else {
  $enclose = GESHI_HEADER_DIV;}

See also[edit | edit source]

Language: English  • Deutsch • 日本語 • русский