让 Sphinx 从 pydoc 生成 RST 类文档
我目前正在将所有现有(不完整)文档迁移到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
autodoc 确实会生成 RST,只是没有官方方法可以从中获取它。获取它的最简单的方法是更改 sphinx.ext.autodoc.Documenter.add_line 方法以向我发出它所获取的行。
由于我想要的只是一次迁移,输出到 stdout 对我来说已经足够了:
现在 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:
Now autodoc prints generated RST on stdout while running and you can simply redirect or copy it elsewhere.
猴子修补自动文档,使其无需编辑任何内容即可工作:
monkey patching autodoc so it works without needing to edit anything:
据我所知,没有自动化工具可以做到这一点。因此,我的方法是编写一个小脚本来读取相关模块(基于 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).