Extension:SyntaxHighlight/zh
- 要在使用源代码编辑器时对维基文本进行语法高亮,参阅CodeMirror 扩展或Remember the dot和Cacycle用户脚本。
![]() Category:Stable extensions/zh |
|
---|---|
![]() |
|
Category:Tag extensions/zh | |
描述 | 使维基页面的源代码能语法高亮 |
作者 |
|
最新版本 | 持续更新 |
MediaWiki | 1.25+Category:Extensions with manual MediaWiki version/zh |
否 | |
GNU通用公眾授權條款2.0或更新版本 | |
下載 | Category:Extensions in Wikimedia version control/zh README |
|
|
<syntaxhighlight> |
|
|
|
問題 | 开启的任务 · |
SyntaxHighlight(语法高亮)扩展,早先被称为SyntaxHighlight_GeSHi,使用<syntaxhighlight>
扩展标签对源代码提供富文本格式。
它由Pygments库提供支持,并支持数百种编程语言和文件格式。
如同<pre>
和<poem >
标签,文本将按照输入的内容准确呈现且保留所有空格。
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.
自1.39版本起,<syntaxhighlight>
标签在单个页面(例如Wikibooks)中有5,000及以上数量的syntaxhighlight标签时被视为高开销。
Increase webserver timeout value in environments in extreme cases.
(参见T316858。)
用法
当您安装本扩展后,您就可以在你的wiki使用“syntaxhighlight”标签。举个例子,
def quick_sort(arr):
less = []
pivot_list = []
more = []
if len(arr) <= 1:
return arr
else:
pass
这是实现上面内容的维基代码:
<syntaxhighlight lang="python" line>
def quick_sort(arr):
less = []
pivot_list = []
more = []
if len(arr) <= 1:
return arr
else:
pass
</syntaxhighlight>
To use on source code that is transcluded, for example from a module call, use the #tag
parser and safesubst:
:[1]
{{#tag:syntaxhighlight|{{safesubst:Module:Sandbox/userName/myModule}}|lang=lua}}
在MediaWiki 1.16之前,这个扩展使用的是<source>
标签。
它现在仍然可以使用,但已棄用,應使用<syntaxhighlight>
。
样式
如果您觉得标签里面的预览文字太大,您可以调整它通过把下面的代码拷贝到您wiki的MediaWiki:Common.css
中 (创建它如果不存在的话):
/* 放置在这里的CSS会应用于全部皮肤 */
.mw-highlight pre {
font-size: 90%;
/* Insert additional CSS styling here */
}
代码块的外框可以通过在上方代码中插入一行border: 1px dashed blue;
实现。
控制使用的字体也可以通过添加font-family: "Courier New", monospace;
到上面的代码中。
语法高亮错误分类
该扩展将添加在<syntaxhighlight>
标签中有错误lang
属性的页面到一个追踪分类。
消息文字MediaWiki:syntaxhighlight-error-category决定了分类的名字,在本维基上为Category:Pages with syntax highlighting errors。
导致页面被此类别标记的最常见错误是<syntaxhighlight>
标记,根本没有lang
属性,因为此扩展的旧版本支持$wgSyntaxHighlightDefaultLang
的定义。
这些通常既可以替换为<pre>
,也可以将$bash或lang="text"
添加到标签。
For example:
<syntaxhighlight lang="text">
...
</syntaxhighlight>
如果内容超过1000行或者超过100jB文本,分类也可能被添加,内容不会被高亮。[2]
参数
lang
lang="name"
属性定义了应使用什么词法分析器。
该语言会影响扩展如何高亮显示源代码。
参阅支持的语言这一章节获得关于受支持语言的详细内容。
def quick_sort(arr):
less = []
<syntaxhighlight lang="python">
...
</syntaxhighlight>
指定了一个无效的或未知的语言名称,则该页面会被加入跟踪分类。 参阅语法高亮错误分类章节获取详细信息。
line
line
属性启用行号。
def quick_sort(arr):
less = []
<syntaxhighlight lang="python" line>
...
</syntaxhighlight>
start
start
属性(与line
相配合)定义了代码块的第一行行号。
例如,line start="55"
将会使行号从55开始。
def quick_sort(arr):
less = []
<syntaxhighlight lang="python" line start="55">
...
</syntaxhighlight>
linelinks
MediaWiki版本: | ≥ 1.36 Gerrit change 653142 |
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 (自从MediaWiki 1.42 • change 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.
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
highlight
属性将指定一行或多行将被标记(通过对指定的行显示不同的背景色)。
您可以指定多个行的数字通过逗号分隔(例如,highlight="1,4,8"
)或使用一个连字符和两个数字表示范围(例如,highlight="5-7"
)。
start
属性对行号重排。def quick_sort(arr):
less = []
pivot_list = []
more = []
if len(arr) <= 1:
return arr
上面为下方代码的输出结果:
<syntaxhighlight lang="python" line start="3" highlight="1,5-7">
...
</syntaxhighlight>
inline
MediaWiki版本: | ≥ 1.26 |
该属性表示源代码将作为段落的一部分内联(而不是自己的块)。 (在版本1.26引入)
class="nowrap"
(在那些受支持的维基;见下方)或style="white-space:nowrap"
则是不换行的。例如:
以下lambda x: x * 2
是在Python的Lambda表达式。
是下方的结果:
以下<syntaxhighlight lang="python" inline>lambda x: x * 2</syntaxhighlight>是在Python的[[w:zh:匿名函数|Lambda表达式]]。
class
当使用了inline
时,指定class="nowrap"
(在那些受到支持的维基上,而不是MediaWiki本身)不应在代码块内的空格处出现换行符。
例如:
没有class="nowrap"
:
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxlambda x: x * 2
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
使用style="white-space:nowrap"
:
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxlambda x: x * 2
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
style
style
属性允许CSS属性被直接包含。
这相当于将块封装在<div>
标签(不是<span>
标签)中。
tab‑size
属性不能够通过这种方式指定。它需要<span>
标签封装,像下方高级段落中描述的样子。
例如:
def quick_sort(arr):
less = []
pivot_list = []
more = []
if len(arr) <= 1:
return arr
else:
pass
是下方的结果:
<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版本: | ≥ 1.43 |
The copy
attribute adds a button link that copies the content to the clipboard when clicked.
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>
支持的语言
<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.
截至2020年1月,Pygments支持的完整语言列表为:
编程语言
- ActionScript
- Ada
- Agda(包括literate)
- Alloy
- AMPL
- ANTLR
- APL
- AppleScript
- 组装(各种)
- Asymptote
- Augeas
- AutoIt
- Awk
- BBC Basic
- Befunge
- BlitzBasic
- Boa
- Boo
- Boogie
- BrainFuck
- C,C ++(包括其变体,例如Arduino)
- C#
- Chapel
- Charm++ CI
- Cirru
- Clay
- Clean
- Clojure
- CoffeeScript
- ColdFusion
- Common Lisp
- Component Pascal
- Coq
- Croc (MiniD)
- Cryptol(包括Literate Cryptol)
- Crystal
- Cypher
- Cython
- D
- Dart
- DCPU-16
- Delphi
- Dylan(包括控制台)
- Eiffel
- Elm
- Emacs Lisp
- Erlang(包括shell session)
- Ezhil
- Factor
- Fancy
- Fantom
- Fennel
- FloScript
- Forth
- Fortran
- FreeFEM++
- F#
- GAP
- Gherkin (Cucumber)
- GLSL shaders
- Golo
- Gosu
- Groovy
- Haskell(包括Literate Haskell)
- HLSL
- HSpec
- Hy
- IDL
- Idris(包括Literate Idris)
- Igor Pro
- Io
- Jags
- Java
- JavaScript
- Jasmin
- Jcl
- Julia
- Kotlin
- Lasso(包括模板)
- Limbo
- LiveScript
- Logtalk
- Logos
- Lua
- Mathematica
- Matlab
- Modelica
- Modula-2
- Monkey
- Monte
- MoonScript
- Mosel
- MuPad
- NASM
- Nemerle
- NesC
- NewLISP
- Nimrod
- Nit
- Notmuch
- NuSMV
- Objective-C
- Objective-J
- Octave
- OCaml
- Opa
- OpenCOBOL
- ParaSail
- Pawn
- PHP
- Perl 5
- Pike
- Pony
- PovRay
- PostScript
- PowerShell
- Praat
- Prolog
- Python(包括控制台会话和回溯)
- QBasic
- Racket
- Raku a.k.a. Perl 6
- REBOL
- Red
- Redcode
- Rexx
- Ride
- Ruby(包括irb会话)
- Rust
- S, S-Plus, R
- Scala
- Scdoc
- Scheme
- Scilab
- SGF
- Shell脚本(Bash、Tcsh、Fish)
- Shen
- Silver
- Slash
- Slurm
- Smalltalk
- SNOBOL
- Snowball
- Solidity
- SourcePawn
- Stan
- Standard ML
- Stata
- Swift
- Swig
- SuperCollider
- Tcl
- Tera Term language
- TypeScript
- TypoScript
- USD
- Unicon
- Urbiscript
- Vala
- VBScript
- Verilog, SystemVerilog
- VHDL
- Visual Basic.NET
- Visual FoxPro
- Whiley
- Xtend
- XQuery
- Zeek
- Zephir
- Zig
模板语言
- Angular模板
- Cheetah模板
- ColdFusion
- Django / Jinja模板
- ERB(Ruby模板)
- Evoque
- Genshi(Trac模板语言)
- Handlebars
- JSP (Java Server Pages)
- Liquid
- Myghty(基于HTML::Mason的框架)
- Mako(Myghty的接替版本)
- Slim
- Smarty模板(PHP模板引擎)
- Tea
- Twig
其他标记
- Apache配置文件
- Apache Pig
- BBCode
- CapDL
- Cap'n Proto
- CMake
- Csound代码
- CSS
- Debian控制文件
- 文件差异
- Dockerfiles
- DTD
- EBNF
- 电子邮件标头
- Extempore
- Flatline
- Gettext catalogs
- Gnuplot脚本
- Groff命令
- Hexdumps
- HTML
- HTTP会话
- IDL
- Inform
- INI风格的配置文件
- IRC日志(irssi风格)
- Isabelle
- JSGF记号
- JSON, JSON-LD
- Lean定理证明器
- Lighttpd配置文件
- Linux内核日志(dmesg)
- LLVM汇编
- LSL脚本
- Makefiles
- MoinMoin/Trac Wiki标记
- MQL
- MySQL
- NCAR语言
- Nginx配置文件
- Nix语言
- NSIS脚本
- Notmuch
- POV-Ray scenes
- Puppet
- QML
- Ragel
- Redcode
- ReST
- Roboconf
- Robot Framework
- RPM spec files
- Rql
- RSL
- Scdoc
- SPARQL
- SQL,以及MySQL、SQLite
- Squid configuration
- TADS 3
- Terraform
- TeX
- Thrift
- TOML
- Treetop grammars
- USD (Universal Scene Description)
- Varnish configs
- VGL
- Vim Script
- WDiff
- Windows批处理文件
- XML
- XSLT
- YAML
- Windows注册表文件
- ansys
- apdl
- asc
- gcode
- golang === go
- gsql
- jslt
- julia-repl
- kuin
- meson
- nestedtext
- nodejsrepl
- nt
- omg-idl
- output
- pem
- procfile
- pwsh
- smithy
- teal
- thingsdb
- ti
- wast
- wat
GeSHi之前支持的词法分析器
以下是GeSHi可以高亮显示的部分语言清单,在切换到Pygments之后不再支持语言的被用横线删去。
GeSHi之前支持的词法分析器 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
python3
)。这是与MediaWiki 1.30捆绑在一起的版本的更改,后者使用的是Python2(python
)。请注意,python3
二进制文件必须安装在PHP解释器的执行PATH中。- 下载,并将解压后的
SyntaxHighlight_GeSHi
移动到extensions/
目录中。
开发者和代码贡献人员应从Git安装扩展,输入:cd extensions/ git clone https://gerrit.wikimedia.org/r/mediawiki/extensions/SyntaxHighlight_GeSHi
- 只有是從Git安裝的才需Composer来安装PHP,发行
composer install --no-dev
至的目录。 (参见T173141了解潜在问题。)Category:Extensions requiring Composer with git/zh - 将下列放置在您的LocalSettings.php 的底部:
wfLoadExtension( 'SyntaxHighlight_GeSHi' );
- 在Linux中,请为pygmentize二进制文件设置可执行权限。 您可以使用FTP客户端或以下shell命令执行此操作:
chmod a+x /path/to/extensions/SyntaxHighlight_GeSHi/pygments/pygmentize
完成 – 在您的wiki上至Special:Version,以验证已成功安装。
- 如果使用Vagrant ,请
vagrant roles enable syntaxhighlight --provision
安装
所以,在从Git安装之后,切换到包含该扩展的目录。例如:
"../extensions/SyntaxHighlight_GeSHi/"
并运行composer install --no-dev
,或在更新时:composer update --no-dev
。或者最好将
"extensions/SyntaxHighlight_GeSHi/composer.json"
添加到在Wiki根目录下的"composer.local.json"文件中,例如:
{
"extra": {
"merge-plugin": {
"include": [
"extensions/SyntaxHighlight_GeSHi/composer.json"
]
}
}
}
composer update --no-dev
。 Voilà!
$wgSyntaxHighlightMaxLines , $wgSyntaxHighlightMaxBytes |
Optional | For performance reasons, blobs or pages (JS, Lua and CSS pages) larger than these values will not be highlighted. (在版本1.40引入) |
- Linux
$wgPygmentizePath |
Optional | $setting(可选):Pygments包中pygmentize的绝对路径。 默认情况下,扩展中附带了Pygments包,$wgPygmentizePath 指向默认了Pygments包,但如果需要,您可以指向不同的版本。 例如:$wgPygmentizePath = "/usr/local/bin/pygmentize"; 。 |
- Windows
- 若您在一台Windows主机上运行MediaWiki,您需要设置Pygmentize.exe的位置
$wgPygmentizePath = "c:\\Python27\\Scripts\\pygmentize.exe";
。- 若没有
pygmentize.exe
,从命令行运行在Scripts
目录中的easy_install Pygments
来生成文件。
- 若没有
若您在使用附带的pygmentize二进制文件(extensions/SyntaxHighlight_GeSHi/pygments/pygmentize),确保您的网络服务器允许执行它。
若您的主机商不允许添加可执行文件到站点目录,安装python-pygments并添加$wgPygmentizePath = pygmentize
到LocalSettings.php
中。
- 问题排除
找升级到MediaWiki 1.26 版本及其以后,一些用户开始报告与这个扩展的问题。 有些情况下,当某些语言(如Lua)可能无法突出显示并打开调试中时,MediaWiki会抛出错误Notice: Failed to invoke Pygments: /usr/bin/env: python3: No such file or directory
。
- 尝试在
LocalSettings.php
指定$wgPygmentizePath
为外部pygmentize二进制文件。
- In shared hosting environments with cPanel, this can be done by setting up a new Python application through the "Setup Python App" menu, and activating the virtual environment for the app through SSH (
source /virtualenv/python/3.5/bin/activate
). After this, the Pygments module can be added to the Python app, for which navigate to the virtual environment path (cd virtualenv/python/3.5/bin/
), download and install Pygments (./pip install Pygments
) and then activate the module by adding "Pygments" under the "Existing applications" section of the "Setup Python App" menu. This will create the required file at path:virtualenv/python/3.5/bin/pygmentize
- In shared hosting environments with cPanel, this can be done by setting up a new Python application through the "Setup Python App" menu, and activating the virtual environment for the app through SSH (
- 获取更多建议和信息,请参见phab:T128993。
- SELinux can also prevent the extension from running with an error similar to
type=AVC msg=audit(1609598265.169:225924): avc: denied { execute } for pid=2360888 comm="bash" name="pygmentize" dev="dm-0" ino=50814399 scontext=system_u:system_r:httpd_t:s0 tcontext=unconfined_u:object_r:httpd_user_content_t:s0 tclass=file permissive=0
in your audit.log. This can be allowed withsetsebool -P httpd_unified 1
- In earlier versions of this extension, Windows would sometimes fail with an _Py_HashRandomization_Init error. This was a bug with the Windows environment not being passed to python executions. A fix was released in 1.40, with backports to 1.38 and 1.39.
可视化编辑器集成
该插件可以使用可视化编辑器直接编辑。
一个对话框会被打开,当一个用户想要编辑syntaxhighlight
段落。
要使其工作,可视化编辑器必须被安装且从最新git版本被配置,与Parsoid相同。
该功能在较老的Parsoid版本中随机地无法使用。
参见Extension:SyntaxHighlight/VisualEditor 获取更多细节
Using with MediaWiki-Docker
See MediaWiki-Docker/Extension/SyntaxHighlight for instructions on configuring a MediaWiki-Docker environment for SyntaxHighlight usage, development, and testing.
- 与
<pre>
和<code>
标记不同,HTML代码实体(例如
)不需要(也不应该)将&
字符转义为&
。. - 与
<pre>
标签一样,但与<code>
标签不同,范围内的标签(除了自己的结束标签)不需要将<
符号转义为<
,也不需要使用<nowiki>
标签转义wikitext。
- 此外,虽然
<pre>
假定制表符每8个字符停止并在复制渲染文本时使用实际空格渲染制表符,<syntaxhighlight>
使用4空格制表位(Internet Explorer使用8除外)并保留渲染文本中的制表符,后者可以使用封闭的$span标记(不是$div,而不是使用自己的$style属性)进行更改。- Tab stop length for
<syntaxhighlight>
may be adjusted using a<span style="">
tag nested inside it that specifies thetab-size
(its own style attribute cannot be used to specify the tab stops, and<div>
cannot be used instead):<span style="-moz-tab-size: nn; -o-tab-size: nn; tab-size: nn;">
- Firefox 4.0到90需要
-moz-
前缀,Opera需要-o-
前缀(从10.60到15)。[3]
- Tab stop length for
(请注意,wiki编辑框采用8空格缩进。 这仅适用于实际保存的页面,通过编辑框或Special:ExpandTemplates生成的预览可能会有所不同。)
- Instructions on updating the pygments version used by SyntaxHighlight
- Pygments – Python syntax highlighter
- 基于此的扩展:
- Extension:SyntaxHighlightPages – 基于标题后缀的高亮。
- 可选扩展:
- Extension:Highlightjs Integration – 使用HighlightJS库的语法高亮(包括Maxima等一些Pygments不支持的语言)。
- Extension:GoogleCodePrettify – 使用谷歌代码美化库的语法高亮扩展。
- Extension:ExternalContent – embed external content, such as code from GitHub, GitLab, and Bitkeeper repos with automatic rendering of Markdown files; and using syntax highlighting via the Prism.js library for nearly 300 supported languages. Content indexed by CirrusSearch
脚注
![]() | 此用于一个或多个维基媒体项目。 这可能意味着足够稳定、运作足够良好,可以用在这样的高流量的网站上。 请在维基媒体的CommonSettings.php和InitialiseSettings.php中查找此的名称以查看哪些网站安装了该。 特定wiki上的已安装的的完整列表位于Special:Version页面。 |