Extension:SyntaxHighlight
![]() リリースの状態: 安定 |
|
---|---|
![]() |
|
実装 | タグ |
説明 | ウィキページ内でソースコードの構文を強調できるようにする |
作者 |
|
最新バージョン | 継続的な更新 |
互換性の方針 | master は後方互換性を維持しています。 |
MediaWiki | 1.25+ |
データベースの変更 | いいえ |
Composer | mediawiki/syntax-highlight |
ライセンス | GNU 一般公衆利用許諾書 2.0 以降 |
ダウンロード | README |
|
|
<syntaxhighlight> |
|
Public wikis using | 11,760 (Ranked 8th) |
SyntaxHighlight 拡張機能の翻訳にご協力ください | |
問題点 | 未解決のタスク · バグを報告 |
これまではSyntaxHighlight_GeSHiとして知られていたSyntaxHighlight(構文強調表示)拡張機能は、<syntaxhighlight>
タグを使用してソースコードに豊富なフォーマットを提供しています。
Pygmentsライブラリを使用しており、数百の異なるプログラミング言語とファイル形式をサポートしています。
<pre>
タグと<poem >
タグのように、テキストは入力のとおりに正確にレンダリングされ、空白は保持されます。
The SyntaxHighlight extension does not work on hardened wiki installations due to lack of proc_open
, shell_exec
and friends.
See タスク T250763.
The <syntaxhighlight>
tag has become expensive since 1.39.
(See タスク T316858.)
使用法
インストール後はwikiページで 「syntaxhighlight」タグを使用できます。例えば次の表示の場合、
def quick_sort(arr):
less = []
pivot_list = []
more = []
if len(arr) <= 1:
return arr
else:
pass
上記は、以下のウィキ文マークアップの結果を表示しています。
def quick_sort(arr):
less = []
pivot_list = []
more = []
if len(arr) <= 1:
return arr
else:
pass
旧バージョン(MediaWiki 1.16以前)では、拡張機能は<source>
というタグを使用していました。
これは引き続きサポートされていますが、非推奨です。代わりに、<syntaxhighlight>
を使うべきです。
スタイル
表示されたコードが大きすぎる場合は、wikiのMediaWiki:Common.cssページに次のコードを入れて調整することができます(存在しない場合は作成してください):
/* ここに配置されたCSSはすべての外装に適用されます */
.mw-highlight pre {
font-size: 90%;
}
コードブロックを境界線で囲むには、上のセクションでborder: 1px dashed blue;
のような行を挿入します。
上記のセクションにfont-family: "Courier New", monospace;
のような行を挿入することによって、使用される「フォントファミリ」の制御も行えます。
Syntax 強調のエラーのカテゴリ
この拡張機能は、<syntaxhighlight>
タグのlang
属性が悪いページを追跡カテゴリに追加します。
The message key
メッセージキーのMediaWiki:syntaxhighlight-error-categoryによりカテゴリ名が決まります。このウィキではCategory:Pages with syntax highlighting errorsになります。
このカテゴリに誤って分類されるページは、<syntaxhighlight>
タグにlang
属性がない場合が多いです。エラーの原因は古いバージョンのこの拡張機能が$wgSyntaxHighlightDefaultLang
の定義をサポートしていたからです。
修正するには属性を<pre>
またはlang="bash"
に変更するか、タグにlang="text"
を加えます。
カテゴリはハイライトの対象が1000行以上だった場合や100キロバイト以上だった場合にも追加されます。[1]
パラメーター
lang
lang="name"
属性により、対応するレクサー(語彙分析)が決まります。
拡張機能がソースコードを強調表示する方式は言語に影響を受けます。
サポートする言語の詳細は「言語のサポート」の節を参照してください。
def quick_sort(arr):
less = []
<syntaxhighlight lang="python">
...
</syntaxhighlight>
無効あるいは未知の名前を指定すると、そのページをトラックするカテゴリが付与されます。 このページの「#Syntax 強調のエラーのカテゴリ」節で詳細を説明します。
line
line
属性で行番号が有効になります。
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 55.
def quick_sort(arr):
less = []
<syntaxhighlight lang="python" line start="55">
...
</syntaxhighlight>
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"
).
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 バージョン: | ≧ 1.26 |
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.
class="nowrap"
(on those wikis that support it; see below) or style="white-space:nowrap"
.例:
次のlambda x: x * 2
は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.
例:
class="nowrap"
なし:
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxlambda x: x * 2
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
style="white-space:nowrap"
あり:
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxlambda x: x * 2
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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.
例えば下記を表示するには:
def quick_sort(arr):
less = []
pivot_list = []
more = []
if len(arr) <= 1:
return arr
else:
pass
次の記述を使います:
def quick_sort(arr):
less = []
pivot_list = []
more = []
if len(arr) <= 1:
return arr
else:
pass
言語のサポート
Pygments は、ライブラリに含まれるさまざまな「レキサー」(lexers) を通じて、何百ものコンピューター言語とファイル形式の構文強調のサポートを提供します。
In most cases, the lang=
attribute to be used with this extension is the lower-case version of the name of the language.
However, many have aliases, or "short names" as they're called in the Pygments documentation; see "Available lexers" for full details.
Some languages previously supported by GeSHi have been mapped to Pygments lexers; see SyntaxHighlightGeSHiCompat.php
for details.
As of January 2020, the full list of languages supported by Pygments is:
プログラミング言語
- ActionScript
- Ada
- Agda (literate を含む)
- Alloy
- AMPL
- ANTLR
- APL
- AppleScript
- Assembly (various)
- 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 (incl. Literate Cryptol)
- Crystal
- Cypher
- Cython
- D
- Dart
- DCPU-16
- Delphi
- Dylan (incl. console)
- Eiffel
- Elm
- Emacs Lisp
- Erlang (シェル セッションを含む)
- 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 (incl. Literate Idris)
- Igor Pro
- Io
- Jags
- Java
- JavaScript
- Jasmin
- Jcl
- Julia
- Kotlin
- Lasso (incl. templating)
- 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 (incl. console sessions and tracebacks)
- QBasic
- Racket
- Raku a.k.a. Perl 6
- REBOL
- Red
- Redcode
- Rexx
- Ride
- Ruby (incl. irb sessions)
- Rust
- S, S-Plus, R
- Scala
- Scdoc
- Scheme
- Scilab
- SGF
- Shell scripts (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 templates
- ColdFusion
- Django / Jinja テンプレート
- ERB (Ruby templating)
- Evoque
- Genshi (the Trac template language)
- Handlebars
- JSP (Java Server Pages)
- Liquid
- Myghty (the HTML::Mason based framework)
- Mako (the Myghty successor)
- Slim
- Smarty templates (PHP templating)
- Tea
- Twig
その他のマークアップ
- Apache 設定ファイル
- Apache Pig
- BBCode
- CapDL
- Cap'n Proto
- CMake
- Csound 楽譜
- CSS
- Debian control files
- 差分ファイル
- Dockerfiles
- DTD
- EBNF
- メール ヘッダー
- Extempore
- Flatline
- Gettext カタログ
- Gnuplot スクリプト
- Groff マークアップ
- 16進ダンプ
- HTML
- HTTP セッション
- IDL
- Inform
- INI-style config files
- IRC logs (irssi style)
- Isabelle
- JSGF notation
- JSON, JSON-LD
- リーンの定理の格言
- Lighttpd config files
- Linux kernel log (dmesg)
- LLVM assembly
- LSL スクリプト
- Makefiles
- MoinMoin/Trac Wiki markup
- MQL
- MySQL
- NCAR command language
- Nginx config files
- Nix 言語
- NSIS スクリプト
- Notmuch
- POV-Ray scenes
- Puppet
- QML
- Ragel
- Redcode
- ReST
- Roboconf
- Robot Framework
- RPM spec files
- Rql
- RSL
- Scdoc
- SPARQL
- SQL, also 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
Lexers previously supported by GeSHi
GeSHi で強調表示できるコンピュータ言語の一部を紹介します。取り消し線のある項目はPygmentshrの移行により非推奨になっています。
Lexers previously supported by GeSHi | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
インストール
python3
)が必要です。同バージョン2(python
)を使ったMediaWiki 1.30同梱版からの変更です。python3
バイナリファイルのインストール先は必ずPHPインタープリターの実行PATHにする点に留意してください。- ダウンロードして、ファイルを
extensions/
フォルダー内のSyntaxHighlight_GeSHi
という名前のディレクトリ内に配置します。 - Gitでのインストールの場合のみ、PHPの依存関係をインストールするためComposerを実行します。 (合併症の可能性についてはタスク T173141を参照。)
- 以下のコードを
LocalSettings.php
の末尾に追加します:wfLoadExtension( 'SyntaxHighlight_GeSHi' );
- Linuxではpygmentize バイナリに次の実行パーミッションを設定します。 You can use an FTP client or the following shell command to do so:
chmod a+x /path/to/extensions/SyntaxHighlight_GeSHi/pygments/pygmentize
- 完了 – ウィキの「Special:Version」に移動して、拡張機能が正しくインストールされたことを確認します。
Vagrant installation:
- Vagrant を使用している場合は、
vagrant roles enable syntaxhighlight --provision
でインストールしてください。
これを受け、Gitからインストール後は拡張機能を保存したディレクトリ、例えば
"../extensions/SyntaxHighlight_GeSHi/"
に移動し、インストールにはcomposer install --no-dev
、また更新するにはcomposer update --no-dev
を実行します。お勧めの代替の手順として、ご利用のwikiのルートディレクトリの「composer.local.json」内に、下記の例のような
"extensions/SyntaxHighlight_GeSHi/composer.json"
という行を追加します。
{
"extra": {
"merge-plugin": {
"include": [
"extensions/SyntaxHighlight_GeSHi/composer.json"
]
}
}
}
composer update --no-dev
を実行します。 Voilà!
設定
$wgSyntaxHighlightMaxLines
and $wgSyntaxHighlightMaxBytes
(optional): For performance reasons, blobs or pages (JS, Lua and CSS pages) larger than these values will not be highlighted. (since 1.40)
- Linux
$wgPygmentizePath
(オプション):Pygmentsパッケージを「ピグメント化する」(pygmentize)絶対パス。 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. 例:$wgPygmentizePath = "/usr/local/bin/pygmentize";
。
- Windows
- MediaWiki を Windows 機器でホスティングするには、Pygmentize.exe のパスは
$wgPygmentizePath = "c:\\Python27\\Scripts\\pygmentize.exe";
に設定しますpygmentize.exe
がない場合、Scripts
フォルダでコマンドラインを開きeasy_install Pygments
を実行して、ファイルを作成。
同梱版のpygment バイナリファイル(extensions/SyntaxHighlight_GeSHi/pygments/pygmentize)を利用する場合は、ご利用のウェブサイトでバイナリの実行を許可しているかどうか確認してください。
ホストがウェブのディレクトリに実行可能ファイル追加を許可していない場合には、LocalSettings.phpにpython-pygments をインストールして$wgPygmentizePath = pygmentize
を追加します。
- トラブルシューティング
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
.
- Try pointing
$wgPygmentizePath
in LocalSettings.php towards an external pygmentize binary.
- 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 (
- See phab:T128993 on this for further suggestions and information.
- 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.
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.
詳細は Extension:SyntaxHighlight/VisualEditor を参照してください
高度な設定
Unlike the <pre>
and <code>
tags, HTML character entities such as
need not (and should not) have the &
character escaped as &
.
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 <
, 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).[2]
(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.
関連項目
- Extensions dependent on this one:
- Extension:SyntaxHighlightPages — highlights pages based on title suffixes.
- Alternative extensions:
- Extension:Highlightjs Integration — syntax highlighter that uses HighlightJS library (includes support for some languages that are missing from Pygments, such as Maxima).
- Extension:GoogleCodePrettify — syntax highlighter that uses Google Code Prettify library.
脚注
この拡張機能は 1 つ以上のウィキメディアのプロジェクトで使用されています。 これはおそらく、この拡張機能が安定していて高いトラフィックのウェブサイトでも十分に動作することを意味します。 この拡張機能がインストールされている場所を確認するには、ウィキメディアの設定ファイル CommonSettings.php および InitialiseSettings.php 内で、この拡張機能の名前を探してください。 特定のウィキにインストールされている拡張機能の完全な一覧は、そのウィキの Special:Version ページにあります。 |
この拡張機能は以下のパッケージ/ウィキファームに含まれています: This is not an authoritative list. Some wiki farms/hosts may contain this extension even if they are not listed here. Always check with your wiki farms/hosts or bundle to confirm. |