让 Sphinx 从 pydoc 生成 RST 类文档

发布于 2024-08-29 06:17:01 字数 521 浏览 7 评论 0原文

我目前正在将所有现有(不完整)文档迁移到 Sphinx

问题是文档使用 Python docstrings (该模块是用 C 编写的,但它可能确实没关系)并且类文档必须转换为可用于 Sphinx 的形式。

sphinx.ext.autodoc,但它会自动将当前文档字符串放入文档中。我想根据当前文档字符串在 (RST) 中生成源文件,然后我可以对其进行编辑并手动改进。

如何将文档字符串转换为 Sphinx 的 RST?

I'm currently migrating all existing (incomplete) documentation to Sphinx.

The problem is that the documentation uses Python docstrings (the module is written in C, but it probably does not matter) and the class documentation must be converted into a form usable for Sphinx.

There is sphinx.ext.autodoc, but it automatically puts current docstrings to the document. I want to generate a source file in (RST) based on current docstrings, which I could then edit and improve manually.

How would you transform docstrings into RST for Sphinx?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

長街聽風 2024-09-05 06:17:01

autodoc 确实会生成 RST,只是没有官方方法可以从中获取它。获取它的最简单的方法是更改​​ sphinx.ext.autodoc.Documenter.add_line 方法以向我发出它所获取的行。

由于我想要的只是一次迁移,输出到 stdout 对我来说已经足够了:

def add_line(self, line, source, *lineno):
    """Append one line of generated reST to the output."""
    print(self.indent + line)
    self.directive.result.append(self.indent + line, source, *lineno)

现在 autodoc 在运行时在 stdout 上打印生成的 RST,您可以简单地将其重定向或复制到其他地方。

The autodoc does generate RST only there is no official way to get it out of it. The easiest hack to get it was by changing sphinx.ext.autodoc.Documenter.add_line method to emit me the line it gets.

As all I want is one time migration, output to stdout is good enough for me:

def add_line(self, line, source, *lineno):
    """Append one line of generated reST to the output."""
    print(self.indent + line)
    self.directive.result.append(self.indent + line, source, *lineno)

Now autodoc prints generated RST on stdout while running and you can simply redirect or copy it elsewhere.

少女七分熟 2024-09-05 06:17:01

猴子修补自动文档,使其无需编辑任何内容即可工作:

import sphinx.ext.autodoc
rst = []
def add_line(self, line, source, *lineno):
    """Append one line of generated reST to the output."""
    rst.append(line)
    self.directive.result.append(self.indent + line, source, *lineno)
sphinx.ext.autodoc.Documenter.add_line = add_line
try:
    sphinx.main(['sphinx-build', '-b', 'html', '-d', '_build/doctrees', '.', '_build/html'])
except SystemExit:
    with file('doc.rst', 'w') as f:
        for line in rst:
            print >>f, line

monkey patching autodoc so it works without needing to edit anything:

import sphinx.ext.autodoc
rst = []
def add_line(self, line, source, *lineno):
    """Append one line of generated reST to the output."""
    rst.append(line)
    self.directive.result.append(self.indent + line, source, *lineno)
sphinx.ext.autodoc.Documenter.add_line = add_line
try:
    sphinx.main(['sphinx-build', '-b', 'html', '-d', '_build/doctrees', '.', '_build/html'])
except SystemExit:
    with file('doc.rst', 'w') as f:
        for line in rst:
            print >>f, line
岁月静好 2024-09-05 06:17:01

据我所知,没有自动化工具可以做到这一点。因此,我的方法是编写一个小脚本来读取相关模块(基于 sphinc.ext.autodoc)并将文档字符串放入文件中(适当格式化)。

As far as I know there are no automated tools to do this. My approach would therefore be to write a small script that reads relevant modules (based on sphinc.ext.autodoc) and throws doc strings into a file (formatted appropriately).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文