Extension:SyntaxHighlight

The SyntaxHighlight extension, formerly known as SyntaxHighlight_GeSHi, provides rich formatting of source code using the   tag.

It is powered by the [http://pygments.org Pygments] library and supports hundreds of different programming languages and file formats.

Like the   and   tags, the text is rendered exactly as it was typed, preserving any white space.

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

is the result of the following wikitext markup:

In older versions (before MediaWiki 1.16), the extension used the tag  .

This is still supported, but   may help avoid conflicts if your source code itself contains   tags (for example XML).

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

Encasing code blocks in borders can be done by inserting a line like <tvar|css> </> in the section above.

Control over font family used can also be exercised by inserting a line like <tvar|css> </> into the section above.

Syntax highlighting error category
The extension adds pages that have a bad <tvar|lang> </> attribute in a <tvar|source> </> or <tvar|syntaxhighlight> </> tag to a link>Special:MyLanguage/Help:Tracking categories</>|tracking category.

The message key <tvar|msg>MediaWiki:syntaxhighlight-error-category</> determines the category name; on this wiki it is <tvar|category>Category:</>.

The most common error that leads to pages being tagged with this category is a <tvar|syntaxhighlight> </> or <tvar|source> </> tag with no <tvar|lang> </> attribute at all.

These can typically either be replaced with <tvar|pre> </>, or <tvar|bash> </> or <tvar|text> </> can be added to the tag.

The second most common error is the use of <tvar|sh> </> or <tvar|shell> </> which are unsupported.

These can typically be replaced with <tvar|bash> </> or <tvar|shell> </>.

lang
The <tvar|lang> </> attribute defines what lexer should be used.

The language affects how the extension highlights the source code.

The pygments parser is case sensitive and all languages have at least one capital letter (lower case language names are [<tvar|url>http://lists.gnu.org/archive/html/bug-global/2014-12/msg00008.html</> reserved for gtags]).

See the section section>#Supported_languages</>|Supported languages for details of supported languages.

Specifying an invalid or unknown name will tag the page with a tracking category.

See the section section>#Syntax highlighting error category</>|Syntax highlighting error category in this page for details.

line
The <tvar|line> </> attribute enables line numbers.

start
The <tvar|start> </> attribute (in combination with <tvar|line> </>) defines the first line number of the code block.

For example, <tvar|start> </> will make line numbering start at 55.

highlight
The <tvar|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, <tvar|highlight> </>) or ranges using two line numbers and a hyphen (for example, <tvar|highlight2> </>).

Note that the line number specification ignores any renumbering of the displayed line numbers with the <tvar|start> </> attribute.

is the result of

inline
The 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 <tvar|enclose> </> attribute results in the same behavior.

Note that line breaks can occur at any space between the opening and closing tags unless the source code is marked non-breakable with <tvar|class> </> (on those wikis that support it; see below) or <tvar|style> </>.

For example:

<tvar|code> </> is a lambda expression in Python.

Is the result of:

<tvar|code> </> is a lambda expression in Python.

class
When <tvar|inline> </> is used, <tvar|nowrap> </> (on those wikis that support it; not on MediaWiki itself) specifies that line breaks should not occur at spaces within the code block.

For example:

Without <tvar|nowrap> </>:

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx With : xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

style
The <tvar|style> </> attribute allows CSS attributes to be included directly.

This is equivalent to enclosing the block in a <tvar|div> </> (not <tvar|span> </>) tag.

The <tvar|size> </> attribute cannot be specified this way; it requires an enclosing <tvar|span> </> tag as described below under anchor>#Advanced</>|Advanced.

For example:

Is the result of:

Supported languages
The Pygments library provides support for hundreds of computer languages and file formats.

A brief list of languages is:

AppleScript, Assembly, Asymptote, Awk, Befunge, Boo, BrainFuck, C, C++, C#, Clojure, CoffeeScript, ColdFusion, Common Lisp, Coq, Cryptol, Crystal, Cython, D, Dart, Delphi, Dylan, Elm, Erlang, Ezhil, Factor, Fancy, Fortran, F#, Gambas, GAP, Gherkin, GL shaders, Groovy, Haskell, IDL, Io, Java, JavaScript, Lasso, LLVM, Logtalk, Lua, Matlab, MiniD, Modelica, Modula-2, MuPad, Nemerle, Nimrod, Objective-C, Objective-J, Octave, OCaml, PHP, Perl, PovRay, PostScript, PowerShell, Prolog, Python 2.x and 3.x (incl. console sessions and tracebacks), R, REBOL, Red, Redcode, Ruby, Rust, S, S-Plus, Scala, Scheme, Scilab, Smalltalk, SNOBOL, Tcl, Vala, Verilog, VHDL, Visual Basic.NET, Visual FoxPro, XQuery, Zephir

([<tvar|external-link1>http://pygments.org/languages/</> full list] and [<tvar|external-link2>http://pygments.org/docs/lexers/</> complete details in the Pygments document]) and there are some mappings for some language names which were supported by GeSHi ([<tvar|external-link3>https://github.com/wikimedia/mediawiki-extensions-SyntaxHighlight_GeSHi/blob/master/SyntaxHighlight.GeSHi.compat.php</> full list]).

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

Configuration

 * Linux:


 * <tvar|setting> </> (optional): Absolute path to pygmentize of the Pygments package. The extension bundles the Pygments package and <tvar|setting> </> points to the bundled version by default, but you can point to a different version, if you want to.  For example: <tvar|setting> </>.
 * <tvar|setting> </>: 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, ..).


 * Windows:


 * If you are hosting your Mediawiki on a Windows machine, you have to set the path for the Pygmentize.exe <tvar|setting> </>
 * If there is no <tvar|pygmentize> </> run <tvar|install> </> from command line inside the <tvar|scripts> </> folder to generate the file.

If you are using the bundled pygmentize binary (<tvar|location>extensions/SyntaxHighlight_GeSHi/pygments/pygmentize</>), make sure your webserver is permitted to execute it.

If your host does not allow you to add executables to your web directory, install python-pygments and add <tvar|setting> </> to LocalSettings.php.


 * Troubleshooting :

After updating to MediaWiki v1.26 and above, some users started reporting problems with the extension.


 * Try pointing <tvar|setting> </> in LocalSettings.php towards an external pygmentize binary.
 * See the [<tvar|url>https://phabricator.wikimedia.org/T128993</> phabricator task] on this for further suggestions and information.

VisualEditor integration
The plugin enables direct editing with link>Special:MyLanguage/Extension:VisualEditor</>|VisualEditor.

A popup is opened when a user wants to edit <tvar|source> </> or <tvar|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 veinstructions>Extension:SyntaxHighlight/VisualEditor</> for details

Advanced
Unlike the <tvar|pre> </> and <tvar|code> </> tags, HTML character entities such as <tvar|nbsp> </> need not have the <tvar|and> </> character escaped as <tvar|and2> </>.

Like the <tvar|pre> </> tag but unlike the <tvar|code> </> tag, tags within the range (other than its own closing tag) need not have the <tvar|less> </> symbol escaped as <tvar|less2> </>, nor does wikitext need to be escaped with a <tvar|nowiki> </> tag.

Furthermore, while <tvar|pre> </> assumes tab stops every 8 characters and renders tabs using actual spaces when the rendered text is copied, <tvar|syntaxhighlight> </> uses 4-space tab stops (except Internet Explorer, which uses 8) and preserves the tab characters in the rendered text; the latter may be changed using an enclosing <tvar|span> </> tag (not <tvar|div> </>, and not using its own <tvar|style> </> attribute).

The <tvar|moz> </> prefix is required for Firefox (from version 4.0), and the <tvar|prefix-o> </> prefix is required for Opera (from version 10.60 to version 15). ( Note that the wiki editing box assumes 8-space tabs. )

This applies only to actual saved pages; previews generated through an edit box or <tvar|special>Special:ExpandTemplates</> may differ.