A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.mediawiki.org/wiki/Extension:SyntaxHighlight_GeSHi below:

Extension:SyntaxHighlight - MediaWiki

This extension

comes with MediaWiki 1.21

and later, so you do not need to download it. The remaining configuration instructions must still be followed.

For syntax highlighting in wikitext editors, such as the source editor, to make editing easier, see the CodeMirror extension or the userscripts of Remember the dot and Cacycle.

The SyntaxHighlight extension, formerly known as SyntaxHighlight_GeSHi, 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> and <poem > tags, the text is rendered exactly as it was typed, preserving any white space.

The SyntaxHighlight extension does not work on wiki installations hardened using Hardened-PHP due to the lack of proc_open, shell_exec and other functions. See T250763.

The ‎<syntaxhighlight> tag has become expensive since 1.39 for 5,000 or some other extreme amount of syntaxhighlight tags in a single page such as for Wikibooks. Increase webserver timeout value in environments in extreme cases. (See T316858.)

Usage

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

def quick_sort(arr):
	less = []
	pivot_list = []
	more = []
	if len(arr) <= 1:
		return arr
	else:
		pass

is the result of the following wikitext markup:

<syntaxhighlight lang="python" line>
def quick_sort(arr):
	less = []
	pivot_list = []
	more = []
	if len(arr) <= 1:
		return arr
	else:
		pass
</syntaxhighlight>

To highlight transclusion of a source code, use the following more elaborated form requesting substitution: [1]

{{#tag:syntaxhighlight|{{safesubst:Module:Sandbox/userName/myModule}}|lang=lua}}

Prior to MediaWiki 1.16, the extension used the tag ‎<source>. This is still supported, but is deprecated. ‎<syntaxhighlight> should always be used.

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):

/* CSS placed here will be applied to all skins */
.mw-highlight pre {
	font-size: 90%;
    /*  Insert additional CSS styling here */
}

Encasing code blocks in borders can be done by inserting a line like border: 1px dashed blue; in the section above. Control over font family used can also be exercised by inserting a line like font-family: "Courier New", monospace; into the section above.

Syntax highlighting error category

The extension adds pages that have a bad lang attribute in a ‎<syntaxhighlight> tag to 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.

The most common error that leads to pages being tagged with this category is a ‎<syntaxhighlight> tag with no lang attribute at all, because older versions of this extension supported the definition of $wgSyntaxHighlightDefaultLang. These can typically either be replaced with ‎<pre>, or lang="text" can be added to the tag. For example:

<syntaxhighlight lang="text">
...
</syntaxhighlight>

The category may also be added, and the content will not be highlighted, if there are more than 1000 lines or more than 100 kB text.[2]

Parameters

lang

The lang="name" attribute defines what lexer should be used. The language affects how the extension highlights the source code. See #Supported languages for details of supported languages.

def quick_sort(arr):
    less = []
<syntaxhighlight lang="python">
...
</syntaxhighlight>

Specifying an invalid or unknown name will tag the page with a tracking category. See the section #Syntax highlighting error category in this page for details.

line

The line attribute enables line numbers.

def quick_sort(arr):
	less = []
<syntaxhighlight lang="python" line>
...
</syntaxhighlight>

start

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 line 55.

def quick_sort(arr):
    less = []
<syntaxhighlight lang="python" line start="55">
...
</syntaxhighlight>

linelinks

The linelinks attribute (in combination with line) adds link anchors to each line of the code block. You can click on the line numbers to get a link that highlights the selected line, and use them in internal and external links. You can also hold ⇧ Shift and click on another line number to get a link that highlights the selected range of lines (Since MediaWiki 1.42change 1007640). When JavaScript is disabled, single-line highlights can be created and existing ones jump to the right location, but the highlight is missing; however, range highlights don’t work at all, so consider using single-line highlights only for accessibility if meaningfully possible. The value of the attribute is used as a prefix for the anchors, to distinguish multiple code blocks on one page.

def quick_sort(arr):
	less = []
	pivot_list = []
	more = []
	if len(arr) <= 1:
		return arr
	else:
		pass

Click here to jump to and highlight line 3 in the code block above. Click here to jump to and highlight lines 2-4 in the code block above.

<syntaxhighlight lang="python" line linelinks="example">
...
</syntaxhighlight>

[[#example-3|...]]
[[#example-2--example-4|...]]

highlight

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").

The line number specification ignores any renumbering of the displayed line numbers with the start attribute.

def quick_sort(arr):
    less = []
    pivot_list = []
    more = []
    if len(arr) <= 1:
        return arr

is the result of:

<syntaxhighlight lang="python" line start="3" highlight="1,5-7">
...
</syntaxhighlight>

inline

MediaWiki version:

1.26

The attribute indicates that the source code should be inline as part of a paragraph (as opposed to being its own block). (introduced in 1.26)

Using the "enclose" parameter is deprecated; if set to "none", it should be replaced with inline; otherwise, it can be removed entirely.

Line breaks can occur at any space between the opening and closing tags unless the source code is marked non-breakable with class="nowrap" (on those wikis that support it; see below) or style="white-space:nowrap".

For example:

The following lambda x: x * 2 is a lambda expression in Python.

Is the result of:

The following <syntaxhighlight lang="python" inline>lambda x: x * 2</syntaxhighlight> is a [[w:Lambda (programming)|lambda expression]] in Python.

class

When inline is used, class="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 class="nowrap":

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxlambda x: x * 2xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

With style="white-space:nowrap":

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxlambda x: x * 2xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

style

The style attribute allows CSS attributes to be included directly. This is equivalent to enclosing the block in a ‎<div> (not ‎<span>) tag. The tab‑size attribute cannot be specified this way; it requires an enclosing ‎<span> tag as described below under Advanced.

For example:

def quick_sort(arr):
	less = []
	pivot_list = []
	more = []
	if len(arr) <= 1:
		return arr
	else:
		pass

Is the result of:

<syntaxhighlight lang="python" style="border: 3px dashed blue;">
def quick_sort(arr):
	less = []
	pivot_list = []
	more = []
	if len(arr) <= 1:
		return arr
	else:
		pass
</syntaxhighlight>

copy

MediaWiki version:

1.43

The copy attribute adds a button link that copies the content to the clipboard when clicked.

This attribute cannot be used together with the

inline

attribute. If both are specified,

copy

will be ignored.

For example:

def quick_sort(arr):
    less = []

Is the result of:

<syntaxhighlight lang="python" copy>
def quick_sort(arr):
    less = []
</syntaxhighlight>

Supported languages

‎<syntaxhighlight>

using Pygments now includes a native

wikitext

lexer.

Pygments library provides syntax-highlighting for hundreds of programming languages and file formats through its built-in regex-based lexing mechanism termed lexers.

Generally, lang uses the lowercase version of the language name, but many languages also have aliases or "short names" as they are called in the Pygments documentation; see Pygments lexers and SyntaxHighlight.lexers.php.

Languages previously supported by GeSHi have been mapped to equivalent Pygments lexers, see SyntaxHighlightGeSHiCompat.php.

As of January 2020, the list of programming languages supported by Pygments is:

Programming languages

Template languages

Other markup

Since MediaWiki 1.37 more lexers were added with the update of pygments to version 2.10.0 as detailed with

T280117

.

Lexers previously supported by GeSHi

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

The version of this extension bundled with

MediaWiki 1.31

requires

Python

version 3 (

python3

) to be installed on the server. This is a change from the version bundled with

MediaWiki 1.30

, which used Python version 2 (

python

). Note that the

python3

binary must be installed in the execution PATH of the PHP interpreter.

Despite its update to Pygments (and away from GeSHi) and despite its updated name, this extension internally still uses the former file names as stated below.

chmod a+x /path/to/extensions/SyntaxHighlight_GeSHi/pygments/pygmentize


Vagrant installation:

When installing from

Git

, please note that starting from MediaWiki 1.26, and ending with MediaWiki 1.31 this extension requires

Composer

.

So, after installation from Git change to the directory containing the extension e.g. "../extensions/SyntaxHighlight_GeSHi/" and run composer install --no-dev, or when updating: composer update --no-dev.

Alternatively as well as preferably add the line "extensions/SyntaxHighlight_GeSHi/composer.json" to the "composer.local.json" file in the root directory of your wiki like e.g.

{
	"extra": {
		"merge-plugin": {
			"include": [
				"extensions/SyntaxHighlight_GeSHi/composer.json"
			]
		}
	}
}
Now run composer update --no-dev. Voilà!

Warning: When uploading the extension via FTP be sure to upload the pygments/pygmentize file with the transfer type binary.

Configuration

$wgSyntaxHighlightMaxLines, $wgSyntaxHighlightMaxBytes Optional For performance reasons, blobs or pages (JS, Lua and CSS pages) larger than these values will not be highlighted. (introduced in 1.40)
Linux
$wgPygmentizePath Optional Absolute path to pygmentize of the Pygments package. The extension bundles the Pygments package and $wgPygmentizePath points to the bundled version by default, but you can point to a different version, if you want to. For example: $wgPygmentizePath = "/usr/local/bin/pygmentize";.
Windows

If you are using the bundled pygmentize binary (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 $wgPygmentizePath = pygmentize to LocalSettings.php.

Troubleshooting

After updating to MediaWiki v1.26 and above, some users started reporting problems with the extension. There could be cases, when some languages, such as Lua might not get highlighted and by turning on debugging, MediaWiki would throw out the error, Notice: Failed to invoke Pygments: /usr/bin/env: python3: No such file or directory.

VisualEditor integration

The plugin enables direct editing with VisualEditor. A popup is opened when a user wants to edit syntaxhighlight sections. For this to work, VisualEditor must be installed and configured from the latest Git version, same for Parsoid. The feature may not work with older Parsoid versions. See Extension:SyntaxHighlight/VisualEditor for details

Advanced

Unlike the ‎<pre> and ‎<code> tags, HTML character entities such as &nbsp; need not (and should not) have the & character escaped as &amp;. Like the ‎<pre> tag but unlike the ‎<code> tag, tags within the range (other than its own closing tag) need not have the < symbol escaped as &lt;, nor does wikitext need to be escaped with a ‎<nowiki> tag.

Furthermore, while ‎<pre> assumes tab stops every 8 characters and renders tabs using actual spaces when the rendered text is copied, ‎<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 ‎<span style="-moz-tab-size: nn; -o-tab-size: nn; tab-size: nn;"> tag (not ‎<div>, and not using its own style attribute). The -moz- prefix is required for Firefox (from version 4.0 to version 90), and the -o- prefix is required for Opera (from version 10.60 to version 15).[3] (Note that the wiki editing box assumes 8-space tabs.) This applies only to actual saved pages; previews generated through an edit box or Special:ExpandTemplates may differ.

See also

This extension is being used on one or more Wikimedia projects. This probably means that the extension is stable and works well enough to be used by such high-traffic websites. Look for this extension's name in Wikimedia's CommonSettings.php and InitialiseSettings.php configuration files to see where it's installed. A full list of the extensions installed on a particular wiki can be seen on the wiki's Special:Version page.

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4