Sphinx,最佳实践
我刚刚开始使用 Sphinx 工具为我的代码生成文档。但我有点困惑,因为它并不像我想象的那么容易。我使用以下命令创建 Sphinx 文档:
sphinx-quickstart
然后在“source”文件夹中创建 *.rst 文件。似乎我需要为每个要为其创建文档的模块创建一个 *.rst 文件。对于 test.py,我创建 test.rst。在 test.rst 中,我有:
.. automodule:: test
:members:
:show-inheritance:
然后在 test.py 中,我有:
"""
.. module:: test
:platform: Unix, Windows
:synopsis: A useful module indeed.
"""
然后我使用以下方法生成文档:
sphinx-build -b html source/ build/
一切都按预期工作,但问题是它并不像我预期的那么容易。我想一定有一种更简单的方法可以跳过其中一些步骤。我想知道是否有任何方法可以为包内的所有模块生成文档,而不是为每个模块生成 *.rst 文件。
谢谢。
I just started to use Sphinx tool to generate a documentation for my code. But I'm a bit confused because it's not as easy as I expected. I create the Sphinx doc using:
sphinx-quickstart
and then I create my *.rst files into the "source" folder. Seems like I need to create a *.rst file for each module I want to create a document for. For test.py, I create test.rst. Inside test.rst, I have:
.. automodule:: test
:members:
:show-inheritance:
Then inside test.py, I have:
"""
.. module:: test
:platform: Unix, Windows
:synopsis: A useful module indeed.
"""
Then I generate the documentation using:
sphinx-build -b html source/ build/
Everything works as expected, but the problem is that it's not as easy as I expected. I guess that there must be an easier way to do it with skipping some of these steps. I wonder if there is any way to generate a documentation for all the modules inside a package instead of generating a *.rst file for each module.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
没有更简单的方法了。 Sphinx 不是像 epydoc 那样的 API 文档生成器,而是专注于手写文档。因此,您需要手写大量文档。
优点是,您还可以编写 API 文档之外的文档(例如教程、使用指南,甚至最终用户文档),并且您可以逻辑地构建 API 文档,而不仅仅是可用对象的简单字母顺序列表。如果做得正确的话,这样的文档通常更容易理解并且更容易使用。
查看知名项目的文档(例如 Werkzeug 或 Sphinx 本身)查看一些最佳实践。
There is no easier way. Sphinx is not an API doc generator like epydoc, but instead focuses on handwritten documentation. Consequently you need to write a lot of the documents by hand.
The advantage is, that you can also write documentation beyond API docs (e.g. tutorials, usage guides, even end user documentation), and that you can structure API documentation logically, beyond just a simple alphabetical listing of available objects. Such documentation is generally a lot easier to comprehend and a lot easier to use, if done correctly.
Take a look at the documentation of well-known projects (e.g. Werkzeug or Sphinx itself) to see some best practices.
快速记录应用程序的一种简单方法是像往常一样将文档字符串写入类和方法中,然后根据需要在 .rst 文件中补充它们。
template.rst:
myapp/lib/template.py:
One simple way to document your application quickly is to just write docstrings into classes and methods as per usual, and then complement them if required in the .rst files.
template.rst:
myapp/lib/template.py:
您可以使用 sphinx-apidoc 为您创建 rst 文件。
运行输出示例:
sphinx-apidoc 不会修改现有文件,您可以使用
-f
标志强制覆盖。You can use
sphinx-apidoc
to create therst
files for you.Example run output:
sphinx-apidoc won't modify existing files, you can force overwrite with the
-f
flag.