首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >递归地使用Sphinx自动摘要生成API文档

递归地使用Sphinx自动摘要生成API文档
EN

Stack Overflow用户
提问于 2018-01-03 08:57:06
回答 2查看 15.4K关注 0票数 23

我想使用Sphinx的自动摘要扩展模板从docstring递归地生成API文档。我想要每个模块、类、方法、属性和函数的单独页面。但它根本没有检测到我的模板。实际上,如果我只是将module.rst文件从_templates/autosummary/中删除,那么整个文件的呈现方式与以前完全相同。我一直跟着这个问题去信。如果你感兴趣,完整的存储库在GitHub上

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

我的目录结构如下:

  • 稀疏
  • docs
    • conf.py
    • index.rst
    • modules.rst
    • 模板/自动摘要/模块.

以下是到目前为止的相关文件:

index.rst

代码语言:javascript
复制
.. 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

代码语言:javascript
复制
API Reference
=============

Modules
-------

.. autosummary::
   :toctree: _autosummary

   sparse

_templates/autosummary/module.rst

代码语言:javascript
复制
{{ 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 %}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-06-29 11:17:52

从SphinxVersion3.1(2020年6月)中,您可以使用新的:recursive:选项获得sphinx.ext.autosummary,以自动检测包中的每个模块,无论嵌套程度如何,并自动为该模块中的每个属性、类、函数和异常生成文档。

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

票数 14
EN

Stack Overflow用户

发布于 2018-01-03 17:24:19

最后,我需要以下文件:

modules.rst

代码语言:javascript
复制
API Reference
=============

.. rubric:: Modules

.. autosummary::
   :toctree: generated

   sparse

_templates/autosummary/module.rst

代码语言:javascript
复制
{{ 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

代码语言:javascript
复制
{{ 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

代码语言:javascript
复制
{{ fullname | escape | underline}}

.. currentmodule:: {{ module }}

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

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

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

票数 13
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48074094

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档