Code block |…

Code blocks are used to mark computer code. This can be an algorithm in a programming language such as PHP, a shell script, a stylesheet, an example Aneamal text …

The related code string markup can be used for code within lines of text.

How to

Start every line of a code block with a vertical bar like in the examples below.

Inside a code block no markup is interpreted. Every character is displayed verbatim.

A code block may optionally be followed by a caption, that is one or more lines of text that do not start with a vertical bar and in which phrase markup is interpreted.

Examples

The first example displays the code for a simple implementation of the factorial function in the Python programming language:

|def f(n):
|	return f(n - 1) * n if n > 1 else 1
def f(n):
	return f(n - 1) * n if n > 1 else 1

Here is another Python program, this time with a caption:

|x = 1.0
|for i in range (0, 0xface):
|	x = 1/x + 1
|
|print (x)
Approximation of the golden ratio
x = 1.0
for i in range (0, 0xface):
	x = 1/x + 1

print (x)
Approximation of the golden ratio

For developers

When Aneamal is translated to HTML, a processed code block is turned into a HTML code element nested inside a HTML pre element, so the examples become:

<pre><code>def f(n):
	return f(n - 1) * n if n &gt; 1 else 1</code></pre>
<figure>
<pre><code>x = 1.0
for i in range (0, 0xface):
	x = 1/x + 1

print (x)</code></pre>
<figcaption>Approximation of the golden ratio</figcaption>
</figure>

Due to having a caption, the second example is also wrapped in a HTML figure element with a nested figcaption element.

Browsers usually render the pre and code elements with a monospace font by default, but you can make this explicit with CSS. You can specify font families of your choice, but it is advisable to include the generic family monospace in the font declaration as well. This will guarantee that the code is displayed in a monospace font even when your preferred font families are not available.

Note that some browsers automatically display the generic monospace font with reduced font size. You can prevent this behaviour by adding the generic family in CSS twice:

code {
	font-family: "Source Code Pro", monospace, monospace;
}