递归使用Sphinx autosummary生成API文档

时间:2018-01-03 08:57:07

标签: python jinja2 python-sphinx restructuredtext

我想使用Sphinx的thisautosummary extension从docstrings中递归生成API文档。我想为每个模块,类,方法,属性和功能分别设置页面。但它根本检测不到我的模板。实际上,如果我只是从module.rst中删除_templates/autosummary/文件,它会以与以前完全相同的方式呈现整个文件。我跟着templates写了这封信。如果您有兴趣,this SO question

编辑:它似乎确实生成了一个不同的文件,我不得不删除docs / _autosummary来读取新模板。但是,现在它生成一个带有sparse标头和description标头的文件。它不会进入{% if classes %}{% if functions %}指令。

我的目录结构如下:

  • 稀疏
  • 文档
    • conf.py
    • index.rst
    • modules.rst
    • _templates /自动摘要/ 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 %}

2 个答案:

答案 0 :(得分:6)

我最终需要以下文件:

modules.rst

API Reference
=============

.. rubric:: Modules

.. autosummary::
   :toctree: generated

   sparse

_templates/autosummary/module.rst

{{ fullname | escape | underline }}

.. rubric:: Description

.. automodule:: {{ fullname }}

.. currentmodule:: {{ fullname }}

{% if classes %}
.. rubric:: Classes

.. autosummary::
    :toctree: .
    {% for class in classes %}
    {{ class }}
    {% endfor %}

{% endif %}

{% if functions %}
.. rubric:: Functions

.. autosummary::
    :toctree: .
    {% for function in functions %}
    {{ function }}
    {% endfor %}

{% endif %}

_templates/autosummary/class.rst

{{ fullname | escape | underline}}

.. currentmodule:: {{ module }}

.. autoclass:: {{ objname }}

   {% block methods %}
   {% block attributes %}
   {% if attributes %}
   .. HACK -- the point here is that we don't want this to appear in the output, but the autosummary should still generate the pages.
      .. autosummary::
         :toctree:
      {% for item in all_attributes %}
         {%- if not item.startswith('_') %}
         {{ name }}.{{ item }}
         {%- endif -%}
      {%- endfor %}
   {% endif %}
   {% endblock %}

   {% if methods %}
   .. HACK -- the point here is that we don't want this to appear in the output, but the autosummary should still generate the pages.
      .. autosummary::
         :toctree:
      {% for item in all_methods %}
         {%- if not item.startswith('_') or item in ['__call__'] %}
         {{ name }}.{{ item }}
         {%- endif -%}
      {%- endfor %}
   {% endif %}
   {% endblock %}

_templates/autosummary/base.rst

{{ fullname | escape | underline}}

.. currentmodule:: {{ module }}

.. auto{{ objtype }}:: {{ objname }}

我还需要转到sphinx/ext/autosummary/generate.py并在函数imported_members=True中设置generate_autosummary_docs

如果您没有像我这样使用numpydoc,则可能需要删除.. HACK指令。

答案 1 :(得分:6)

从Sphinx 3.1版(2020年6月)开始,您可以使用新的:recursive:选项来获取sphinx.ext.autosummary,以自动检测包中的每个模块(无论嵌套程度如何),并自动为每个属性生成文档,类,功能和异常。

在这里查看我的答案:https://stackoverflow.com/a/62613202/12014259