openstack-manuals/doc/contributor-guide/source/rst-conv/source-code.rst
Andreas Jaeger 2d44b2b36d Prepare for Sphinx 1.5
The new sphinx version introduces some changes that break build:

* Warns if code cannot be parsed for highlighting. Fix the code so
  that it can be parsed, this includes uncommenting "..." lines.
  Note that not every config file is an ini-file.
  Also, the parser seems to have bugs and cannot parse all files.
  Fix mysql ini file and enable the parameter, see
http://dev.mysql.com/doc/refman/5.7/en/innodb-parameters.html#sysvar_innodb_file_per_table
* :option: works only with declared options, replace useage with
  simple ``.

This change only handles a few files, more to come later.

Change-Id: I7c7335e514581622dd562ee355f62d6ae1beaa18
2017-01-11 20:37:55 +01:00

5.6 KiB

Code samples

Format code snippets as standalone literal blocks. There are several ways to define a code-block within an RST file.

Standard literal block

Directive :: or code
Arguments none
Options none
Description
  • Introduces a standard reST literal block.
  • Preserves line breaks and whitespaces.
  • Automatically highlights language (Python, by default)

Use :: or code directive if you provide the code snippets written in one programming language within one file. By default, the code-block formatted this way is shown in a Python highlighting mode.

To define another highlighting language, use the code-block directive as described in the non-standard-block section. Make sure to close these tags with end. Additionally, add the path tag to provide the parser with the path of the configuration files. This should allow the parser to parse the markup syntax to Bash.

Note

The end tag allows the parser to identify the scope of code blocks. The path tag allows the parser to identify the path of the config file. For more information, refer to the rst2bash section. These changes are mandatory only for the installation guides.

Input

Add logging statements::

  from nova.openstack.common import log as logging
  LOG = logging.getLogger(__name__)

Output

Add logging statements:

from nova.openstack.common import log as logging
LOG = logging.getLogger(__name__)

Non-standard literal block

Directive code-block
Arguments python (default), ruby, c, console, ini, and others
Options linenos, emphasize-lines
Description
  • Specifies the highlighting language directly.
  • Preserves line breaks and whitespaces.

To optimize the output of code for a specific programming language, specify the corresponding argument with code-block. Use ini for configuration files, console for console inputs and outputs, and so on.

Input

.. path /path/to/config/file
.. code-block:: ini

   # Configuration for nova-rootwrap
   # This file should be owned by (and only-writeable by) the root user

   [DEFAULT]
   # List of directories to load filter definitions from (separated by ',').

.. end

Output

# Configuration for nova-rootwrap
# This file should be owned by (and only-writeable by) the root user

[DEFAULT]
# List of directories to load filter definitions from (separated by ',').

Note

When you write the command example, you should write the input and output as it is from the console in one code block, not add an extra blank line, not split them into input block and output block. You can omit the output where appropriate.

Literal block using a remote file

Directive remote-code-block
Arguments python (default), ruby, c, console, ini, and others
Options linenos, emphasize-lines
Description
  • Specifies the highlighting language directly.
  • Preserves line breaks and whitespaces.

This directive behaves exactly like the code-block directive, but gets the content from a remote URL (http or https).

remote-code-block is a custom directive.

Input

.. path /path/to/config/file
.. remote-code-block:: ini

   http://git.openstack.org/cgit/openstack/nova/tree/etc/nova/api-paste.ini?h=stable/newton

.. end

Output

############
# Metadata #
############
[composite:metadata]
use = egg:Paste#urlmap
/: meta

[pipeline:meta]
pipeline = cors ec2faultwrap logrequest metaapp