【问题标题】:Including docstring in Sphinx Documentation在 Sphinx 文档中包含文档字符串
【发布时间】:2011-10-19 17:14:08
【问题描述】:

我想在我的 Sphinx 文档中仅包含特定函数的文档字符串。但是,似乎没有选项可以使用 http://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html 仅显示这些详细信息而无需关联的类和函数定义

我已经尝试创建一个如Show *only* docstring in Sphinx documentation? 中所述的类,但我不确定它如何适合模板。

我还尝试了 autodoc-process-docstring 事件处理程序,但没有成功。

所以而不是我的文档显示(目前):

class module.MyClass(param)

    This is the class doc string

    my_method()

        This is my method doc string

我只想显示:

This is my method doc string

我当前在 .txt 文件中的模板是:

.. autoclass:: module.MyClass
    :members: my_method

【问题讨论】:

    标签: python python-sphinx autodoc


    【解决方案1】:

    查看源代码并进行实验后 - 以下是在 Sphinx 1.1 中的操作方法。

    在您的 conf.py 文件中创建一个新的 MethodDocumenter 子类。在这里您可以设置一个新的“objtype”,确保文档字符串没有缩进,并删除标题。

    from sphinx.ext import autodoc
    
    class SimpleDocumenter(autodoc.MethodDocumenter):
        objtype = "simple"
    
        #do not indent the content
        content_indent = ""
    
        #do not add a header to the docstring
        def add_directive_header(self, sig):
            pass
    

    然后确保将其添加到具有以下功能的可用文档中(再次在 conf.py 中):

    def setup(app):
        app.add_autodocumenter(SimpleDocumenter)
    

    然后,当您只想显示方法的文档字符串时,请在 .txt 或 .rst 文件中使用以下格式。只需在您的 objname 前加上 auto。

    .. autosimple:: mod.MyClass.my_method
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-27
      相关资源
      最近更新 更多