Extension:SyntaxHighlight/zh

SyntaxHighlight扩展，原先被称作SyntaxHighlight_GeSHi，使用 扩展标签对源代码提供了丰富的格式. 它是由Pygments库提供支持的，并支持数百种不同的编程语言和文件格式.

就像 和 标签，文本将按照输入的内容准确呈现且保留所有空格.

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

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

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

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

代码块的外框可以通过在上方代码中插入一行 实现. 控制使用的字体也可以通过添加 到上面的代码中.

语法高亮错误分类
该扩展将添加在 或 标签中有错误 属性的页面到一个追踪分类. 消息文字MediaWiki:syntaxhighlight-error-category决定了分类的名字，在本维基上为Category:.

多数页面被加入该分类的错误是使用了 或 标签但没有 属性. 这些通常可以用 或 替换.

第二普遍的错误是使用 或 等不受支持的 参数. 这些可以通过 或 替换.

语言
属性定义了应使用什么词法分析器. 该语言会影响扩展如何高亮显示源代码. pygments解析器区分大小写，所有语言都至少有一个大写字母（小写字母名称保留给gtags）. 参阅支持的语言这一章节获得关于受支持语言的详细内容.

指定了一个无效的或未知的语言名称，则该页面会被加入跟踪分类. 参阅语法高亮错误分类章节获取详细信息.

行号
属性启用行号.

开始位置
属性（与 相配合）定义了代码块的第一行行号. 例如， 将会使行号从55开始.

高亮
属性将指定一行或多行将被标记（通过对指定的行显示不同的背景色）. 您可以指定多个行的数字通过逗号分隔（例如， ）或使用一个连字符和两个数字表示范围（例如， ）. 注意，行的数字将忽略任何 属性的重新标号.

上面为下方代码的输出结果：

内联
该属性表示源代码将作为段落的一部分内联（而不是自己的块）. 该选项在MediaWiki 1.26 版本开始可用. 为了保持向后兼容， 也会有同样效果.

注意，换行符可以在标签开始和结束之间的任何空格处出现，若源代码带有 （在那些受支持的维基；见下方）或 则是不换行的.

例如：

是在Python的Lambda 表达式.

是下方的结果：

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.

配置

 * 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.