Code Macro
Highlights code |
Type | JAR |
Category | |
Developed by | |
Rating | |
License | GNU Lesser General Public License 2.1 |
Bundled With | XWiki Standard |
Table of contents
Description
See https://pygments.org/languages/ for the list of all supported languages. If you have a hard time finding the right id for the language, you can look at https://pygments.org/docs/lexers/ (Short names).
Usage
... code here...
{{/code}}
Parameters definition
Name | Optional | Allowed values | Default value | Description |
---|---|---|---|---|
language | yes | a string | the macro tries to determine the syntax | the language identifier of the provided code snippet. If no language identifier is specified then the macro will try to determine the syntax. To avoid highlight you can use "none" language identifier. |
layout | yes |
| plain | The layout to apply to the code. |
This macro also support all Box Macro parameters.
Configuration
It's possible to set some configuration for this macro in the xwiki.properties file:
Name | Optional | Allowed values | Default value | Description |
---|---|---|---|---|
rendering.macro.code.pygments.style (since 2.4M1) | yes | a string | default | the name of the Pygments style to use. See http://pygments.org/docs/styles/ for more about styles. |
Example:
Adding new languages
Anyone can add a new language support by:
- adding a XWiki java component: implementing HighlightParser interface component. See Component Module for more details on how to write component for the new XWiki 2.0 architecture
- the default org.xwiki.rendering.parser.HighlightParser is based on Pygments so the best for both projects is probably to provide a new syntax parser to Pygments project. See http://pygments.org/contribute/.
Example
<html>
<head>Cool!</head>
</html>
{{/code}}
Result
<head>Cool!</head>
</html>
Listing all supported languages
The list is on the Pygments language list. However if you want to dynamically check the list of supported language for the XWiki version used of Pygments, you could put the following in a wiki page, using a user with Programming Rights (and assuming that the Python Macro is installed):
import pygments
from pygments.lexers import get_all_lexers
print "|=Language |=Name |=File Extension |=Mime Types"
for lexer in get_all_lexers():
print "| ", lexer[0], " | ", ", ".join(lexer[1]), " | ", ", ".join(lexer[2]), " | ", ", ".join(lexer[3])
{{/python}}
Prerequisites & Installation Instructions
We recommend using the Extension Manager to install this extension (Make sure that the text "Installable with the Extension Manager" is displayed at the top right location on this page to know if this extension can be installed with the Extension Manager).
You can also use the manual method which involves dropping the JAR file and all its dependencies into the WEB-INF/lib folder and restarting XWiki.
Dependencies
Dependencies for this extension (org.xwiki.platform:xwiki-platform-rendering-macro-code 14.6):
- commons-io:commons-io 2.11.0
- org.xwiki.rendering:xwiki-rendering-macro-box 14.6
- org.python:jython-slim 2.7.2
- org.pygments:pygments 2.4.2
- org.xwiki.rendering:xwiki-rendering-api 14.6
- org.xwiki.rendering:xwiki-rendering-transformation-macro 14.6