Extension:SyntaxHighlight GeSHi

From MediaWiki.org
Jump to: navigation, search
MediaWiki extensions manual - list
Crystal Clear action run.png
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
Last version master (17 May 2009)
MediaWiki 1.11.+
License GPLv2
Download
README
Tags
<syntaxhighlight>
Hooks used
ShowRawCssJs

ParserFirstCallInit

Check usage (experimental)
Bugs: list open list all report

The Extension:SyntaxHighlight GeSHi tag displays 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.

Contents

[edit] source tag replaced

Before rev:50696, the <source> tag was used. It is still supported, but <syntaxhighlight> is recommended to avoid possible conflicts. The original syntax is used in some other languages, e.g. XML.

[edit] Usage

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

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

show:

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

[edit] Alternative Tag

<source> is also used in some other languages (for example XML). If your source code contains <source>, it will conflict with this tag since there is no way for the interpreter to identify how each is used.

As of rev:50696, an alternative tag <syntaxhighlight> is provided. <syntaxhighlight> is recommended to avoid the conflicts mentioned above.

[edit] Parameters

lang="??"
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.
line="GESHI_NORMAL_LINE_NUMBERS|GESHI_FANCY_LINE_NUMBERS"
Type of line numbering to use (default is no line numbering). Corresponds to enable_line_numbers the flag in GeSHi.
line start="??"
Use together with the parameter "line". Define the start number of the line. If you type line start="55", it will start counting at 55, then 56,57,58... and so on. Corresponds to start_line_numbers_at method on GeSHi
highlight="??"
Specifies which line is highlighted. Note that the parameter line start="??" doesn't affect how it counts the lines.
enclose="??"
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. Choosing "div" will cause text to wrap, which is helpful if text is extending off the edge of the screen, causing horizontal scrolling.
strict
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.

[edit] More Usage

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">
HTML module goes here...
</syntaxhighlight>

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>
    

[edit] Supported languages

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.

Code Language
abap ABAP
actionscript ActionScript
ada Ada
apache Apache Configuration
applescript AppleScript
asm Assembly
asp Active Server Pages (ASP)
autoit AutoIt
bash Bash
basic4gl Basic4GL
bf Brainfuck
blitzbasic Blitz BASIC
bnf Backus-Naur Form
c C
c_mac C (Mac)
caddcl AutoCAD DCL
cadlisp AutoLISP
cfdg CFDG
cfm ColdFusion Markup Language
cil Common Intermediate Language (CIL)
cobol COBOL
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
gettext GNU internationalization (i18n) library
glsl OpenGL Shading Language (GLSL)
gml Game Maker Language (GML)
gnuplot gnuplot
groovy Groovy
haskell Haskell
hq9plus HQ9+
html4strict HTML
idl Uno IDL
ini INI
inno Inno
intercal INTERCAL
io Io
java Java
java5 Java(TM) 2 Platform Standard Edition 5.0
javascript JavaScript
kixtart KiXtart
klonec Klone C
klonecpp Klone C++
latex LaTeX
lisp Lisp
lolcode LOLCODE
lotusscript LotusScript
lua Lua
Code Language
m68k Motorola 68000 Assembler
make make
matlab MATLAB M
mirc mIRC scripting language
mxml MXML
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
oracle11 Oracle 11 SQL
pascal Pascal
per per
perl Perl
php-brief PHP
php PHP
pixelbender Pixel Bender
plsql PL/SQL
povray Persistence of Vision Raytracer
powershell Windows PowerShell
progress OpenEdge Advanced Business Language
prolog Prolog
providex ProvideX
python Python
qbasic QBasic/QuickBASIC
rails Rails
reg Windows Registry
robots robots.txt
ruby Ruby
sas SAS
scala Scala
scheme Scheme
scilab Scilab
sdlbasic SdlBasic
smalltalk Smalltalk
smarty Smarty
sql SQL
tcl Tcl
teraterm Tera Term
text Plain text
thinbasic thinBasic
tsql Transact-SQL
typoscript TypoScript
vb Visual Basic
vbnet Visual Basic .NET
verilog Verilog
vhdl VHDL
vim Vimscript
visualfoxpro Visual FoxPro
visualprolog Visual Prolog
whitespace Whitespace
winbatch Winbatch
xml XML
xorg_conf Xorg.conf
xpp X++
z80 ZiLOG Z80 Assembler

[edit] Extra features

[edit] Default Source Language

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

[edit] Installation

[edit] Step 1: downloading

Method 1
Manual downloading with snapshot

Click on the snapshot link, also above

Method 2
Manual downloading with subversion

Alternatively, click on the extension's Subversion repository. Open each of the 4 file's pages, and download the files by opening and saving the pages behind the download link. Save (or upload) these files into the extensions/SyntaxHighlight_GeSHi directory of your wiki.

Next, go to the GeSHi project site and download the latest stable release. Unpack (and upload) these files into the extensions/SyntaxHighlight_GeSHi/geshi directory of your wiki.

Notes:

  • GeSHi 1.1.2alpha3 does not work with the MediaWiki extension.
  • You need both the MediaWiki extension files and the GeSHi project files from SourceForge to make it work!
  • You can delete the extensions/SyntaxHighlight_GeSHi/geshi/docs directory to save a few megabytes.
Method 3
Already bundled with subversion (SVN)

Since r46666, we bundle Geshi into subversion. When downloading via the ExtensionDistributor or directly from SVN, there should already be a "geshi" directory inside your SyntaxHighlight_GeSHi directory. If this is the case then GeSHi is already installed.

Method 4
Using an SVN tool

Use an SVN tool to checkout http://svn.wikimedia.org/svnroot/mediawiki/trunk/extensions/SyntaxHighlight_GeSHi/ .

For example using the SVN command-line tool:

svn co http://svn.wikimedia.org/svnroot/mediawiki/trunk/extensions/SyntaxHighlight_GeSHi

This will automatically also download the latest stable version of GeSHi.

Save (or upload) everything into the extensions/SyntaxHighlight_GeSHi directory of your wiki.

Note: You can delete the extensions/SyntaxHighlight_GeSHi/geshi/docs directory and the various .svn directories to save a few megabytes.

You can also use the SVN command-line tool to download the latest stable version without all the .svn directories. This will save you having to find and remove each of them.

svn export http://svn.wikimedia.org/svnroot/mediawiki/trunk/extensions/SyntaxHighlight_GeSHi

[edit] Step 2: Installation

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');

[edit] Configuration

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

[edit] Method 1: CSS file

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

[edit] Method 2: Inline CSS

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;";/
<!!!continued... THE LINE ABOVE AND THE LINE BELOW ARE ALL ONE LINE!> 
$css\[\] = "\\tline-height: normal; border: 1px dashed #2f6fab;";/g' \
SyntaxHighlight_GeSHi.class.php

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.

[edit] Default DIV Based Rendering

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 a 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;}
...

[edit] Bugs and Limitations

You may report bugs by clicking here.

[edit] See also


Language: English  • Deutsch • 日本語 • Русский
Personal tools
Namespaces

Variants
Actions
Navigation
Support
Download
Development
Communication
Print/export
Toolbox