在Sphinx文档中为交互式Python控制台块添加输出?
我该如何在Sphinx文档中添加交互式Python控制台代码块的输出呢?
比如说,我想这样做:
First set the variable::
>>> x = 1
Then print the variable::
>>> print x
然后让Sphinx自动把print x
的输出插入到文档里?
我试过:
sphinxcontrib-autorun
https://pypi.python.org/pypi/sphinxcontrib-autorun/0.1-20140415 — 但是它在不同的解释器中运行每个代码块。IPython.sphinxext.ipython_directive
— 但是它强制使用IPython的语法,而不是“传统”的Python控制台。
还有其他方法可以做到这一点吗?
2 个回答
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)
它的用法是:
.. snvrevision: 'my/file.py'
3
好的,我已经在这里做了一个这样的东西: https://bitbucket.org/wolever/sphinx-contrib/src/tip/autorun2/?at=default