python-sphinx - 如何在节点中为指令添加 rst 格式?

标签 python-sphinx restructuredtext docutils

如何在节点中使用 rst?例如我想输出包含文件 about.rst

class Foo(Directive):

    def run(self):
        return [
            nodes.Text("**adad**"),  # <-- Must be a bold text
            nodes.Text(".. include:: about.rst"),  # <-- Must include file
        ]

最佳答案

您可以构建一个 ViewList原始数据(每个条目一行),让 Sphinx 解析该内容,然后返回 Sphinx 给你的节点。以下对我有用:

from docutils import nodes
from docutils.statemachine import ViewList
from sphinx.util.compat import Directive
from sphinx.util.nodes import nested_parse_with_titles

class Foo(Directive):
    def run(self):
        rst = ViewList()

        # Add the content one line at a time.
        # Second argument is the filename to report in any warnings
        # or errors, third argument is the line number.            
        rst.append("**adad**", "fakefile.rst", 10)
        rst.append("", "fakefile.rst", 11)
        rst.append(".. include:: about.rst", "fakefile.rst", 12)

        # Create a node.
        node = nodes.section()
        node.document = self.state.document

        # Parse the rst.
        nested_parse_with_titles(self.state, rst, node)

        # And return the result.
        return node.children

def setup(app):
    app.add_directive('foo', Foo)

我不得不为一个项目做类似的事情——代替我使用 source of the inbuilt autodoc extension 的任何(很容易找到的)相关文档。作为指南。

关于python-sphinx - 如何在节点中为指令添加 rst 格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34350844/

相关文章:

python - 狮身人面像 : Link to a method of a class in another module in Python docstring

python - 在 Sphinx 文档中包含源代码

python-sphinx - 如何使用 sphinx 删除 reStructuredText 中代码块周围的框

python - NumpyDoc 检查是否符合文档规范?

python - 始终重新生成包含特定指令的 Sphinx 文档

sql - SQL的reStructuredText?

python - "module ' 区域设置 ' has no attribute ' 运行 python-sphinx 时规范化 '"

django - Django 中的 Sphinx 文档

python - 根据搜索字符串从多行文本中捕获一行

html - 为什么图像缩放在生成 html 文件时在 reStructuredText 中不起作用?