Skip to content

Commit 0aa6d0b

Browse files
committed
Rewrite "Parsing directive content as reStructuredText"
1 parent 49eeebf commit 0aa6d0b

File tree

1 file changed

+43
-25
lines changed

1 file changed

+43
-25
lines changed

doc/extdev/markupapi.rst

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -94,46 +94,64 @@ using :meth:`.Sphinx.add_directive` or :meth:`.Sphinx.add_directive_to_domain`.
9494
.. _Creating directives: https://docutils.sourceforge.io/docs/howto/rst-directives.html
9595

9696

97-
Parsing directive content as ReST
98-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
97+
.. _parsing-directive-content-as-rest:
9998

100-
Many directives will contain more markup that must be parsed. To do this, use
101-
one of the following APIs from the :meth:`Directive.run` method:
99+
Parsing directive content as reStructuredText
100+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
102101

103-
* ``self.state.nested_parse``
104-
* :func:`sphinx.util.nodes.nested_parse_with_titles` -- this allows titles in
105-
the parsed content.
102+
Many directives will contain more markup that must be parsed.
103+
To do this, use one of the following APIs from the :meth:`Directive.run` method:
106104

107-
Both APIs parse the content into a given node. They are used like this::
105+
* :py:meth:`~sphinx.util.docutils.SphinxDirective.parse_content_to_nodes()`
106+
* :py:meth:`~sphinx.util.docutils.SphinxDirective.parse_text_to_nodes()`
107+
* :py:func:`~sphinx.util.parsing.nested_parse_to_nodes()`
108108

109-
node = docutils.nodes.paragraph()
110-
# either
111-
nested_parse_with_titles(self.state, self.result, node)
112-
# or
113-
self.state.nested_parse(self.result, 0, node)
109+
The first method parses all the directive's content as markup,
110+
whilst the second only parses the given *text* string.
111+
Both methods return the parsed Docutils nodes in a list.
112+
113+
The methods are used as follows:
114+
115+
.. code-block:: python
116+
117+
def run(self) -> list[Node]:
118+
parsed = self.parse_content_to_nodes()
119+
# or
120+
parsed = self.parse_text_to_nodes('spam spam spam')
121+
# or
122+
parsed = nested_parse_to_nodes(self.state, 'spam spam spam')
123+
return parsed
124+
125+
To parse inline markup,
126+
use :py:meth:`~sphinx.util.docutils.SphinxDirective.parse_inline()`.
127+
This must only be used for text which is a single line or paragraph,
128+
and does not contain any structural elements
129+
(headings, transitions, directives, etc).
114130

115131
.. note::
116132

117-
``sphinx.util.docutils.switch_source_input()`` allows to change a target file
118-
during nested_parse. It is useful to mixed contents.
119-
For example, ``sphinx.ext.autodoc`` uses it to parse docstrings::
133+
``sphinx.util.docutils.switch_source_input()`` allows changing
134+
the source (input) file during parsing content in a directive.
135+
It is useful to parse mixed content, such as in ``sphinx.ext.autodoc``,
136+
where it is used to parse docstrings.
137+
138+
.. code-block:: python
120139
121-
from sphinx.util.docutils import switch_source_input
140+
from sphinx.util.docutils import switch_source_input
141+
from sphinx.util.parsing import nested_parse_to_nodes
122142
123-
# Switch source_input between parsing content.
124-
# Inside this context, all parsing errors and warnings are reported as
125-
# happened in new source_input (in this case, ``self.result``).
126-
with switch_source_input(self.state, self.result):
127-
node = docutils.nodes.paragraph()
128-
self.state.nested_parse(self.result, 0, node)
143+
# Switch source_input between parsing content.
144+
# Inside this context, all parsing errors and warnings are reported as
145+
# happened in new source_input (in this case, ``self.result``).
146+
with switch_source_input(self.state, self.result):
147+
parsed = nested_parse_to_nodes(self.state, self.result)
129148
130149
.. deprecated:: 1.7
131150

132151
Until Sphinx 1.6, ``sphinx.ext.autodoc.AutodocReporter`` was used for this
133152
purpose. It is replaced by ``switch_source_input()``.
134153

135-
If you don't need the wrapping node, you can use any concrete node type and
136-
return ``node.children`` from the Directive.
154+
.. autofunction:: sphinx.util.parsing.nested_parse_to_nodes
137155

138156

139157
ViewLists

0 commit comments

Comments
 (0)