Extension:SyntaxHighlight

This extension adds the  tag to present formatted source code.

As of 50696, an alternative tag  is provided. is recommended to avoid possible conflicts. The original syntax  is also used in some other languages, e.g. XML.

Introduction
The  tags allow the display of preformatted code modules but in addition they add coloring according to the code language settings. Like the  tags and the   tags, they preserve white space, that is, they depict the code module exactly as it was typed.

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

show:

Alternative Tag
is also used in some other languages (for example XML). If your source code contains is provided. is recommended to avoid the conflicts mentioned above.

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: Type the name of the parameter to enable line numbering formatting style. Corresponds to enable_line_numbers method on 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.
 * 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.

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: A typical result is just:

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. ) might not be the most recent version;  see 10967 for Wikimedia sites.

Default Source Language
Added in 50693.

If the site mainly quote 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 before the require_once line. We set C programming language as an example:

Bugs and Limitations
''None so far. You may report bugs by clicking here.''

C#
Results in:

Java
Results in:

PHP
Results in:

Python
Results in:

Fortran
bugs:
 * comments for fortran77 now work.

Results in:

SQL
Results in:

XML
Results in:

Step 1
Download the 4 extension files from the Subversion repository and save them 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: (superfluous)
Step 1 apparently now automatically checks out the 1.0.X stable release of GeSHi, rendering this step superfluous.

(Recommended) Download 1.0.X stable release of Subversion repository in SourceForge.net: cd SyntaxHighlight_GeSHi svn co http://geshi.svn.sourceforge.net/svnroot/geshi/branches/RELEASE_1_0_X_STABLE/geshi-1.0.X/src/ geshi
 * 1) change to the directory of the SyntaxHighlight_GeSHi extension
 * 1) check out a current version of GeSHi into a directory called 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 the end of your LocalSettings.php:

Step 4 (Optional)
If needed, change the following line in SyntaxHighlight_GeSHi.class.php to suit the path of your geshi.php file

Sometimes you'll 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:

Problem with CSS / Border
If you want the dashed border like for  tags you have to readd them.

Method 1, CSS

 * This method requires r52346 or higher of this extension.

Add to or : div.mw-geshi { padding: 1em; margin:1em 0; border: 1px dashed #2fab6f; }

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

Method 2,
You can edit the SyntaxHighlight_GeSHi.class.php around line 222. Look for this: and change it to: On a Unix-like system simply type this command in the directory containing "SyntaxHighlight_GeSHi.class.php": Link to Extension Talk (2008): Extension_talk:SyntaxHighlight_GeSHi
 * See also 52346.