gpt4 book ai didi

python - docutils/sphinx 自定义指令创建兄弟部分而不是子部分

转载 作者:行者123 更新时间:2023-11-28 17:43:46 25 4
gpt4 key购买 nike

考虑具有此框架的 reStructuredText 文档:

Main Title
==========

text text text text text

Subsection
----------

text text text text text

.. my-import-from:: file1
.. my-import-from:: file2

my-import-from 指令由文档特定的 Sphinx 扩展提供,它应该读取作为其参数提供的文件,解析嵌入其中的 reST,并将结果注入(inject)为当前输入文件中的一个部分。 (类似于 autodoc,但文件格式不同。)我现在拥有的代码如下所示:

class MyImportFromDirective(Directive):
required_arguments = 1
def run(self):
src, srcline = self.state_machine.get_source_and_line()
doc_file = os.path.normpath(os.path.join(os.path.dirname(src),
self.arguments[0]))
self.state.document.settings.record_dependencies.add(doc_file)
doc_text = ViewList()

try:
doc_text = extract_doc_from_file(doc_file)
except EnvironmentError as e:
raise self.error(e.filename + ": " + e.strerror) from e

doc_section = nodes.section()
doc_section.document = self.state.document

# report line numbers within the nested parse correctly
old_reporter = self.state.memo.reporter
self.state.memo.reporter = AutodocReporter(doc_text,
self.state.memo.reporter)
nested_parse_with_titles(self.state, doc_text, doc_section)
self.state.memo.reporter = old_reporter

if len(doc_section) == 1 and isinstance(doc_section[0], nodes.section):
doc_section = doc_section[0]

# If there was no title, synthesize one from the name of the file.
if len(doc_section) == 0 or not isinstance(doc_section[0], nodes.title):
doc_title = nodes.title()
doc_title.append(make_title_text(doc_file))
doc_section.insert(0, doc_title)

return [doc_section]

这是可行的,除了新部分是作为当前部分的注入(inject)的,而不是兄弟。换句话说,上面的示例文档生成了一个像这样的 TOC 树:

  • Main Title
    • Subsection
      • File1
      • File2

而不是想要的

  • Main Title
    • Subsection
    • File1
    • File2

我该如何解决这个问题? Docutils 文档……不充分,尤其是在控制部分深度方面。我尝试过的一件明显的事情是返回 doc_section.children 而不是 [doc_section];从 TOC 树中完全删除 File1File2(但确实使文档正文中的节标题出现是为了正确嵌套级别)。

最佳答案

我认为不可能通过从指令中返回该部分来做到这一点(不按照 Florian 建议的方式进行操作),因为它将附加到“当前”部分。但是,您可以通过 self.state.section 添加该部分,就像我在下面所做的那样(为简洁起见删除了选项的处理)

class FauxHeading(object):
"""
A heading level that is not defined by a string. We need this to work with
the mechanics of
:py:meth:`docutils.parsers.rst.states.RSTState.check_subsection`.

The important thing is that the length can vary, but it must be equal to
any other instance of FauxHeading.
"""

def __init__(self, length):
self.length = length

def __len__(self):
return self.length

def __eq__(self, other):
return isinstance(other, FauxHeading)


class ParmDirective(Directive):

required_arguments = 1
optional_arguments = 0
has_content = True
option_spec = {
'type': directives.unchanged,
'precision': directives.nonnegative_int,
'scale': directives.nonnegative_int,
'length': directives.nonnegative_int}

def run(self):
variableName = self.arguments[0]
lineno = self.state_machine.abs_line_number()
secBody = None
block_length = 0

# added for some space
lineBlock = nodes.line('', '', nodes.line_block())

# parse the body of the directive
if self.has_content and len(self.content):
secBody = nodes.container()
block_length += nested_parse_with_titles(
self.state, self.content, secBody)

# keeping track of the level seems to be required if we want to allow
# nested content. Not sure why, but fits with the pattern in
# :py:meth:`docutils.parsers.rst.states.RSTState.new_subsection`
myLevel = self.state.memo.section_level
self.state.section(
variableName,
'',
FauxHeading(2 + len(self.options) + block_length),
lineno,
[lineBlock] if secBody is None else [lineBlock, secBody])
self.state.memo.section_level = myLevel

return []

关于python - docutils/sphinx 自定义指令创建兄弟部分而不是子部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20912467/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com