Code blocks

This page shows awesome features of code blocks.

Sphinx code-block directive

In most cases, you should use Sphinx’s code-block directive to mark up code blocks. If you don’t provide an explicit language to the directive, a fallback is used:

  1. You used the highlight directive to set a default on a per-document basis. Any code block _after_ this directive is highlighted with the language you specified.

  2. You set the global fallback language for highlighting in the Sphinx configuration file with the highlight_language option.

Explicit is better than implicit. Unless all code blocks in your docs are in the same language, it’s better to provide the language of the code block to the code-block directive:

rst
.. code-block:: python

   print("Hello World")

This renders as:

python
print("Hello World")

All code blocks have a Copy button. Clicking the button copies the code to the clipboard.

To add a caption to a code block, use the :caption: CAPTION_TEXT option:

javascript Example code
console.log("Hello World")

To show line numbers in the code block, use the :linenos: option:

python
1for i in range(3):
2   print(f"{i} line of code")

To emphasize specific lines in code blocks, use the :emphasize-lines: LINE_NUMBERS option:

bash
echo "Don't emphasize this"
echo "Emphasize this"
echo "Don't emphasize this either"

Highlight code changes

Often, you want to highlight what code need to be changed. With the Awesome Theme, you can use the following options for the code-block directive:

  • To highlight lines, that need to be added, use :emphasize-added: LINE_NUMBERS.

  • To highlight lines that need to be removed, use :emphasize-removed: LINE_NUMBERS.

python
print("red")
print("green")
print("regular highlighting is applied")

The :emphasize-added: and :emphasize-removed: options preserve the highlighting of the code. If you copy the code, the + and - characters aren’t copied.

If you don’t want to use these options, you can use Pygments built-in diff language:

diff
+ print("red")
- print("green")
  print("no highlighting is applied here")

Here, the syntax isn’t highlighted. If you copy the code to the clipboard, the + and - characters are copied as well.

The following example is for testing the previous options with line numbers:

python
1print("One line of code")
2print("Removed line of code")
3print("Added line of code")
4print("Emphasized line of code")
5print("Normal line of code")

There is currently one visual bug with emphasizing lines #171:

python A really long line
print("A shorter line of code.")
print("And a really long line of code that should overflow the container on most screen sizes which illustrates the issue.")

You can’t include reStructuredText markup in code blocks, such as bold text or hyperlinks.

Docutils code directive

The code-block directive only works with Sphinx. If you want to re-use your documentation outside Sphinx, for example, rst2html, you can also use the code directive to mark up code blocks.

shell
echo "This is rendered with the docutils' code directive"

You can’t use captions, highlighted lines, or any of the other options for Sphinx code blocks.

Parsed literal blocks

If you want to write blocks of literal text containing any markup, such as bold text or hyperlinks, use a parsed-literal directive.

This can contain markup, but not syntax highlighting.

You can’t use syntax highlighting with parsed-literal blocks.