Template:SyntaxHighlight

From Wikitia
Jump to navigation Jump to search

<languages/>

This extension comes with MediaWiki 1.21 and above. Thus you do not have to download it again. However, you still need to follow the other instructions provided.

[[Category:extensions bundled with MediaWiki 1.21{{#translation:}}]]

CodeMirror>CodeMirror </> extension or the userscripts of [[<tvar|user1>User:Remember the dot/Syntax highlighter</>|Remember the dot]] and [[<tvar|user2>:en:User:Cacycle/wikEd</>|Cacycle]].</translate>
MediaWiki extensions manual
OOjs UI icon advanced.svg
SyntaxHighlight
Release status: stableLua error: callParserFunction: function "#translation" was not found.[[Category:stable extensions{{#translation:}}]]
SyntaxHighlighting with Pygments.png
Implementation Lua error: callParserFunction: function "#translation" was not found.
Description <translate> Allows source code to be syntax highlighted on the wiki pages</translate>
Author(s)
  • Brion Vibber,
  • Tim Starling,
  • Rob Church,
  • Ori Livneh
Latest version Lua error: callParserFunction: function "#translation" was not found.
MediaWiki 1.25+
Database changes No
License Lua error: callParserFunction: function "#translation" was not found.
Download [[Category:Extensions in Wikimedia version control{{#translation:}}]]
README

  • $wgPygmentizePath
  • $wgSyntaxHighlightModels
<syntaxhighlight>
Translate the SyntaxHighlight extension if it is available at translatewiki.net
Issues Open tasks · Report a bug

[[Category:All extensions{{#translation:}}]]

<translate> The SyntaxHighlight extension, formerly known as SyntaxHighlight_GeSHi, provides rich formatting of source code using the <tvar|syntaxhighlight><syntaxhighlight></> tag.</translate> <translate> It is powered by the [<tvar|url>http://pygments.org</> Pygments] library and supports hundreds of different programming languages and file formats.

Like the <tvar|pre><pre></> and <tvar|poem><poem ></> tags, the text is rendered exactly as it was typed, preserving any white space.

Usage

Once installed, you can use "syntaxhighlight" tags on wiki pages. For example, </translate>

def quickSort(arr):
	less = []
	pivotList = []
	more = []
	if len(arr) <= 1:
		return arr
	else:
		pass

<translate> is the result of the following wikitext markup: </translate>

<syntaxhighlight lang="Python" line='line'>
def quickSort(arr):
	less = []
	pivotList = []
	more = []
	if len(arr) <= 1:
		return arr
	else:
		pass
</syntaxhighlight>

<translate> In older versions (before MediaWiki 1.16), the extension used the tag <tvar|source><source></>.</translate> <translate> This is still supported, but <tvar|syntaxhighlight><syntaxhighlight></> may help avoid conflicts if your source code itself contains <tvar|source><source></> tags (for example XML).

Styling

If the displayed code is too big, you can adjust it by putting the following into the <tvar|css>MediaWiki:Common.css</> page in your wiki (create it if it does not exist): </translate>

/* <translate><!--T:106--> CSS placed here will be applied to all skins</translate> */
.mw-highlight pre {
	font-size: 90%;
}

<translate> Encasing code blocks in borders can be done by inserting a line like <tvar|css>border: 1px dashed blue;</> in the section above.</translate> <translate> Control over font family used can also be exercised by inserting a line like <tvar|css>font-family: "Courier New", monospace;</> into the section above.

Syntax highlighting error category

The extension adds pages that have a bad <tvar|lang>lang</> attribute in a <tvar|source><source></> or <tvar|syntaxhighlight><syntaxhighlight></> tag to a [[<tvar|link>Special:MyLanguage/Help:Tracking categories</>|tracking category]].</translate> <translate> The message key <tvar|msg>MediaWiki:syntaxhighlight-error-category</> determines the category name; on this wiki it is <tvar|category>Category:Pages with syntax highlighting errors</>.

The most common error that leads to pages being tagged with this category is a <tvar|syntaxhighlight><syntaxhighlight></> or <tvar|source><source></> tag with no <tvar|lang>lang</> attribute at all, because older versions of this extension supported the definition of "$wgSyntaxHighlightDefaultLang".</translate> <translate> These can typically either be replaced with <tvar|pre><pre></>, or <tvar|bash>lang="bash"</> or <tvar|text>lang="text"</> can be added to the tag.

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.<tvar|1>[1]</>

Parameters

lang

The <tvar|lang>lang="name"</> attribute defines what lexer should be used.</translate> <translate> The language affects how the extension highlights the source code.</translate> <translate> See the section [[<tvar|section>#Supported_languages</>|Supported languages]] for details of supported languages. </translate>

def quickSort(arr):
    less = []
<syntaxhighlight lang="Python">
..
</syntaxhighlight>

<translate> Specifying an invalid or unknown name will tag the page with a tracking category.</translate> <translate> See the section [[<tvar|section>#Syntax highlighting error category</>|Syntax highlighting error category]] in this page for details.

line

The <tvar|line>line</> attribute enables line numbers. </translate>

def quickSort(arr):
	less = []
<syntaxhighlight lang="Python" line>
..
</syntaxhighlight>

<translate>

start

The <tvar|start>start</> attribute (in combination with <tvar|line>line</>) defines the first line number of the code block.</translate> <translate> For example, <tvar|start>line start="55"</> will make line numbering start at 55. </translate>

def quickSort(arr):
    less = []
<syntaxhighlight lang="Python" line start="55">
..
</syntaxhighlight>

<translate>

highlight

The <tvar|highlight>highlight</> attribute specifies one or more lines that should be marked (by highlighting those lines with a different background color).</translate> <translate> You can specify multiple line numbers separated by commas (for example, <tvar|highlight>highlight="1,4,8"</>) or ranges using two line numbers and a hyphen (for example, <tvar|highlight2>highlight="5-7"</>).</translate> <translate> Note that the line number specification ignores any renumbering of the displayed line numbers with the <tvar|start>start</> attribute. </translate>

def quickSort(arr):
    less = []
    pivotList = []
    more = []
    if len(arr) <= 1:
        return arr

<translate> is the result of </translate>

<syntaxhighlight lang="Python" highlight="1,5-7" start='3' line>
..
</syntaxhighlight>

<translate>

inline

</translate>

MediaWiki version: 1.26

<translate> The attribute indicates that the source code should be inline as part of a paragraph (as opposed to being its own block).</translate> <translate> This option is available starting with MediaWiki 1.26.</translate> <translate> For backwards-compatibility, an <tvar|enclose>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 <tvar|class>class="nowrap"</> (on those wikis that support it; see below) or <tvar|style>style=white-space:nowrap</>.

For example:

<tvar|code>lambda x: x * 2</> is a lambda expression in Python.

Is the result of: </translate>

<translate><!--T:50--> <tvar|code><syntaxhighlight lang="Python" inline>lambda x: x * 2</syntaxhighlight></> is a [[w:Lambda (programming)|lambda expression]] in Python.</translate>

<translate>

class

When <tvar|inline>inline</> is used, <tvar|nowrap>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 <tvar|nowrap>class="nowrap"</>: </translate>

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxlambda x: x * 2xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

With style="white-space:nowrap":

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxlambda x: x * 2xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

<translate>

style

The <tvar|style>style</> attribute allows CSS attributes to be included directly.</translate> <translate> This is equivalent to enclosing the block in a <tvar|div><div></> (not <tvar|span><span></>) tag.</translate> <translate> The <tvar|size>tab‑size</> attribute cannot be specified this way; it requires an enclosing <tvar|span><span></> tag as described below under [[<tvar|anchor>#Advanced</>|Advanced]].

For example: </translate>

def quickSort(arr):
	less = []
	pivotList = []
	more = []
	if len(arr) <= 1:
		return arr
	else:
		pass

<translate> Is the result of: </translate>

<syntaxhighlight lang="Python" style="border:3px dashed blue">
def quickSort(arr):
	less = []
	pivotList = []
	more = []
	if len(arr) <= 1:
		return arr
	else:
		pass
</syntaxhighlight>

<translate>

Supported languages

The Pygments library provides support for hundreds of computer languages and file formats.</translate> <translate> [<tvar|url>http://pygments.org/languages/</> full list] is: </translate>

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)
  • JSON
  • 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

<translate> For accurate language codes, see [<tvar|external-link2>http://pygments.org/docs/lexers/</> complete details in the Pygments document] and there are some mappings for some language names which were supported by GeSHi ([<tvar|external-link3>https://github.com/wikimedia/mediawiki-extensions-SyntaxHighlight_GeSHi/blob/master/includes/SyntaxHighlightGeSHiCompat.php</> full list]). </translate>

Template:Tip

<translate> Below is a partial list of languages that GeSHi could highlight, with strike-through for languages no longer supported after the switch to Pygments. </translate>

<translate> Previously supported lexers</translate>
<translate> Code</translate> <translate> Language</translate>
4cs 4CS
6502acme MOS 6502 (6510) ACME Cross Assembler
6502kickass MOS 6502 (6510) Kick Assembler
6502tasm MOS 6502 (6510) TASM/64TASS
68000devpac Motorola 68000 - HiSoft Devpac ST 2 Assembler
abap ABAP
actionscript ActionScript
actionscript3 ActionScript3
ada Ada
algol68 ALGOL 68
apache Apache Configuration
applescript AppleScript
apt_sources Apt sources
arm ARM Assembler
asm Assembly
asp Active Server Pages (ASP)
asymptote Asymptote
autoconf Autoconf
autohotkey AutoHotkey
autoit AutoIt
avisynth AviSynth
awk AWK
bascomavr BASCOM AVR
bash Bash
basic4gl Basic4GL
bf Brainfuck
bibtex BibTeX
blitzbasic Blitz BASIC
bnf Backus–Naur Form
boo Boo
c C
c_loadrunner C Loadrunner
c_mac C (Mac)
caddcl AutoCAD DCL
cadlisp AutoLISP
cfdg CFDG
cfm ColdFusion Markup Language
chaiscript ChaiScript
cil Common Intermediate Language (CIL)
clojure Clojure
cmake CMake
cobol COBOL
coffeescript CoffeeScript
cpp C++
cpp-qt C++ (Qt toolkit)
csh C shell
csharp C#
css Cascading Style Sheets (CSS)
cuesheet Cue sheet
d D
dart Dart
dcl Data Control Language
dcpu16 DCPU-16
dcs Data Conversion System
delphi Delphi
diff Diff
div DIV
dosbatch DOS batch file
dot DOT
e E
ebnf Extended Backus–Naur Form
ecmascript ECMAScript
eiffel Eiffel
email Email (mbox \ eml \ RFC format)
epc Enerscript
erlang Erlang
euphoria Euphoria
f1 Formula One
falcon Falcon
fo FO
fortran Fortran
freebasic FreeBASIC
freeswitch FreeSWITCH
fsharp Fsharp
gambas Gambas
gdb GDB
genero Genero
genie Genie
gettext gettext
glsl OpenGL Shading Language (GLSL)
gml Game Maker Language (GML)
gnuplot gnuplot
go Go
groovy Groovy
gwbasic GW-BASIC
haskell Haskell
haxe Haxe
hicest HicEst
hq9plus HQ9+
html4strict HTML
html5 HTML5
icon Icon
idl Uno IDL
ini INI
inno Inno
intercal INTERCAL
io Io
j J
java Java
java5 Java(TM) 2 Platform Standard Edition 5.0
javascript JavaScript
jquery jQuery
kixtart KiXtart
klonec Klone C
klonecpp Klone C++
kotlin Kotlin
ksh Korn shell
latex LaTeX
lb Liberty BASIC
ldif LDAP Data Interchange Format
lisp Lisp
llvm LLVM
locobasic Locomotive BASIC
logtalk Logtalk
lolcode LOLCODE
lotusformulas Formula language
lotusscript LotusScript
lscript LightWave 3D
lsl2 Linden Scripting Language
lua Lua
magiksf Magik
m68k Motorola 68000 Assembler
make make
mapbasic MapBasic
matlab MATLAB M
mirc mIRC scripting language
mmix MMIX
modula2 Modula-2
modula3 Modula-3
mpasm Microchip Assembler
mxml MXML
mysql MySQL
nagios Nagios
netrexx NetRexx
newlisp NewLISP
nsis Nullsoft Scriptable Install System (NSIS)
oberon2 Oberon-2
objc Objective-C
objeck Objeck
ocaml OCaml
ocaml-brief OCaml
octave Octave
oobas OpenOffice.org Basic
oorexx Object REXX
oracle11 Oracle 11 SQL
oracle8 Oracle 8 SQL
oxygene Oxygene
oz Oz
parasail ParaSail
parigp PARI/GP
pascal Pascal
pcre Perl Compatible Regular Expressions
per per
perl Perl
perl6 Perl 6
pf PF
php PHP
php-brief PHP (deprecated – no colors, odd framing)
pic16 PIC assembly language
pike Pike
pixelbender Pixel Bender
pli PL/I
plsql PL/SQL
postgresql PostgreSQL
postscript PostScript
povray Persistence of Vision Raytracer
powerbuilder PowerBuilder
powershell Windows PowerShell
proftpd ProFTPD
progress OpenEdge Advanced Business Language
prolog Prolog
properties Properties file
providex ProvideX
purebasic PureBasic
pycon Python
pys60 PyS60
python
py
Python
python3
py3
Python3
q Q
qbasic QBasic/QuickBASIC
rails Rails
rebol Rebol
reg Windows Registry
rexx Rexx
robots robots.txt
rpmspec RPM Spec files
rsplus R
ruby Ruby
sas SAS
scala Scala
scheme Scheme
sh
shell
shell-session
Shell Script
scilab Scilab
sdlbasic SdlBasic
smalltalk Smalltalk
smarty Smarty
spark SPARK
sparql SPARQL
sql SQL
stonescript StoneScript (Scripting language for ShiVa3D)
systemverilog SystemVerilog
tcsh Tcsh
tcl Tcl
teraterm Tera Term
text Plain text
thinbasic thinBasic
ts TypeScript
tsql Transact-SQL
typoscript TypoScript
unicon Unicon
upc Unified Parallel C
urbi URBI
uscript UnrealScript
vala Vala
vb Visual Basic
vbnet Visual Basic .NET
vedit VEDIT
verilog Verilog
vhdl VHDL
vim Vim script
visualfoxpro Visual FoxPro
visualprolog Visual Prolog
whitespace Whitespace
whois Whois
winbatch Winbatch
xml XML
xorg_conf Xorg.conf
yaml YAML
xpp Microsoft Dynamics AX
z80 ZiLOG Z80 Assembler
zxbasic ZXBasic

<translate>

Installation

</translate>

python>https://www.python.org</> Python] version 3 (python3) to be installed on the server. This is a change from the version bundled with MediaWiki 1.30, which used Python version 2 (python). Note that the python3 binary must be installed in the execution PATH of the PHP interpreter.</translate>
<translate> Despite its update to Pygments (and away from GeSHi) and despite its updated name, this extension internally still uses the former file names as stated below.</translate>
  • Download and place the file(s) in a directory called SyntaxHighlight_GeSHi in your extensions/ folder.
  • Only when installing from git run Composer to install PHP dependencies, by issuing composer install --no-dev in the extension directory. (See T173141 for potential complications.)
  • Add the following code at the bottom of your LocalSettings.php:
    wfLoadExtension( 'SyntaxHighlight_GeSHi' );

<translate>

  • In Linux, set execute permissions for the pygmentize binary.</translate> <translate> You can use an FTP client or the following shell command to do so:</translate>
chmod a+x /path/to/extensions/SyntaxHighlight_GeSHi/pygments/pygmentize
  • ☑Y Done – Navigate to Special:Version on your wiki to verify that the extension is successfully installed.

To users running MediaWiki 1.24 or earlier:

The instructions above describe the new way of installing this extension using wfLoadExtension(). If you need to install this extension on these earlier versions (MediaWiki 1.24 and earlier), instead of wfLoadExtension( 'SyntaxHighlight_GeSHi' );, you need to use:

require_once "$IP/extensions/SyntaxHighlight_GeSHi/SyntaxHighlight_GeSHi.php";
composer install --no-dev</>, or when updating: <tvar ">directory>"../extensions/SyntaxHighlight_GeSHi/"</> and run <tvar

<translate>

Configuration

</translate>

Linux
  • <translate> <tvar|setting>$wgPygmentizePath</> (optional): Absolute path to pygmentize of the Pygments package.</translate> <translate> The extension bundles the Pygments package and <tvar|setting>$wgPygmentizePath</> points to the bundled version by default, but you can point to a different version, if you want to.</translate> <translate> For example: <tvar|setting>$wgPygmentizePath = "/usr/local/bin/pygmentize";</>.</translate>
  • <translate> <tvar|setting>$wgSyntaxHighlightModels</>: Configure the default lexer for some wiki pages.</translate> <translate> By default this will highlight javascript and css pages.</translate> <translate> Additional content models can be configured by extensions (e.g. Lua, JSON, ..).</translate> <translate> Example:</translate>
     $wgSyntaxHighlightModels[CONTENT_MODEL_SCRIBUNTO] = 'lua';
Windows
  • <translate> If you are hosting your MediaWiki on a Windows machine, you have to set the path for the Pygmentize.exe <tvar|setting>$wgPygmentizePath = "c:\\Python27\\Scripts\\pygmentize.exe";</></translate>
    • <translate> If there is no <tvar|pygmentize>pygmentize.exe</> run <tvar|install>easy_install Pygments</> from command line inside the <tvar|scripts>Scripts</> folder to generate the file.</translate>

<translate> If you are using the bundled pygmentize binary (<tvar|location>extensions/SyntaxHighlight_GeSHi/pygments/pygmentize</>), make sure your webserver is permitted to execute it.</translate> <translate> If your host does not allow you to add executables to your web directory, install python-pygments and add <tvar|setting>$wgPygmentizePath = pygmentize</> to LocalSettings.php. </translate>

<translate> Troubleshooting</translate>

<translate> After updating to MediaWiki v1.26 and above, some users started reporting problems with the extension.</translate> <translate> There could be cases, when some languages, such as Lua might not get highlighted and by turning on [[<tvar|debug>Special:MyLanguage/Manual:How to debug</>|debugging]], MediaWiki would throw out the error, <tvar|error>Notice: Failed to invoke Pygments: /usr/bin/env: python3: No such file or directory</>.</translate>

  • <translate> Try pointing <tvar|setting>$wgPygmentizePath</> in LocalSettings.php towards an external pygmentize binary.</translate>
  • 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

<translate>

VisualEditor integration

The plugin enables direct editing with [[<tvar|link>Special:MyLanguage/Extension:VisualEditor</>|VisualEditor]].</translate> <translate> A popup is opened when a user wants to edit <tvar|source>source</> or <tvar|syntaxhighlight>syntaxhighlight</> sections.</translate> <translate> For this to work, VisualEditor must be installed and configured from the latest git version, same for Parsoid.</translate> <translate> The feature randomly does not work with older Parsoid versions. </translate> <translate> See [[<tvar|veinstructions>Extension:SyntaxHighlight/VisualEditor</>]] for details </translate>

<translate>

Advanced

Unlike the <tvar|pre><pre></> and <tvar|code><code></> tags, HTML character entities such as &nbsp; need not (and should not) have the <tvar|and>&</> character escaped as &amp;.</translate> <translate> Like the <tvar|pre><pre></> tag but unlike the <tvar|code><code></> tag, tags within the range (other than its own closing tag) need not have the <tvar|less><</> symbol escaped as <tvar|less2>&lt;</>, nor does wikitext need to be escaped with a <tvar|nowiki><nowiki></> tag.

Furthermore, while <tvar|pre><pre></> assumes tab stops every 8 characters and renders tabs using actual spaces when the rendered text is copied, <tvar|syntaxhighlight><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 <tvar|span><span style="-moz-tab-size:nn; -o-tab-size:nn; tab-size:nn;"></> tag (not <tvar|div><div></>, and not using its own <tvar|style>style</> attribute).</translate> <translate> The <tvar|moz>-moz-</> prefix is required for Firefox (from version 4.0), and the <tvar|prefix-o>-o-</> prefix is required for Opera (from version 10.60 to version 15).</translate>[2] (<translate> Note that the wiki editing box assumes 8-space tabs.</translate>) <translate> This applies only to actual saved pages; previews generated through an edit box or <tvar|special>Special:ExpandTemplates</> may differ.

See also

</translate>

<translate>

  • Extensions dependent on this one:

</translate>

<translate>

  • Alternative extensions:

</translate>

<translate>

Footnotes

</translate>

[[Category:Skins used on Wikimedia{{#translation:}}|SyntaxHighlight]]

[[Category:Syntax highlighting extensions{{#translation:}}]] [[Category:GeSHi extensions{{#translation:}}]] [[Category:Extensions with VisualEditor support{{#translation:}}]] [[Category:RejectParserCacheValue extensions{{#translation:}}]]