Extension:SyntaxHighlight/zh

The SyntaxHighlight extension, formerly known as SyntaxHighlight_GeSHi, provides rich formatting of source code using the  tag. It is powered by the 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.

用法
当你安装了本扩展，你就可以在你的wiki使用“syntaxhighlight”标签了. 举个例子，

这是实现上面内容的维基代码

在较老的版本（MediaWiki 1.16 以前）中，这个扩展使用的是 标签 它现在任然可以使用，不过  也许可以帮助你获取比  更准确的结果（例如XML语言）

样式
如果你觉得标签里面的预览文字太大了，你可以把类似于下面的代码拷贝到你wiki的 MediaWiki:Common.css （如果这个页面还没有被建立，那你就建立一个）

Encasing code blocks in borders can be done by inserting a line like  in the section above. Control over font family used can also be exercised by inserting a line like  into the section above.

Syntax highlighting error category
The extension adds pages that have a bad  attribute in a   or   tag to a tracking category. The message key MediaWiki:syntaxhighlight-error-category determines the category name; on this wiki it is Category:.

The most common error that leads to pages being tagged with this category is a  or   tag with no   attribute at all. These can typically be replaced with  or   can be added.

The second most common error is the use of  or   which are unsupported. These can typically be replaced with  or.

支持的语法
The  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 reserved for gtags). See the section Supported languages for details of supported languages.

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.

对齐线
The  attribute enables line numbers.

自定义对齐线的开始位置
The  attribute (in combination with  ) defines the first line number of the code block. For example,  will make line numbering start at 55.

高亮
The  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, ) or ranges using two line numbers and a hyphen (for example,  ). Note that the line number specification ignores any renumbering of the displayed line numbers with the  attribute.

is the result of

同行显示
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  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  (on those wikis that support it; see below) or.

For example:

is a lambda expression in Python.

Is the result of:

is a lambda expression in Python.

class样式
When  is used,   (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 :

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx With : xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

style样式
The  attribute allows CSS attributes to be included directly. This is equivalent to enclosing the block in a  (not  ) tag. The  attribute cannot be specified this way; it requires an enclosing   tag as described below under Advanced.

For example:

Is the result of:

支持的语言
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#, 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), REBOL, Red, Redcode, Ruby, Rust, S, S-Plus, R, Scala, Scheme, Scilab, Smalltalk, SNOBOL, Tcl, Vala, Verilog, VHDL, Visual Basic.NET, Visual FoxPro, XQuery, Zephir

(full list and complete details in the Pygments document) and there are some mappings for some language names which were supported by GeSHi (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:


 * (optional): Absolute path to pygmentize of the Pygments package. The extension bundles the Pygments package and  points to the bundled version by default, but you can point to a different version, if you want to. For example:.
 * : 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
 * If there is no  run   from command line inside the   folder to generate the file.

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  to LocalSettings.php.


 * Troubleshooting:

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


 * Try pointing  in LocalSettings.php towards an external pygmentize binary.
 * See the phabricator task on this for further suggestions and information.

VisualEditor integration
The plugin enables direct editing with VisualEditor. A popup is opened when a user wants to edit  or   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.

Advanced
Unlike the  and   tags, HTML character entities such as   need not have the   character escaped as. Like the  tag but unlike the   tag, tags within the range (other than its own closing tag) need not have the   symbol escaped as , nor does wikitext need to be escaped with a   tag.

Furthermore, while  assumes tab stops every 8 characters and renders tabs using actual spaces when the rendered text is copied,   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   tag (not , and not using own   attribute). The  prefix is required for Firefox (from version 4.0), and the   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 Special:ExpandTemplates may differ.