Extension:SyntaxHighlight
SyntaxHighlight リリースの状態: 安定 |
|||
---|---|---|---|
![]() |
|||
実装 | Tag | ||
説明 | ウィキページ内でソースコードの構文を強調できるようにする | ||
作者 |
|
||
最新バージョン | continuous updates | ||
MediaWiki | 1.25+ | ||
データベースの変更 | いいえ | ||
ライセンス | GNU General Public License 2.0 or later | ||
ダウンロード | README |
||
|
|||
|
|||
|
|||
translatewiki.net で翻訳を利用できる場合は、SyntaxHighlight 拡張機能の翻訳にご協力ください | |||
使用状況とバージョン マトリクスを確認してください。 | |||
問題点 | 未解決のタスク · バグを報告 |
これまではSyntaxHighlight_GeSHiとして知られていたSyntaxHighlight(構文強調表示)拡張機能は、<syntaxhighlight>
タグを使用してソースコードに豊富なフォーマットを提供しています。
Pygmentsライブラリを使用しており、数百の異なるプログラミング言語とファイル形式をサポートしています。
<pre>
タグと<poem >
タグのように、テキストは入力のとおりに正確にレンダリングされ、空白は保持されます。
Contents
使用法
インストール後はwikiページで 「syntaxhighlight」タグを使用できます。例えば次の表示の場合。
1 def quickSort(arr):
2 less = []
3 pivotList = []
4 more = []
5 if len(arr) <= 1:
6 return arr
7 else:
8 pass
上記は、以下のウィキ文マークアップの結果を表示しています。
<syntaxhighlight lang="Python" line='line'>
def quickSort(arr):
less = []
pivotList = []
more = []
if len(arr) <= 1:
return arr
else:
pass
</syntaxhighlight>
旧バージョン(MediaWiki 1.16以前)では、拡張機能は<source>
というタグを使用していました。
これは引き続きサポートされていますが、ソースコード自体に<source>
タグ(XMLなど)が含まれている場合、<syntaxhighlight>
を使ったほうが競合を避けやすくなります。
スタイル
表示されたコードが大きすぎる場合は、wikiのMediaWiki:Common.cssページに次のコードを入れて調整することができます(存在しない場合は作成してください):
/* ここに配置されたCSSはすべてのスキンに適用されます */
.mw-highlight pre {
font-size: 90%;
}
コードブロックを境界線で囲むには、上のセクションでborder: 1px dashed blue;
のような行を挿入します。
上記のセクションにfont-family: "Courier New", monospace;
のような行を挿入することによって、使用される「フォントファミリ」の制御も行えます。
Syntax 強調のエラーのカテゴリ
この拡張機能は、<source>
または<syntaxhighlight>
タグのlang
属性が悪いページを追跡カテゴリに追加します。
The message key
メッセージキーのMediaWiki:syntaxhighlight-error-categoryによりカテゴリ名が決まります。このウィキではCategory:Pages with syntax highlighting errorsになります。
このカテゴリに誤って分類されるページは、しばしばタグが<syntaxhighlight>
あるいは<source>
にされlang
属性がない状態で、エラーの原因はこの拡張機能の旧版の定義が「$wgSyntaxHighlightDefaultLang」をサポートしていたからです。
修正するには属性を<pre>
またはlang="bash"
に変更するか、タグにlang="text"
を加えます。
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.[1]
パラメーター
言語
lang="Name"
属性により、対応するレクサー(語彙分析)が決まります。
拡張機能がソースコードを強調表示する方式は言語に影響を受けます。
pygmentsパーサは大文字小文字を認識し、どの言語も最低1文字は大文字を含みます(小文字のみの言語名はgtags用に割り当てられます。)
サポートする言語の詳細は「言語のサポート」の節を参照してください。
def quickSort(arr):
less = []
<syntaxhighlight lang="Python"> .. </syntaxhighlight>
無効あるいは未知の名前を指定すると、そのページをトラックするカテゴリが付与されます。 このページの「#Syntax 強調のエラーのカテゴリ」節で詳細を説明します。
line
line
属性で行番号が有効になります。
1 def quickSort(arr):
2 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.
55 def quickSort(arr):
56 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"
).
Note that the line number specification ignores any renumbering of the displayed line numbers with the start
attribute.
3 def quickSort(arr):
4 less = []
5 pivotList = []
6 more = []
7 if len(arr) <= 1:
8 return arr
is the result of
<syntaxhighlight lang="Python" highlight="1,5-7" start='3' line> .. </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.
For backwards-compatibility, an enclose="none"
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 class="nowrap"
(on those wikis that support it; see below) or style=white-space:nowrap
.
For example:
lambda x: x * 2
is a lambda expression in Python.
Is the result of:
<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 * 2
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
With 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 quickSort(arr):
less = []
pivotList = []
more = []
if len(arr) <= 1:
return arr
else:
pass
次の記述を使います:
<syntaxhighlight lang="Python" style="border:3px dashed blue">
def quickSort(arr):
less = []
pivotList = []
more = []
if len(arr) <= 1:
return arr
else:
pass
</syntaxhighlight>
言語のサポート
Pygmentsライブラリがサポートする言語やコンピュータ言語、ファイル形式は数百あります。 full list is:
Programming languages
- ActionScript
- Ada
- ANTLR
- 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 (Cucumber)
- 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
- REBOL
- Red
- Redcode
- Ruby
- Rust
- S, S-Plus and R
- Scala
- Scheme
- Scilab
- Smalltalk
- SNOBOL
- Tcl
- Vala
- Verilog
- VHDL
- Visual Basic.NET
- Visual FoxPro
- XQuery
- Zephir
Template languages
- Cheetah templates
- Django / Jinja templates
- ERB
- Genshi
- JSP
- Myghty
- Mako
- Smarty templates
- Tea
Other markup
- Apache config files
- Bash shell scripts
- BBCode
- CMake
- CSS
- Debian control files
- Diff files
- DTD
- Gettext catalogs
- Gnuplot script
- Groff markup
- HTML
- HTTP sessions
- INI-style config files
- IRC logs (irssi style)
- Lighttpd config files
- Makefiles
- MoinMoin/Trac Wiki markup
- MySQL
- Nginx config files
- POV-Ray scenes
- Ragel
- Redcode
- ReST
- Robot Framework
- RPM spec files
- SQL
- Squid configuration
- TeX
- tcsh
- Vim Script
- Windows batch files
- XML
- XSLT
- YAML
厳密な言語コードは詳細を網羅したPygments説明文書も提供。そのほか、GeSHiがサポートする言語名の一部について関数も提供しています(一覧表はこちら)。
GeSHi で強調表示できるコンピュータ言語の一部を紹介します。取り消し線のある項目はPygmentshrの移行により非推奨になっています。
非推奨になったレクサー | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
インストール
python3
)が必要です。同バージョン2(python
)を使ったMediaWiki 1.30同梱版からの変更です。python3
バイナリファイルのインストール先は必ずPHPインタープリターの実行PATHにする点に留意してください。- ダウンロードして、ファイルを
extensions/
フォルダー内のSyntaxHighlight_GeSHi
という名前のディレクトリ内に配置します。
- 以下のコードを LocalSettings.php の末尾に追加します:
wfLoadExtension( 'SyntaxHighlight_GeSHi' );
- Linuxではpygmentize バイナリに次の実行パーミッションを設定します。
chmod a+x /path/to/extensions/SyntaxHighlight_GeSHi/pygments/pygmentize
完了 – ウィキの「Special:Version」に移動して、拡張機能が正しくインストールされたことを確認します。
MediaWiki 1.24 以前を稼働させている利用者へ:
上記の手順では、wfLoadExtension()
を使用してこの拡張機能をインストールする新しい方法を記載しています。 この拡張機能をこれらの過去のバージョン (MediaWiki 1.24 以前) にインストールする必要がある場合は、wfLoadExtension( 'SyntaxHighlight_GeSHi' );
の代わりに以下を使用する必要があります:
require_once "$IP/extensions/SyntaxHighlight_GeSHi/SyntaxHighlight_GeSHi.php";
これを受け、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à!設定
- 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. For example:$wgPygmentizePath = "/usr/local/bin/pygmentize";
.$wgSyntaxHighlightModels
: 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
- 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
を追加します。
- 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
.
- 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 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 source
or syntaxhighlight
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.
See Extension:SyntaxHighlight/VisualEditor for details
高度な設定
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), 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.
関連項目
- Pygments – Python syntax highlighter
- 関連する拡張機能:
- Extension:GoogleCodePrettify – syntax highlighter that uses Google Code Prettify library.
- Extension:SyntaxHighlighter – syntax highlighter that uses SyntaxHighlighter library.
- Extension:ViewFiles – an extension, dependent on this one, to support highlighting text from files viewed via a special page.
脚注
![]() | この拡張機能は 1 つ以上のウィキメディアのプロジェクトで使用されています。 これはおそらく、この拡張機能が安定していて高いトラフィックのウェブサイトでも十分に動作することを意味します。 この拡張機能がインストールされている場所を確認するには、ウィキメディアの設定ファイル CommonSettings.php および InitialiseSettings.php 内で、この拡張機能の名前を探してください。 特定のウィキにインストールされている拡張機能の完全な一覧は、そのウィキの Special:Version ページにあります。 |
- Pages using duplicate arguments in template calls
- Extensions bundled with MediaWiki 1.21/ja
- GPL licensed extensions/ja
- Stable extensions/ja
- Tag extensions/ja
- Extensions in Wikimedia version control/ja
- ContentGetParserOutput extensions/ja
- ParserFirstCallInit extensions/ja
- ApiFormatHighlight extensions/ja
- ResourceLoaderRegisterModules extensions/ja
- All extensions/ja
- Extensions used on Wikimedia/ja
- Syntax highlighting extensions/ja
- GeSHi extensions/ja
- Extensions with VisualEditor support/ja
- RejectParserCacheValue extensions/ja