解析器API¶
The docutils documentation describes 解析器如下:
解析器分析输入文档并创建节点树表示。
在Sphinx中,解析器模块与docutils的工作方式相同。 解析器通过使用Application API的扩展注册到Sphinx; Sphinx.add_source_suffix() 和 Sphinx.add_source_parser() 。
source suffix 是从文件后缀到文件类型的映射。例如,.rst 文件被映射到 'restructuredtext' 类型。 Sphinx使用文件类型从注册列表中查找解析器。 在搜索时,Sphinx引用 Parser.supported 属性并选取一个包含属性中文件类型的解析器。
用户可以使用 source_suffix 覆盖源后缀映射,如下所示:
# a mapping from file suffix to file types
source_suffix = {
'.rst': 'restructuredtext',
'.md': 'markdown',
}
您应该指出解析器支持的文件类型。这将允许用户适当地配置他们的设置。
-
class
sphinx.parsers.Parser[源代码]¶ 源类解析器的基类。额外的解析器应该继承这个类而不是
docutils.parsers.Parser。 与docutils.parsers.Parser相比,该类提高了Sphinx API的可访问性。子类可以访问以下对象和函数:
- self.app
应用程序对象 (
sphinx.application.Sphinx)- self.config
配置对象(
sphinx.config.Config)- self.env
- self.warn()
发出警告。 (与
sphinx.application.Sphinx.warn()一样)- self.info()
发出信息性消息。 (与
sphinx.application.Sphinx.info()一样)
1.6 版后已移除:
warn()和info()已弃用。 使用sphinx.util.logging代替.