literal blocks

Sphinx interprets anything following the form :: <newline> as pre-formatted text:

Text in a literal block::

   The indented paragraph following this is highlighted and presented as
   is.

results in :

Text in a literal block:

The indented paragraph following this is highlighted and presented as
is.

(Notice one of the colons has been removed.)

An isolated :: functions similarly - both colons removed in output.

code

Code-blocks are highlighted when they follow a .. code-block:: directive, where a trailing argument can specify the language.

The following python code is thus highlighted simply by appearing in an indented region preceded by :::

#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""Fibonacci sequences using generators

This program is part of "Dive Into Python", a free Python book for
experienced programmers.  Visit http://diveintopython.org/ for the
latest version.
"""


def fibonacci(max):
    a, b = 0, 1
    while a < max:
        yield a
        a, b = b, a+b

for n in fibonacci(1000):
    print n,

See also

literal includes for including code from files, numbering it and including code selectively.

Using the .. code-block:: <language here> directive, code will be appropriately presented. So:

.. code-block:: sh

   echo $PATH
   for i in $(seq 1 10);
      do
         echo $$( 2*i+3 ))
      done;

will be highlighted as shell-code:

echo $PATH
for i in $(seq 1 10);
   do
      echo $(( 2*i+3 ))
   done;

Table Of Contents

Previous topic

some markup features

Next topic

inclusion

This Page