我正在考虑使用Sphinx为一个非python项目(SKILL,尽管这里不相关)生成文档。大多数函数都有docstring,所以作为第一种方法,我决定创建一个自定义解析器,使用提供的docstring从文件中记录 * 过程 *。
下面是我当前的解析器结构,我也提供了in this other answer:
from docutils import nodes
from sphinx.parsers import Parser
class MySkillParser(Parser):
supported = ("skill",) # Declare supported languages
def parse(self, inputstring, document):
# Here, parse the document contained in "inputstring" and dynamically generate content
txt = nodes.Text('HI')
document += txt
def setup(app): # This is called when loading the extension
app.add_source_suffix('.il', 'skill')
app.add_source_parser(MySkillParser)
return {'version': '0.1'}
字符串
除了在添加节点时遇到一些困难之外,这似乎工作得很好,除了以下警告:
第一个月
当在其他文档的目录中引用myfile
时,例如索引,就会发生这种情况。myfile.il
会被解析,并通过解析器生成一个html文件(使用上面的代码,它将包含“HI”)。但是,sphinx不知道文档标题,所以它拒绝生成目录条目。
我的问题很简单:如何让sphinx知道文档的名称?
这类似于以下问题:
然而,这些并不引用自定义解析器,而是使用现有的 rst 或 md 解析器。
Sphinx文档指出:
文件顶部附近的字段列表通常会被docutils解析为docinfo并显示在页面上。然而,在Sphinx中,位于任何其他标记之前的字段列表会作为文档元数据从docinfo移动到Sphinx环境中,并且不会显示在输出中。
所以 * 我认为 * 解析器需要修改sphinx环境,但具体是什么呢?根据sphinx源代码和一些文档,我尝试了多种方法,独立地或组合地,都无济于事:
title = "Document title"
def parse(self, inputstring, document):
# The following was also tried with self.env.get_doctree(self.env.docname) instead of "document"
document.settings.title = titletxt
document.settings._title = titletxt # Inspired from https://github.com/sphinx-doc/sphinx/blob/35965903177c6ed9a6afb62ccd33243a746a3fc0/sphinx/builders/latex/__init__.py#L314C20-L314C20
document['docname'] = titletxt # from https://github.com/sphinx-doc/sphinx/blob/35965903177c6ed9a6afb62ccd33243a746a3fc0/sphinx/builders/latex/__init__.py#L347
document['contentsname'] = titletxt # from https://github.com/sphinx-doc/sphinx/blob/35965903177c6ed9a6afb62ccd33243a746a3fc0/sphinx/builders/latex/__init__.py#L306C17-L306C40
titlenode = nodes.title('Hi', nodes.Text('Title node'))
document += titlenode
self.env.titles[self.env.docname] = titlenode
self.env.longtitles[self.env.docname] = titlenode
self.env.tocs[self.env.docname] = titlenode # Less sure about this one
document.title = 'another futile attempt'
# Also tried: parse a rst string into the document with docutils.core.publish_doctree(rst)
型
经过多次尝试后,我决定在这里询问。在这一点上,我不确定是否解析器负责设置标题,或者是否应该在其他地方设置。
1条答案
按热度按时间bogh5gae1#
经过大量痛苦的调试,结果发现无论使用哪种解析器,sphinx都会提取文档的第一部分并将其用作元数据源。
因此,需要添加必要的信息,例如文档标题作为文档对象中的一个部分:
字符串
空
test.il
页面的相应输出:的数据
请注意,该页面列在导航侧边栏中。它也可以从其他页面中的
toc
元素引用。