From MediaWiki.org
(Redirected from Extension:SyntaxHighlight)
Jump to: navigation, search
This extension is bundled with MediaWiki 1.21 and above. Thus you do not have to download it again.
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, Rob Church, and Ori Livneh
Latest version continuous updates
Database changes No
License GNU General Public License 2.0 or later
  • $wgPygmentizePath
  • $wgSyntaxHighlightModels
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 provides rich formatting of source code using the <syntaxhighlight> tag. It is powered by the Pygments library and supports hundreds of different programming languages and file formats.

Like the <pre> tag and <poem> tags, the text is rendered exactly as it was typed, preserving any white space.

NB Despite the name, this extension no longer uses GeSHi for syntax highlighting, having moved to Pygments in June 2015.

Usage[edit | edit source]

Once installed, you can use "syntaxhighlight" tags on wiki pages. For example,

def quickSort(arr):
    less = []
    pivotList = []
    more = []
    if len(arr) <= 1:
        return arr

is the result of the following wikitext markup:

<syntaxhighlight lang="python">
def quickSort(arr):
    less = []
    pivotList = []
    more = []
    if len(arr) <= 1:
        return arr

In older versions (before MediaWiki 1.16), the extension used the tag <source>. This is still supported, but <syntaxhighlight> may help avoid conflicts if your source code itself contains <source> tags (for example XML).

Styling[edit | edit source]

If the displayed code is too big, you can adjust it by putting the following into the MediaWiki:Geshi.css page in your wiki (create it if it does not exist):

/* CSS placed here will be applied to GeSHi syntax highlighting */
.mw-highlight pre {
 font-size: 1em;

Actually this is a bug that needs to be fixed. Encasing code blocks in borders can be done by inserting a line like border: 1px dashed blue; in the div section above. Control over font family used can also be exercised by inserting a line like font-family: monospace, "Courier New" !important; into both sections above.

Parameters[edit | edit source]

lang[edit | edit source]

The lang="name" attribute defines what lexer should be used. This affects how the extension highlights the source code. See the section Supported languages in this page for details of supported languages.

def quickSort(arr):
    less = []
<syntaxhighlight lang="python">

Specifying an invalid or unknown name will tag the page with a tracking category. The message key MediaWiki:syntaxhighlight-error-category determines the category name, on this wiki it is Category:Pages with syntax highlighting errors.

line[edit | edit source]

The line attribute enables line numbers.

1 def quickSort(arr):
2     less = []
<syntaxhighlight lang="python" line>

start[edit | edit source]

The start attribute (in combination with line) defines the first line number of the code block. For example, line start="55" will make line numbering start at 55.

55 def quickSort(arr):
56     less = []
<syntaxhighlight lang="python" line start="55">

highlight[edit | edit source]

The highlight attribute specifies one or more lines that should be marked (by highlighting those lines with a different background color). You can specify multiple line numbers separated by commas (for example, highlight="1,4,8") or ranges using two line numbers and a hyphen (for example, highlight="5-7").

def quickSort(arr):
    less = []
    pivotList = []
    more = []
    if len(arr) <= 1:
        return arr

is the result of

<syntaxhighlight lang="python" highlight="1,5-7">

inline[edit | edit source]

MediaWiki version: 1.26

The inline attribute indicates that the source code should be inline as part of a paragraph (as opposed to being its own block). This option is available starting with MediaWiki 1.26. For backwards-compatibility, an enclose="none" attribute results in the same behaviour.

For example, lambda x: x * 2 is a lambda expression in Python.

Supported languages[edit | edit source]

The Pygments library provides support for hundreds of computer languages and file formats (full list) and there are some mappings for some language names which were supported by GeSHi (full list).

Pygments does not yet provide a "wikitext" or "mediawiki" lexer. Use "html" instead. (phab:T29828)

Below is a partial list of languages that GeSHi could highlight, with strike-through for languages no longer supported after the switch to Pygments.

Installation[edit | edit source]

  • Download and place the file(s) in a directory called SyntaxHighlight_GeSHi in your extensions/ folder.
  • Add the following code at the bottom of your LocalSettings.php:
wfLoadExtension( 'SyntaxHighlight_GeSHi' );
  • YesY Done - Navigate to Special:Version on your wiki to verify that the extension is successfully installed.

To users running MediaWiki 1.24 or earlier:

The instructions above describe the new way of installing extensions using wfLoadExtension(), since MediaWiki 1.25. If you need to install this extension in earlier versions, instead of wfLoadExtension( 'SyntaxHighlight_GeSHi' );, you need to use:

require_once "$IP/extensions/SyntaxHighlight_GeSHi/SyntaxHighlight_GeSHi.php";

(To run an extension on an earlier release, you may need to download the version of it tagged for that release from Special:ExtensionDistributor.)

When installing from git, please note that starting from MediaWiki 1.26 this extension requires composer. From the directory containing the extension run composer install, or when updating: composer update.

Configuration[edit | edit source]

  • $wgPygmentizePath (optional): The extension bundles the Pygments package, but you can point to a different version.
  • $wgSyntaxHighlightModels: Configure the default lexer for some wiki pages. By default this will highlight javascript and css pages. Additional content models can be configured by extensions (e.g. Lua, JSON, ..).

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.

See also[edit | edit source]

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