Syntax Highlighting Plugin
Highlight source code fragments for many languages
Introduction
The Syntax Highlighting Plugin is used to emphasize the rendering of your wiki text according to several languages. It currently uses
enscript
to render its output.
Supported languages
Recent versions of
enscript
support highlighting of the following languages:
ada, asm, awk, bash, c, changelog, cpp, csh, delphi, diff, diffs, diffu, dylan, eiffel, elisp, forth, fortran, fortran_pp, haskell, html, icon, idl, inf, java, javascript, ksh, lua, m4, mail, makefile, matlab, nroff, oberon2, objc, outline, oz, pascal, perl, php, postscript, pyrex, python, rfc, ruby, scheme, sh, skill, smalltalk, sml, sql, states, synopsys, tcl, tcsh, tex, vba, verilog, vhdl, vrml, wmlscript, zsh
You need to installed and enabled the SyntaxHighlightingPlugin to see the actual languages that are available.
Additional languages can be added, see
genscript documentation.
Syntax Rules
To use this plugin, use the following syntax:
<sticky>
%CODE{ lang="cpp" num="10" numstep="2" }%
...code...
%ENDCODE%
</sticky>
The
<sticky>
tags are required to prevent TWiki's WYSIWYG editor from removing line breaks inside the code block.
In addition,
%SYNTAXHIGHLIGHTING{supported}%
returns the list of currently supported languages as indicated above.
CODE Parameters
Parameter |
Description |
Default |
"..." or lang="..." |
Source language. Supported languages: ada , asm , awk , bash , changelog , cpp , csh , c , delphi , diffs , diff , diffu , dylan , eiffel , elisp , Name: , forth , fortran_pp , fortran , haskell , html , icon , idl , inf , javascript , java , ksh , lua , m4 , mail , makefile , matlab , nroff , oberon2 , objc , outline , oz , pascal , perl , php , postscript , pyrex , python , rfc , ruby , scheme , sh , skill , Smalltalk , sml , sql , states , synopsys , tcl , tcsh , tex , vba , verilog , vhdl , vrml , wmlscript , zsh |
(none, required) |
num="..." |
Show line numbers next to the source code. An integer indicates the start number. Also "on" and "off" |
"off" |
step="..." |
Increment line numbers with the given step. Negative numbers will decrement. |
"1" |
style="..." |
Style of box around the source code |
light gray box |
numstyle="..." |
Style of line number column |
light brown box |
Note: The default can be set in configure
Examples
The following text:
<sticky>
%CODE{"c++"}%
#include <iostream>
int main()
{
// Hello world example
std::cout << "Hello, world." << std::endl;
}
%ENDCODE%
</sticky>
gives (if installed):
%CODE{"c++"}%
#include
int main()
{
// Hello world example
std::cout << "Hello, world." << std::endl;
}
%ENDCODE%
You can also output numbered lines starting at 10 with this text:
<sticky>
%CODE{"sh" num="10"}%
#!/bin/sh
languages=`enscript --help-highlight | grep 'Name:' | cut -d ' ' -f 2`
for l in $languages; do
cat << EOF
* $l
EOF
done
%ENDCODE%
</sticky>
gives (if installed):
%CODE{"sh" num="10"}%
#!/bin/sh
languages=`enscript --help-highlight | grep 'Name:' | cut -d ' ' -f 2`
for l in $languages; do
cat << EOF
EOF
done
%ENDCODE%
Plugin Installation & Configuration
Note: You do not need to install anything on the browser to use this extension. The following instructions are for the administrator who installs the extension on the TWiki server.
- For an automated installation, run the configure script and follow "Find More Extensions" in the in the Extensions section.
- Or, follow these manual installation steps:
- Download the ZIP file from the extension home on twiki.org (see below).
- Unzip
SyntaxHighlightingPlugin.zip
in your twiki installation directory.
- Set the ownership of the extracted directories and files to the webserver user.
- Install the dependencies (if any).
- Plugin configuration and testing:
- Run the configure script and enable the plugin in the Plugins section.
- Configure additional plugin settings in the Extensions section if needed.
- Test if the installation was successful using the examples provided.
- Configure settings in Extensions section:
-
$TWiki::cfg{Plugins}{SyntaxHighlightingPlugin}{EnscriptPath}
# path to enscript script
-
$TWiki::cfg{Plugins}{SyntaxHighlightingPlugin}{DefaultLang}
# Default language
-
$TWiki::cfg{Plugins}{SyntaxHighlightingPlugin}{Numbering}
# Default for line numbering, 'off' or 'on'
-
$TWiki::cfg{Plugins}{SyntaxHighlightingPlugin}{Step}
# Default step of numbering
-
$TWiki::cfg{Plugins}{SyntaxHighlightingPlugin}{Style}
# Style of pre tag containing the source code
-
$TWiki::cfg{Plugins}{SyntaxHighlightingPlugin}{NumStyle}
# Style of numbering column
-
$TWiki::cfg{Plugins}{SyntaxHighlightingPlugin}{Debug}
# Debug setting
- Change of Syntax: Content of an older version of this plugin with the following syntax needs to be updated:
%begin sh%
...code...
%end%
This has been changed to be more consistent with other twiki variables. There is a script included which will replace the old syntax with the new one. To use it, copy it from the tools
directory and into your data
directory. When you run it, it will look through your webs and replace the syntax. Note that its not the best script in the world, so always test it on a copy of your data first!
- Plugin Files:
data/TWiki/SyntaxHighlightingPlugin.txt | Plugin topic |
data/TWiki/VarCODE.txt | Variable documentation topic |
pub/TWiki/SyntaxHighlightingPlugin/cpp-screenshot.png | |
pub/TWiki/SyntaxHighlightingPlugin/cpp-screenshot-300.png | |
lib/TWiki/Plugins/SyntaxHighlightingPlugin.pm | Perl module |
lib/TWiki/Plugins/SyntaxHighlightingPlugin/Config.spec | |
tools/SyntaxHighlightingPlugin_covert.pl | |
Plugin Info
Related Topics: VarCODE,
TWikiPreferences,
TWikiPlugins