@@ -94,46 +94,64 @@ using :meth:`.Sphinx.add_directive` or :meth:`.Sphinx.add_directive_to_domain`.
94
94
.. _Creating directives : https://docutils.sourceforge.io/docs/howto/rst-directives.html
95
95
96
96
97
- Parsing directive content as ReST
98
- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
97
+ .. _parsing-directive-content-as-rest :
99
98
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
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
102
101
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:
106
104
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() `
108
108
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).
114
130
115
131
.. note ::
116
132
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
120
139
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
122
142
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)
129
148
130
149
.. deprecated :: 1.7
131
150
132
151
Until Sphinx 1.6, ``sphinx.ext.autodoc.AutodocReporter `` was used for this
133
152
purpose. It is replaced by ``switch_source_input() ``.
134
153
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
137
155
138
156
139
157
ViewLists
0 commit comments