"code" Macro

Last modified by Vincent Massol on 2026/11/12 20:27

Reference

Description

The code macro displays source code with syntax highlighting: keywords, strings and comments are coloured according to the language of the code. In the macro list of the editors it appears as "Code", in the "Formatting" category.

The highlighting is computed on the server by Pygments, so readers receive the code already coloured. To highlight code in the browser instead, use the Prism.js extension.

Usage

{{code language="java"}}
System.out.println("Hello");
{{/code}}

Written on its own, the macro displays a block of code. Written inside a paragraph, it displays the code within the sentence, and the layout parameter has no effect.

The Pygments lexer documentation gives the short names to set language to, such as java, python or xml. It documents the latest Pygments, which can know languages that the one bundled with XWiki doesn't: the List the Languages Your Wiki Can Highlight example lists exactly those of your wiki. Without language, Pygments guesses the language from the code, which often fails on a short snippet. The value none displays the code without any highlighting. A language that Pygments doesn't know, or code that it fails to highlight, is displayed without highlighting rather than as an error. For another language, a developer can write a highlighter for it.

The highlighted result is reused when the page is displayed again, so highlighting large code costs time once. This doesn't apply to code read through source, which is highlighted at each display.

Parameters

NameMandatoryAllowed ValuesDefault ValueDescription
contentnotext""The code to display. Ignored when source is set.
languagenothe short name of a Pygments lexer, or noneguessed from the codeThe language used to highlight the code. none disables highlighting.
layoutnoplain, linenumbersplainlinenumbers numbers the lines of the code. Only applies to a block of code.
sourceno<type>:<reference>noneWhere to read the code from instead of the content of the macro; see Code Read From Somewhere Else for the types. The editors list it among the advanced parameters.

The macro also accepts every parameter of the "box" Macro, such as title and cssClass.

Examples

A Block of Python Code

{{code language="python"}}
def greet(name):
    """Return a greeting for the given name."""
    return f"Hello, {name}!"

for user in ["Alice", "Bob"]:
    print(greet(user))
{{/code}}

A block of Python code with its keywords, strings and comments in different colours

Line Numbers

{{code language="python" layout="linenumbers"}}
def greet(name):
    ...
{{/code}}

The same Python code with a line number in front of each line

Code Inside a Sentence

To list the files of the current directory, run {{code language="bash"}}ls -la{{/code}} in a terminal.

A sentence with a highlighted shell command inside it

A Title Above the Code

{{code language="xml" title="pom.xml"}}
<dependency>
  <groupId>org.xwiki.platform</groupId>
  <artifactId>xwiki-platform-rendering-macro-code</artifactId>
</dependency>
{{/code}}

An XML snippet in a box, with the title pom.xml above it

List the Languages Your Wiki Can Highlight

This table lists every language that the bundled Pygments knows, with the short names to give to language. It uses the Python Macro, bundled with XWiki, and needs programming right.

{{python}}
from pygments.lexers import get_all_lexers

print("|=Language|=Short Names|=File Patterns")
for name, aliases, patterns, mimetypes in sorted(get_all_lexers()):
    print("|%s|%s|%s" % (name, ", ".join(aliases), ", ".join(patterns)))
{{/python}}

Code Read From Somewhere Else

Instead of writing the code inside the macro, you can give its location with the source parameter, as <type>:<reference>. The content of the macro is then ignored.

TypeThe reference isExample
string, the defaultthe code itselfsource="string:x = 1", or source="x = 1"
scriptthe name of a variable set by a script earlier on the pagesource="script:myvar"
pagea page, whose content is displayedsource="page:Sandbox/MyPage"
documenta page, given as a document referencesource="document:Sandbox.MyPage"
page_attachmentan attachment of a pagesource="page_attachment:Sandbox/MyPage/script.js"
attachmentan attachment, given as a document attachment referencesource="attachment:[email protected]"
object_propertya property of the first object of a class on a pagesource="object_property:Sandbox.MyPage^XWiki.StyleSheetExtension.code"

When language is not set, the macro takes it from the source when it can: HTML, XML, JavaScript, JSON, PHP or CSS from the media type of an attachment, HTML from a page written in an HTML syntax, and Velocity from a text area property holding Velocity code. Otherwise, Pygments guesses it.

Reading the code from elsewhere has conditions:

  • With script, the last author of the page must have script right, and the variable must exist; otherwise the macro displays an error. It also displays an error where scripts are not allowed to run, such as in a comment.
  • With a page, an attachment or a property, both the last author of the page and the person viewing it must be allowed to view the page it comes from. The source must exist. A page given without a language is read in the language the reader is viewing, from its translation when it has one.
  • An attachment larger than 1 MB is refused, unless an administrator raised the limit.

For example, with a fibonacci.js attachment on the page and no language given, the macro takes the language from the media type of the attachment:

{{code source="attachment:fibonacci.js"}}{{/code}}

The JavaScript content of the attachment, highlighted

Related

Get Connected