Code Macro

The Code macro is used to highlight code in languages supported by Pygments.

Usage

{{code language=<language>}}
// Lines of code
{{/code}}

Parameters

NameOptionalAllowed valuesDescriptionDefault value
languageYesA stringThe language identifier for the code snippetIf not specified, the macro will try to determine the syntax. To avoid the highlighting, you can use none.

The Code macro also supports all the parameters of the Box macro which are:

NameOptionalAllowed valuesDescriptionDefault value
titleYesA string: raw text or "xwiki/2.0" syntaxThe title displayed in the message boxNone
imageYesA string representing an absolute URLThe image to display in the message boxNone
cssClassYesA stringThe CSS sheet to use for rendering the boxNone

Customization

It is possible to configure the style used by the Code macro via the "WEB-INF/xwiki.properties" file by adding the parameter: rendering.macro.code.pygments.style. The default value is default but you can actually specify any style name supported by Pygments.

Examples

Using only the language parameter

{{code language="java"}}
{{groovy}}
return java.util.Arrays.asList(new org.xwiki.rendering.block.WordBlock("Hello"),
org.xwiki.rendering.block.SpaceBlock.SPACE_BLOCK,
new org.xwiki.rendering.block.WordBlock("world"));
{{/groovy}}
{{/code}}

will display 

{{groovy}}
return java.util.Arrays.asList(new org.xwiki.rendering.block.WordBlock("Hello"),
org.xwiki.rendering.block.SpaceBlock.SPACE_BLOCK,
new org.xwiki.rendering.block.WordBlock("world"));
{{/groovy}}

Using the Box macro parameters

{{code language="java" title="Content" image="http://mywiki.com/xwiki/bin/download/Main/WebHome/Tulips.jpg"
cssClass="floatingBox"}}
{{groovy}}
return java.util.Arrays.asList(new org.xwiki.rendering.block.WordBlock("Hello"),
org.xwiki.rendering.block.SpaceBlock.SPACE_BLOCK,
new org.xwiki.rendering.block.WordBlock("world"));
{{/groovy}}
{{/code}}

will display

CodeMacro.png

The custom CSS can be included in a StyleSheetExtension object attached to the page. For the above example the CSS rules for the ".floatingBox" class are:

.floatingBox img{
width: 50px;
}
.floatingBox{
width: 800px;
}

Related Pages

Trying to decide between Cloud and On Premise? See the comparison on the XWiki Help Center.

Search this space