| 1 |
Docutils markup API |
| 2 |
=================== |
| 3 |
|
| 4 |
This section describes the API for adding reStructuredText markup elements |
| 5 |
(roles and directives). |
| 6 |
|
| 7 |
|
| 8 |
Roles |
| 9 |
----- |
| 10 |
|
| 11 |
Roles follow the interface described below. |
| 12 |
They have to be registered by an extension using |
| 13 |
:meth:`.Sphinx.add_role` or :meth:`.Sphinx.add_role_to_domain`. |
| 14 |
|
| 15 |
|
| 16 |
.. code-block:: python |
| 17 |
|
| 18 |
def role_function( |
| 19 |
role_name: str, raw_source: str, text: str, |
| 20 |
lineno: int, inliner: Inliner, |
| 21 |
options: dict = {}, content: list = [], |
| 22 |
) -> tuple[list[Node], list[system_message]]: |
| 23 |
elements = [] |
| 24 |
messages = [] |
| 25 |
return elements, messages |
| 26 |
|
| 27 |
The *options* and *content* parameters are only used for custom roles |
| 28 |
created via the :dudir:`role` directive. |
| 29 |
The return value is a tuple of two lists, |
| 30 |
the first containing the text nodes and elements from the role, |
| 31 |
and the second containing any system messages generated. |
| 32 |
For more information, see the `custom role overview`_ from Docutils. |
| 33 |
|
| 34 |
.. _custom role overview: https://docutils.sourceforge.io/docs/howto/rst-roles.html |
| 35 |
|
| 36 |
|
| 37 |
Creating custom roles |
| 38 |
^^^^^^^^^^^^^^^^^^^^^ |
| 39 |
|
| 40 |
Sphinx provides two base classes for creating custom roles, |
| 41 |
:class:`~sphinx.util.docutils.SphinxRole` and :class:`~sphinx.util.docutils.ReferenceRole`. |
| 42 |
|
| 43 |
These provide a class-based interface for creating roles, |
| 44 |
where the main logic must be implemented in your ``run()`` method. |
| 45 |
The classes provide a number of useful methods and attributes, |
| 46 |
such as ``self.text``, ``self.config``, and ``self.env``. |
| 47 |
The ``ReferenceRole`` class implements Sphinx's ``title <target>`` logic, |
| 48 |
exposing ``self.target`` and ``self.title`` attributes. |
| 49 |
This is useful for creating cross-reference roles. |
| 50 |
|
| 51 |
|
| 52 |
Directives |
| 53 |
---------- |
| 54 |
|
| 55 |
Directives are handled by classes derived from |
| 56 |
``docutils.parsers.rst.Directive``. They have to be registered by an extension |
| 57 |
using :meth:`.Sphinx.add_directive` or :meth:`.Sphinx.add_directive_to_domain`. |
| 58 |
|
| 59 |
.. module:: docutils.parsers.rst |
| 60 |
|
| 61 |
.. class:: Directive |
| 62 |
|
| 63 |
The markup syntax of the new directive is determined by the follow five class |
| 64 |
attributes: |
| 65 |
|
| 66 |
.. autoattribute:: required_arguments |
| 67 |
.. autoattribute:: optional_arguments |
| 68 |
.. autoattribute:: final_argument_whitespace |
| 69 |
.. autoattribute:: option_spec |
| 70 |
|
| 71 |
Option validator functions take a single parameter, the option argument |
| 72 |
(or ``None`` if not given), and should validate it or convert it to the |
| 73 |
proper form. They raise :exc:`ValueError` or :exc:`TypeError` to indicate |
| 74 |
failure. |
| 75 |
|
| 76 |
There are several predefined and possibly useful validators in the |
| 77 |
:mod:`docutils.parsers.rst.directives` module. |
| 78 |
|
| 79 |
.. autoattribute:: has_content |
| 80 |
|
| 81 |
New directives must implement the :meth:`run` method: |
| 82 |
|
| 83 |
.. method:: run() |
| 84 |
|
| 85 |
This method must process the directive arguments, options and content, and |
| 86 |
return a list of Docutils/Sphinx nodes that will be inserted into the |
| 87 |
document tree at the point where the directive was encountered. |
| 88 |
|
| 89 |
Instance attributes that are always set on the directive are: |
| 90 |
|
| 91 |
.. attribute:: name |
| 92 |
|
| 93 |
The directive name (useful when registering the same directive class under |
| 94 |
multiple names). |
| 95 |
|
| 96 |
.. attribute:: arguments |
| 97 |
|
| 98 |
The arguments given to the directive, as a list. |
| 99 |
|
| 100 |
.. attribute:: options |
| 101 |
|
| 102 |
The options given to the directive, as a dictionary mapping option names |
| 103 |
to validated/converted values. |
| 104 |
|
| 105 |
.. attribute:: content |
| 106 |
|
| 107 |
The directive content, if given, as a :class:`!ViewList`. |
| 108 |
|
| 109 |
.. attribute:: lineno |
| 110 |
|
| 111 |
The absolute line number on which the directive appeared. This is not |
| 112 |
always a useful value; use :attr:`srcline` instead. |
| 113 |
|
| 114 |
.. attribute:: content_offset |
| 115 |
|
| 116 |
Internal offset of the directive content. Used when calling |
| 117 |
``nested_parse`` (see below). |
| 118 |
|
| 119 |
.. attribute:: block_text |
| 120 |
|
| 121 |
The string containing the entire directive. |
| 122 |
|
| 123 |
.. attribute:: state |
| 124 |
state_machine |
| 125 |
|
| 126 |
The state and state machine which controls the parsing. Used for |
| 127 |
``nested_parse``. |
| 128 |
|
| 129 |
.. seealso:: |
| 130 |
|
| 131 |
`Creating directives`_ HOWTO of the Docutils documentation |
| 132 |
|
| 133 |
.. _Creating directives: https://docutils.sourceforge.io/docs/howto/rst-directives.html |
| 134 |
|
| 135 |
|
| 136 |
.. _parsing-directive-content-as-rest: |
| 137 |
|
| 138 |
Parsing directive content as reStructuredText |
| 139 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 140 |
|
| 141 |
Many directives will contain more markup that must be parsed. |
| 142 |
To do this, use one of the following APIs from the :meth:`~Directive.run` method: |
| 143 |
|
| 144 |
* :py:meth:`.SphinxDirective.parse_content_to_nodes()` |
| 145 |
* :py:meth:`.SphinxDirective.parse_text_to_nodes()` |
| 146 |
|
| 147 |
The first method parses all the directive's content as markup, |
| 148 |
whilst the second only parses the given *text* string. |
| 149 |
Both methods return the parsed Docutils nodes in a list. |
| 150 |
|
| 151 |
The methods are used as follows: |
| 152 |
|
| 153 |
.. code-block:: python |
| 154 |
|
| 155 |
def run(self) -> list[Node]: |
| 156 |
# either |
| 157 |
parsed = self.parse_content_to_nodes() |
| 158 |
# or |
| 159 |
parsed = self.parse_text_to_nodes('spam spam spam') |
| 160 |
return parsed |
| 161 |
|
| 162 |
.. note:: |
| 163 |
|
| 164 |
The above utility methods were added in Sphinx 7.4. |
| 165 |
Prior to Sphinx 7.4, the following methods should be used to parse content: |
| 166 |
|
| 167 |
* ``self.state.nested_parse`` |
| 168 |
* :func:`sphinx.util.nodes.nested_parse_with_titles` -- this allows titles in |
| 169 |
the parsed content. |
| 170 |
|
| 171 |
.. code-block:: python |
| 172 |
|
| 173 |
def run(self) -> list[Node]: |
| 174 |
container = docutils.nodes.Element() |
| 175 |
# either |
| 176 |
nested_parse_with_titles(self.state, self.result, container, self.content_offset) |
| 177 |
# or |
| 178 |
self.state.nested_parse(self.result, self.content_offset, container) |
| 179 |
parsed = container.children |
| 180 |
return parsed |
| 181 |
|
| 182 |
To parse inline markup, |
| 183 |
use :py:meth:`~sphinx.util.docutils.SphinxDirective.parse_inline()`. |
| 184 |
This must only be used for text which is a single line or paragraph, |
| 185 |
and does not contain any structural elements |
| 186 |
(headings, transitions, directives, etc). |
| 187 |
|
| 188 |
.. note:: |
| 189 |
|
| 190 |
``sphinx.util.docutils.switch_source_input()`` allows changing |
| 191 |
the source (input) file during parsing content in a directive. |
| 192 |
It is useful to parse mixed content, such as in ``sphinx.ext.autodoc``, |
| 193 |
where it is used to parse docstrings. |
| 194 |
|
| 195 |
.. code-block:: python |
| 196 |
|
| 197 |
from sphinx.util.docutils import switch_source_input |
| 198 |
from sphinx.util.parsing import nested_parse_to_nodes |
| 199 |
|
| 200 |
# Switch source_input between parsing content. |
| 201 |
# Inside this context, all parsing errors and warnings are reported as |
| 202 |
# happened in new source_input (in this case, ``self.result``). |
| 203 |
with switch_source_input(self.state, self.result): |
| 204 |
parsed = nested_parse_to_nodes(self.state, self.result) |
| 205 |
|
| 206 |
.. deprecated:: 1.7 |
| 207 |
|
| 208 |
Until Sphinx 1.6, ``sphinx.ext.autodoc.AutodocReporter`` was used for this |
| 209 |
purpose. It is replaced by ``switch_source_input()``. |
| 210 |
|
| 211 |
|
| 212 |
.. _ViewLists: |
| 213 |
|
| 214 |
ViewLists and StringLists |
| 215 |
^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 216 |
|
| 217 |
Docutils represents document source lines in a ``StringList`` class, |
| 218 |
which inherits from ``ViewList``, both in the ``docutils.statemachine`` module. |
| 219 |
This is a list with extended functionality, |
| 220 |
including that slicing creates views of the original list and |
| 221 |
that the list contains information about source line numbers. |
| 222 |
|
| 223 |
The :attr:`Directive.content` attribute is a ``StringList``. |
| 224 |
If you generate content to be parsed as reStructuredText, |
| 225 |
you have to create a ``StringList`` for the Docutils APIs. |
| 226 |
The utility functions provided by Sphinx handle this automatically. |
| 227 |
Important for content generation are the following points: |
| 228 |
|
| 229 |
* The ``ViewList`` constructor takes a list of strings (lines) |
| 230 |
and a source (document) name. |
| 231 |
* The ``ViewList.append()`` method takes a line and a source name as well. |