网页 综合 更新时间 2026-11-17 02:46:24

Docutils markup API — Sphinx documentation

自动蜘蛛 www.sphinx-doc.org 正文约 4773 字
This section describes the API for adding reStructuredText markup elements (roles and directives).
Roles follow the interface described below. They have to be registered by an extension using Sphinx.add_role() or Sphinx.add_role_to_domain().
The options and content parameters are only used for custom roles created via the role directive. The return value is a tuple of two lists, the first containing the text nodes and elements from the role, and the second containing any system messages generated. For more information, see the custom role overview from Docutils.
Sphinx provides two base classes for creating custom roles, SphinxRole and ReferenceRole.
These provide a class-based interface for creating roles, where the main logic must be implemented in your run() method. The classes provide a number of useful methods and attributes, such as self.text, self.config, and self.env. The ReferenceRole class implements Sphinx’s title <target> logic, exposing self.target and self.title attributes. This is useful for creating cross-reference roles.
Directives are handled by classes derived from docutils.parsers.rst.Directive. They have to be registered by an extension using Sphinx.add_directive() or Sphinx.add_directive_to_domain().
The markup syntax of the new directive is determined by the follow five class attributes:
Number of required directive arguments.
Number of optional arguments after the required arguments.
May the final argument contain whitespace?
Mapping of option names to validator functions.
Option validator functions take a single parameter, the option argument (or None if not given), and should validate it or convert it to the proper form. They raise ValueError or TypeError to indicate failure.
There are several predefined and possibly useful validators in the docutils.parsers.rst.directives module.
May the directive have content?
New directives must implement the run() method:
This method must process the directive arguments, options and content, and return a list of Docutils/Sphinx nodes that will be inserted into the document tree at the point where the directive was encountered.
Instance attributes that are always set on the directive are:
The directive name (useful when registering the same directive class under multiple names).
The arguments given to the directive, as a list.
The options given to the directive, as a dictionary mapping option names to validated/converted values.
The directive content, if given, as a ViewList.
The absolute line number on which the directive appeared. This is not always a useful value; use srcline instead.
Internal offset of the directive content. Used when calling nested_parse (see below).
The string containing the entire directive.
The state and state machine which controls the parsing. Used for nested_parse.
Creating directives HOWTO of the Docutils documentation
Many directives will contain more markup that must be parsed. To do this, use one of the following APIs from the run() method:
SphinxDirective.parse_content_to_nodes()
SphinxDirective.parse_text_to_nodes()
The first method parses all the directive’s content as markup, whilst the second only parses the given text string. Both methods return the parsed Docutils nodes in a list.
The methods are used as follows:
The above utility methods were added in Sphinx 7.4. Prior to Sphinx 7.4, the following methods should be used to parse content:
self.state.nested_parse
sphinx.util.nodes.nested_parse_with_titles() – this allows titles in the parsed content.
To parse inline markup, use parse_inline(). This must only be used for text which is a single line or paragraph, and does not contain any structural elements (headings, transitions, directives, etc).
sphinx.util.docutils.switch_source_input() allows changing the source (input) file during parsing content in a directive. It is useful to parse mixed content, such as in sphinx.ext.autodoc, where it is used to parse docstrings.
Deprecated since version 1.7: Until Sphinx 1.6, sphinx.ext.autodoc.AutodocReporter was used for this purpose. It is replaced by switch_source_input().
Docutils represents document source lines in a StringList class, which inherits from ViewList, both in the docutils.statemachine module. This is a list with extended functionality, including that slicing creates views of the original list and that the list contains information about source line numbers.
The Directive.content attribute is a StringList. If you generate content to be parsed as reStructuredText, you have to create a StringList for the Docutils APIs. The utility functions provided by Sphinx handle this automatically. Important for content generation are the following points:
The ViewList constructor takes a list of strings (lines) and a source (document) name.
访问原文 返回搜索 https://www.sphinx-doc.org/en/master/extdev/markupapi.html
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。