【发布时间】:2018-06-12 22:24:47
【问题描述】:
我想使用 Sphinx 的 autosummary extension 和 templates 从文档字符串递归生成 API 文档。我想要每个模块、类、方法、属性和函数的单独页面。但它根本没有检测到我的模板。事实上,如果我只是从_templates/autosummary/ 中删除module.rst 文件,它会以与以前完全相同的方式呈现整个文件。我已经关注this SO question 到这封信。如果你有兴趣,the full repository is on GitHub。
编辑:它似乎确实生成了一个不同的文件,我必须删除 docs/_autosummary 才能读取新模板。但是,现在它会生成一个带有sparse 标头和description 标头的文件。它不会进入 {% if classes %} 和 {% if functions %} 指令。
我的目录结构如下:
- 稀疏
- 文档
- conf.py
- index.rst
- modules.rst
- _templates/autosummary/module.rst
以下是目前的相关文件:
index.rst:
.. sparse documentation master file, created by
sphinx-quickstart on Fri Dec 29 20:58:03 2017.
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.
Welcome to sparse's documentation!
==================================
.. toctree::
:maxdepth: 2
:caption: Contents:
modules
Indices and tables
==================
* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`
modules.rst:
API Reference
=============
Modules
-------
.. autosummary::
:toctree: _autosummary
sparse
_templates/autosummary/module.rst:
{{ fullname | escape | underline }}
Description
-----------
.. automodule:: {{ fullname | escape }}
{% if classes %}
Classes
-------
.. autosummary:
:toctree: _autosummary
{% for class in classes %}
{{ class }}
{% endfor %}
{% endif %}
{% if functions %}
Functions
---------
.. autosummary:
:toctree: _autosummary
{% for function in functions %}
{{ function }}
{% endfor %}
{% endif %}
【问题讨论】:
-
你为什么不用sphinx-apidoc?
-
如果 sphinx-apidoc 可以做类似于我想要的事情(使用模板和成员摘要),那么我会的。不幸的是,我对 Sphinx 不太擅长,一个例子会很有用。但据我了解,sphinx-apidoc 相当于使用带有
autosummary_generate = True的自动摘要。 -
你试过什么?您是否阅读过 sphinx-apidoc 的文档,看看它是否满足您的需求?
-
@StevePiercy 经过一番摆弄,我让它工作了。请参阅下面的答案。
标签: python jinja2 python-sphinx restructuredtext toctree