从 Sphinx 版本 3.1(2020 年 6 月)开始,sphinx.ext.autosummary
(终于!)具有自动递归。
因此,不再需要硬编码模块名称或依赖Sphinx AutoAPI或Sphinx AutoPackageSummary等第三方库来进行自动包检测。
Python 3.7 包文档示例(参见 Github 上的代码和ReadTheDocs上的结果):
mytoolbox
|-- mypackage
| |-- __init__.py
| |-- foo.py
| |-- mysubpackage
| |-- __init__.py
| |-- bar.py
|-- doc
| |-- source
| |--index.rst
| |--conf.py
| |-- _templates
| |-- custom-module-template.rst
| |-- custom-class-template.rst
conf.py
:
import os
import sys
sys.path.insert(0, os.path.abspath('../..')) # Source code dir relative to this file
extensions = [
'sphinx.ext.autodoc', # Core library for html generation from docstrings
'sphinx.ext.autosummary', # Create neat summary tables
]
autosummary_generate = True # Turn on sphinx.ext.autosummary
# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
index.rst
(注意新:recursive:
选项):
Welcome to My Toolbox
=====================
Some words.
.. autosummary::
:toctree: _autosummary
:template: custom-module-template.rst
:recursive:
mypackage
这足以自动总结包中的每个模块,无论嵌套多么深。对于每个模块,它会总结该模块中的每个属性、函数、类和异常。
但奇怪的是,默认sphinx.ext.autosummary
模板不会继续为每个属性、函数、类和异常生成单独的文档页面,并从汇总表链接到它们。可以扩展模板来执行此操作,如下所示,但我不明白为什么这不是默认行为 - 这肯定是大多数人想要的......?我已将其作为功能请求提出。
我必须在本地复制默认模板,然后添加到它们:
- 复制
site-packages/sphinx/ext/autosummary/templates/autosummary/module.rst
到mytoolbox/doc/source/_templates/custom-module-template.rst
- 复制
site-packages/sphinx/ext/autosummary/templates/autosummary/class.rst
到mytoolbox/doc/source/_templates/custom-class-template.rst
使用选项的钩子custom-module-template.rst
在index.rst
上面:template:
。(删除该行以查看使用默认站点包模板会发生什么。)
custom-module-template.rst
(右侧注明的附加行):
{{ fullname | escape | underline}}
.. automodule:: {{ fullname }}
{% block attributes %}
{% if attributes %}
.. rubric:: Module Attributes
.. autosummary::
:toctree: <-- add this line
{% for item in attributes %}
{{ item }}
{%- endfor %}
{% endif %}
{% endblock %}
{% block functions %}
{% if functions %}
.. rubric:: {{ _('Functions') }}
.. autosummary::
:toctree: <-- add this line
{% for item in functions %}
{{ item }}
{%- endfor %}
{% endif %}
{% endblock %}
{% block classes %}
{% if classes %}
.. rubric:: {{ _('Classes') }}
.. autosummary::
:toctree: <-- add this line
:template: custom-class-template.rst <-- add this line
{% for item in classes %}
{{ item }}
{%- endfor %}
{% endif %}
{% endblock %}
{% block exceptions %}
{% if exceptions %}
.. rubric:: {{ _('Exceptions') }}
.. autosummary::
:toctree: <-- add this line
{% for item in exceptions %}
{{ item }}
{%- endfor %}
{% endif %}
{% endblock %}
{% block modules %}
{% if modules %}
.. rubric:: Modules
.. autosummary::
:toctree:
:template: custom-module-template.rst <-- add this line
:recursive:
{% for item in modules %}
{{ item }}
{%- endfor %}
{% endif %}
{% endblock %}
custom-class-template.rst
(右侧注明的附加行):
{{ fullname | escape | underline}}
.. currentmodule:: {{ module }}
.. autoclass:: {{ objname }}
:members: <-- add at least this line
:show-inheritance: <-- plus I want to show inheritance...
:inherited-members: <-- ...and inherited members too
{% block methods %}
.. automethod:: __init__
{% if methods %}
.. rubric:: {{ _('Methods') }}
.. autosummary::
{% for item in methods %}
~{{ name }}.{{ item }}
{%- endfor %}
{% endif %}
{% endblock %}
{% block attributes %}
{% if attributes %}
.. rubric:: {{ _('Attributes') }}
.. autosummary::
{% for item in attributes %}
~{{ name }}.{{ item }}
{%- endfor %}
{% endif %}
{% endblock %}