Code
With Writerside, you can format pieces of text as code, insert blocks of code, command input and output examples, and compare pieces of code.
Inline code
Use inline formatting to wrap a piece of text as a variable, method, or function name, a command or option.
To insert some inline code, use the <code>
element. In Markdown, use single backticks `
.
Call the getAllItems()
method.
Code blocks
Use code blocks to provide samples of source code, configuration files, command-line interaction, or REST API calls.
To add a code block, use the <code-block>
element and specify the language in the lang
attribute. In Markdown, use triple backticks ```
and specify the language directly after the opening backticks.
Result:
Your readers can copy the contents of the code block by clicking the icon.
XML and HTML code
If you need to provide a sample of XML or HTML code, wrap the contents of the code block with a CDATA section. This will prevent Writerside from processing tags. You can also use the corresponding intention action for this.
Alternatively, you can escape the <
and >
characters using <
and >
.
Links in code blocks
Specify links to topics and external URLs from code blocks as [[[text|URL]]]
.
Result:
To disable links and render [[[text|URL]]]
as is, set disable-links="true"
.
Reference code from file
You can add files with code samples to a dedicated directory in your documentation project and use them in multiple code blocks. This is better than adding the same code samples literally and provides a single place for managing your code samples.
Specify the name of the directory with code samples in the <snippets> element of writerside.cfg.
<snippets src="codeSnippets"/>Add a file with code to the code snippets directory, for example:
@Test fun testSum() { val expected = 42 assertEquals(expected, testSample.sum(40, 2)) } @Test fun testMultiply() { val expected = 42 assertEquals(expected, testSample.multiply(21, 2)) }Use the
src
attribute to specify the file name:<code-block lang="kotlin" src="newTest.kt"/>```kotlin ``` { src="newTest.kt" }Result:
@Test fun testSum() { val expected = 42 assertEquals(expected, testSample.sum(40, 2)) } @Test fun testMultiply() { val expected = 42 assertEquals(expected, testSample.multiply(21, 2)) }
Although referencing files from a dedicated directory is more convenient, you do not need to do it this way. Instead of registering the snippets directory, you can reference files using a path relative to the current topic file:
You can also specify which parts of the code from the file you want in a code block. For example, use the include-lines
attribute to specify lines to include:
Result:
Alternatively, use the include-symbol
attribute to specify the symbol to include, such as a function:
Result:
Compare code blocks
Use the <compare>
element to demonstrate the difference between two code blocks, for example, before and after refactoring.
Result: