公用事业

Sphinx提供了实用程序类和函数来开发扩展。

组件的基类

这些基类对于允许您的扩展获取Sphinx组件非常有用。 ConfigBuildEnvironment 以此类推)。

备注

它们的子类可能不能与纯docutil一起工作,因为它们与Sphinx强耦合。

class sphinx.transforms.SphinxTransform(document, startnode=None)[源代码]

转换的基类。

与.相比 docutils.transforms.Transform ,这个类改进了对Sphinx API的可访问性。

property app: Sphinx

Sphinx 对象。

property config: Config

Config 对象。

property env: BuildEnvironment

BuildEnvironment 对象。

class sphinx.transforms.post_transforms.SphinxPostTransform(document, startnode=None)[源代码]

后期变换的基类。

调用后期转换来修改文档以重新构造文档以进行输出。它们解析引用、转换图像、对每种输出格式进行特殊转换等等。这个类帮助实现这些POST转换。

apply(**kwargs: Any) None[源代码]

覆盖以将转换应用于文档树。

is_supported() bool[源代码]

检查此转换在当前构建器中是否有效。

run(**kwargs: Any) None[源代码]

后置变换的主要方法。

子类应该重写此方法,而不是 apply()

class sphinx.util.docutils.SphinxDirective(name, arguments, options, content, lineno, content_offset, block_text, state, state_machine)[源代码]

Sphinx指令的基类。

此类为Sphinx指令提供帮助器方法。

Added in version 1.8.

备注

此类的子类可能不能与docutil一起使用。这个类与Sphinx强耦合。

get_location() str[源代码]

获取用于记录的当前位置信息。

Added in version 4.2.

get_source_info() tuple[str, int][源代码]

获取信号源和行号。

Added in version 3.0.

parse_content_to_nodes(allow_section_headings: bool = False) list[Node][源代码]

将指令的内容解析到节点中。

参数:

allow_section_headings -- 指令内容中是否允许出现标题(部分)?请注意,此选项绕过了Docutils对doctree结构的通常检查,并且滥用此选项可能会导致doctree不连贯。在Docutils中,节节点只能是 Structural 节点,其中包括 document , section ,而且 sidebar

Added in version 7.4.

parse_inline(text: str, *, lineno: int = -1) tuple[list[Node], list[system_message]][源代码]

解析 text 作为内联元素。

参数:
  • text -- 要分析的文本,应该是一行或一段。它不能包含任何结构元素(标题、过渡、指令等)。

  • lineno -- 解释文本开始的行号。

返回:

节点列表(文本和内联元素)和system_mailings列表。

Added in version 7.4.

parse_text_to_nodes(text: str = '', /, *, offset: int = -1, allow_section_headings: bool = False) list[Node][源代码]

解析 text 变成节点。

参数:
  • text -- 字符串形式的文本。 StringList 也被接受。

  • allow_section_headings -- 是否允许标题(部分) text ?请注意,此选项绕过了Docutils对doctree结构的通常检查,并且滥用此选项可能会导致doctree不连贯。在Docutils中,节节点只能是 Structural 节点,其中包括 document , section ,而且 sidebar

  • offset -- 内容的抵消。

Added in version 7.4.

set_source_info(node: Node) None[源代码]

将信号源和行号设置为节点。

Added in version 2.1.

property config: Config

Config 对象。

Added in version 1.8.

property env: BuildEnvironment

BuildEnvironment 对象。

Added in version 1.8.

class sphinx.util.docutils.SphinxRole[源代码]

Sphinx角色的基类。

此类为Sphinx角色提供帮助器方法。

Added in version 2.0.

备注

此类的子类可能不能与docutil一起使用。这个类与Sphinx强耦合。

get_location() str[源代码]

获取用于记录的当前位置信息。

Added in version 4.2.

property config: Config

Config 对象。

Added in version 2.0.

content: Sequence[str]

字符串列表,用于定制的指令内容(来自“Role”指令)。

property env: BuildEnvironment

BuildEnvironment 对象。

Added in version 2.0.

inliner: Inliner

这个 docutils.parsers.rst.states.Inliner 对象。

lineno: int

解释文本开始的行号。

name: str

文档中实际使用的角色名称。

options: dict[str, Any]

用于定制的指令选项词典(来自“角色”指令)。

rawtext: str

包含整个解释文本输入的字符串。

text: str

解释的文本内容。

class sphinx.util.docutils.ReferenceRole[源代码]

引用角色的基类。

参考角色可以接受 link title <target> 角色的文本样式。解析结果;链接标题和目标将存储到 self.titleself.target

Added in version 2.0.

disabled: bool

布尔值表示引用已禁用。

has_explicit_title: bool

布尔值表示角色是否具有明确的头衔。

target: str

解释文本的链接目标。

title: str

解释文本的链接标题。

class sphinx.transforms.post_transforms.images.ImageConverter(document, startnode=None)[源代码]

图像转换器的基类。

图像转换器是一种Docutils转换模块。它用于将构建器不支持的图像文件转换为该构建器的适当格式。

例如, LaTeX builder 支持PDF、PNG和JPEG作为图像格式。但是,它不支持SVG图像。对于这种情况,使用图像转换器可以将这些不受支持的图像嵌入到文档中。其中一个图像转换器; sphinx.ext.imgconverter 可以在内部使用Imagemagick将SVG图像转换为PNG格式。

制作自定义图像转换器有三个步骤:

  1. 使…成为…的子类 ImageConverter 班级

  2. 超覆 conversion_rulesis_available()convert()

  3. 使用以下命令将图像转换器注册到Sphinx Sphinx.add_post_transform()

convert(_from: str | PathLike[str], _to: str | PathLike[str]) bool[源代码]

将图像文件转换为预期的格式。

_from 是源图像文件的路径,并且 _to 是目标文件的路径。

is_available() bool[源代码]

返回图像转换器是否可用。

available: bool | None = None

转换器是否可用。将在生成的第一次调用时填充。结果在相同的过程中共享。

待处理

这应该被重构为不存储没有类变量的状态。

conversion_rules: list[tuple[str, str]] = []

图像转换器支持的转换规则。它表示为一对源图像格式(MIMETYPE)和目地One::

conversion_rules = [
    ('image/svg+xml', 'image/png'),
    ('image/gif', 'image/png'),
    ('application/pdf', 'image/png'),
]
default_priority = 200

此转换的数字优先级,从0到999(覆盖)。

实用程序组件

class sphinx.events.EventManager(app: Sphinx)[源代码]

Sphinx的事件经理。

add(name: str) None[源代码]

注册一个定制的Sphinx事件。

connect(name: Literal['config-inited'], callback: Callable[[Sphinx, Config], None], priority: int) int[源代码]
connect(name: Literal['builder-inited'], callback: Callable[[Sphinx], None], priority: int) int
connect(name: Literal['env-get-outdated'], callback: Callable[[Sphinx, BuildEnvironment, Set[str], Set[str], Set[str]], Sequence[str]], priority: int) int
connect(name: Literal['env-before-read-docs'], callback: Callable[[Sphinx, BuildEnvironment, list[str]], None], priority: int) int
connect(name: Literal['env-purge-doc'], callback: Callable[[Sphinx, BuildEnvironment, str], None], priority: int) int
connect(name: Literal['source-read'], callback: Callable[[Sphinx, str, list[str]], None], priority: int) int
connect(name: Literal['include-read'], callback: Callable[[Sphinx, Path, str, list[str]], None], priority: int) int
connect(name: Literal['doctree-read'], callback: Callable[[Sphinx, nodes.document], None], priority: int) int
connect(name: Literal['env-merge-info'], callback: Callable[[Sphinx, BuildEnvironment, Set[str], BuildEnvironment], None], priority: int) int
connect(name: Literal['env-updated'], callback: Callable[[Sphinx, BuildEnvironment], str], priority: int) int
connect(name: Literal['env-get-updated'], callback: Callable[[Sphinx, BuildEnvironment], Iterable[str]], priority: int) int
connect(name: Literal['env-check-consistency'], callback: Callable[[Sphinx, BuildEnvironment], None], priority: int) int
connect(name: Literal['write-started'], callback: Callable[[Sphinx, Builder], None], priority: int) int
connect(name: Literal['doctree-resolved'], callback: Callable[[Sphinx, nodes.document, str], None], priority: int) int
connect(name: Literal['missing-reference'], callback: Callable[[Sphinx, BuildEnvironment, addnodes.pending_xref, nodes.TextElement], nodes.reference | None], priority: int) int
connect(name: Literal['warn-missing-reference'], callback: Callable[[Sphinx, Domain, addnodes.pending_xref], bool | None], priority: int) int
connect(name: Literal['build-finished'], callback: Callable[[Sphinx, Exception | None], None], priority: int) int
connect(name: Literal['html-collect-pages'], callback: Callable[[Sphinx], Iterable[tuple[str, dict[str, Any], str]]], priority: int) int
connect(name: Literal['html-page-context'], callback: Callable[[Sphinx, str, str, dict[str, Any], nodes.document], str | None], priority: int) int
connect(name: Literal['linkcheck-process-uri'], callback: Callable[[Sphinx, str], str | None], priority: int) int
connect(name: Literal['object-description-transform'], callback: Callable[[Sphinx, str, str, addnodes.desc_content], None], priority: int) int
connect(name: Literal['autodoc-process-docstring'], callback: _AutodocProcessDocstringListener, priority: int) int
connect(name: Literal['autodoc-before-process-signature'], callback: Callable[[Sphinx, Any, bool], None], priority: int) int
connect(name: Literal['autodoc-process-signature'], callback: Callable[[Sphinx, Literal['module', 'class', 'exception', 'function', 'method', 'attribute'], str, Any, dict[str, bool], str | None, str | None], tuple[str | None, str | None] | None], priority: int) int
connect(name: Literal['autodoc-process-bases'], callback: Callable[[Sphinx, str, Any, dict[str, bool], list[str]], None], priority: int) int
connect(name: Literal['autodoc-skip-member'], callback: Callable[[Sphinx, Literal['module', 'class', 'exception', 'function', 'method', 'attribute'], str, Any, bool, dict[str, bool]], bool], priority: int) int
connect(name: Literal['todo-defined'], callback: Callable[[Sphinx, todo_node], None], priority: int) int
connect(name: Literal['viewcode-find-source'], callback: Callable[[Sphinx, str], tuple[str, dict[str, tuple[Literal['class', 'def', 'other'], int, int]]]], priority: int) int
connect(name: Literal['viewcode-follow-imported'], callback: Callable[[Sphinx, str, str], str | None], priority: int) int
connect(name: str, callback: Callable[..., Any], priority: int) int

将处理程序连接到特定事件。

disconnect(listener_id: int) None[源代码]

断开处理程序的连接。

emit(name: str, *args: Any, allowed_exceptions: tuple[type[Exception], ...] = ()) list[Any][源代码]

发出Sphinx事件。

emit_firstresult(name: str, *args: Any, allowed_exceptions: tuple[type[Exception], ...] = ()) Any[源代码]

发出Sphinx事件并返回第一个结果。

它返回第一个处理程序的结果,该处理程序没有返回 None

效用函数

sphinx.util.parsing.nested_parse_to_nodes(state: RSTState, text: str | StringList, *, source: str = '<generated text>', offset: int = 0, allow_section_headings: bool = True, keep_title_context: bool = False) list[Node][源代码]

解析 text 变成节点。

参数:
  • state -- 状态机状态。必须是的子集 RSTState .

  • text -- 字符串形式的文本。 StringList 也被接受。

  • source -- 文本的来源,创建新的时使用 StringList .

  • offset -- 内容的抵消。

  • allow_section_headings -- 是否允许标题(部分) text ?请注意,此选项绕过了Docutils对doctree结构的通常检查,并且滥用此选项可能会导致doctree不连贯。在Docutils中,节节点只能是 Structural 节点,其中包括 document , section ,而且 sidebar

  • keep_title_context -- 如果为False(默认),那么 content 就像它是一个独立的文档一样进行解析,这意味着标题装饰(例如强调线)不需要与周围的文档相匹配。当解析的内容来自完全不同的上下文(例如文档字符串)时,这很有用。如果为True,那么标题强调线必须与周围文档中的强调线相匹配,否则行为未定义。

Added in version 7.4.

公用事业类型

class sphinx.util.typing.ExtensionMetadata[源代码]

扩展返回的元数据 setup() 功能

看到 扩展元数据 .

env_version: int

标识扩展添加的dev数据版本的一个整数。

parallel_read_safe: bool

指定扩展是否支持并行读取源文件。

parallel_write_safe: bool

指定扩展是否支持并行写入输出文件(默认: True ).

version: str

扩展版本(默认: 'unknown version' ).