Code snippets
Code block
Use code blocks when you have to showcase multiple lines of code. For example:
A configuration example
A code construct example
A command line usage example
An API call example
In semantic markup, use the <code-block>
tag.
In Markdown place triple backticks ```
before and after the code block.
It will be highlighted according to the language you specify, and the user will be able to copy it by clicking icon.
The code block can include links, the syntax is [[[alt text|URL]]]
. For example, to refer to an article or an external specification.
Enhance the code block using the following attributes:
- lang
Specify the programming or markup language. In Markdown, place it after the opening
```
.- collapsible
Use it to make the code block collapsible. It is useful when your code example is long and you don't want a reader to scroll down.
- default-state=[collapsed|expanded]
If you've set
collapsible="true"
, select the default state. By default, it is collapsed.public static void main(String[] args) { System.out.println("Hi!") }public static void main(String[] args) { System.out.println("Hi!") }- collapsed-title
Specify the title to display it, when the block is collapsed.
- collapsed-title-line-number
Specify the line to use as the title, when the block is collapsed.
- src
Name of the file with the code snippet.
The source file must be located under the directory you've specified in the
snippets
attribute of the writerside.cfg file.- include-lines
Range or comma-separated list of lines that you want to include.
- include-symbol
Specify the code construction, like class or method name to include. The attribute value is a dot-separated path to the construction, for example, MyClass.Myfunction.
- prompt
Use to specify the prompt symbol to display at the beginning of each line of code. The code sample is copied without a prompt.
- ignore-vars
If set to
true
— %-variables will be escaped, which means nothing will be rendered instead of them.- validate=[true|false]
Set true to validate the code in a snippet.
- noinject
Specify if you do not want to validate the code block content as injected XML.
- disable-links
If you use links like
[[[alt text|URL]]]
inside a code block, you can also disable them using this setting. So this markup won't be considered a link.- id
Specify a unique identifier to use as a link anchor and generate a meaningful URL for this code block. You can reference and reuse any element that has an ID.
- filter
Define a set of custom filters for conditionally including this code block.
- instance
Define a set of instances for conditionally filtering this code block.
- switcher-key
Make this code block switchable depending on the selected key.
Inline code
Use inline code when you need to wrap a short fragment of code in text, for example, to highlight a name of a variable, a method, a function, or a terminal command.
In semantic markup, to format some text as code wrap it with a <code>
tag. It doesn't have any additional attributes.
In Markdown, enclose it in backticks (`).
To output a line on the screen use echo "Hello world!"
.
Compare two code blocks
To display two code snippets next to each other, use <compare>
tag.
Put exactly two code snippets (both Markdown and semantic markup is accepted) inside.
The compare
element has the following attributes:
- style
[left-right|top-bottom]
Select the arrangement of the code snippets.- first-title
Specify the first block title. By default, Before.
- second-title
Specify the second block title. By default, After.
- id
Specify a unique identifier to use as a link anchor and generate a meaningful URL for this comparison block. You can reference and reuse any element that has an ID.
- filter
Define a set of custom filters for conditionally including this comparison block.
- instance
Define a set of instances for conditionally filtering this comparison block.
- switcher-key
Make this comparison block switchable depending on the selected key.