XML technologies / Markdown in publishing / Present code
Markdown is well suited to documentation that combines explanatory text with technical sources. The code remains copyable and can retain its indentation. However, there are three separate tasks: presenting code verbatim, highlighting its syntax, and executing or validating it.
The examples show both basic block notations, nested code fences, attributes and a complete small XSLTstylesheet. Execution is limited to the expressly supplied trustworthy test files. Pandoc itself is not an execution service for arbitrary code here. S08, S04
Example 26: Indented code block
CommonMark
Four leading spaces mark a code block here. They are part of the Markdown notation and are not themselves displayed as four additional spaces in the output. The further indentation within the XML fragment, however, is preserved.
SOURCE
The following fragment is literal code:
<book>
<title>Reference Edition</title>
</book>RENDERED RESULT
The blank line before the Block separates it from the preceding paragraph. Within lists, the container's indentation rules also apply. Indented code blocks in this form do not allow a language identifier. For detailed technical documentation, fenced code blocks are therefore often more convenient.
Example 27: Code fence and language identifier
CommonMark
At least three backticks open a code fence. A sequence of at least the same length closes it. Tildes are an alternative, but must then also be used when closing. The character sequence xml is a hint for language handling.
SOURCE
```xml
<article id="a1">
<title>Reference Edition</title>
<p>The release is approved.</p>
</article>
```RENDERED RESULT
The parser recognizes a code block; the colored syntax highlighting is handled by the output processing. Neither attractive coloring nor the identifier confirms that the XML is well-formed or schema-valid. The package additionally checks the corresponding XML with a XML parser. This keeps presentation, syntactic checking, and domain-specific validation distinguishable from one another.
Example 28: Documenting Markdown syntax literally
CommonMark
Anyone explaining Markdown often needs to show a code fence within a code example. The outer fence here uses four backticks and the inner one uses three. As a result, the inner sequence does not terminate the outer block.
SOURCE
````markdown
```xml
<status>approved</status>
```
````RENDERED RESULT
The output is deliberately not a rendered XML example, but a visible Markdown code fence. This principle also applies to automatically generated documentation: the length of the outer fence must be adapted to the content. Simply always placing three backticks around external text can incorrectly structure the entire rest of the page.
Example 29: Code attributes and line numbers
Pandoc Markdown
Pandoc allows attributes directly on the code fence. The class xml specifies the language, numberLines requests line numbers, and startFrom sets the starting value. The HTML preview actually uses the generated highlighting structure and appropriate CSSrules.
SOURCE
```{.xml .numberLines startFrom="10"}
<article>
<title>Reference Edition</title>
</article>
```RENDERED RESULT
These specifications are not a normal CommonMark language identifier. Without a suitable renderer, the line numbers may be missing. A cross-reference to "line 12" is therefore also not created automatically. When code is subsequently changed, an editorially written reference must continue to be checked.
Example 30: An actually executed XSLT example
CommonMark
The stylesheet code is merely displayed in Markdown. The actual execution takes place separately with the accompanying XML document inputs/article.xml and the stylesheet inputs/title.xsl. The expected text is Reference Edition. A test compares the actual transformation output with it.
SOURCE
```xslt
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/article">
<xsl:value-of select="title"/>
</xsl:template>
</xsl:stylesheet>
```RENDERED RESULT
This checks two different assertions: Pandoc processes the code block and an XSLT processor processes the stylesheet. A successful Markdown build alone is not proof that program examples work. Untrusted code must not be automatically executed with unrestricted access to the file system or network.




