在Sphinx文档中向交互式Python控制台块添加输出?

2024-05-14 15:33:30 发布

您现在位置:Python中文网/ 问答频道 /正文

如何将输出添加到Sphinx文档中的交互式Python控制台块?在

例如,我可以做一些类似的事情:

First set the variable::

    >>> x = 1

Then print the variable::

    >>> print x

并让Sphinx自动将print x的输出插入到文档中?在

我试过:

还有别的办法吗?在


Tags: the文档orgpypisphinxipythonsphinxcontrib事情
2条回答

为Sphinx编写扩展的文档位于http://sphinx-doc.org/extdev/index.html#dev-extensions

我唯一的提示是使用self.state.nested_parse(..)可以省去很多麻烦。这里是我拥有的最简单的扩展:

class SvnRevisionDirective(Directive):
    """Directive to display subversion revision of the path.
    """
    has_content = True
    required_arguments = 1
    optional_arguments = 1
    final_argument_whitespace = False
    option_spec = {}

    def run(self):
        path = self.arguments[0]
        rev = svntools.Revision(path)   # uses subprocess etc.
        paragraph = nodes.paragraph()
        self.state.nested_parse(
            StringList([
                '**Revision:** r%d' % rev   # you can use regular rst syntax here(!)
            ]), 0, paragraph)
        return [paragraph]

...

def setup(app):
    ...
    app.add_directive('svnrevision', SvnRevisionDirective)

它的用法是

^{pr2}$

相关问题 更多 >

    热门问题