在Sphinx文档中包含docstring [英] Including docstring in Sphinx Documentation

查看:142
本文介绍了在Sphinx文档中包含docstring的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



尝试按照在Sphinx文档中仅显示* docstring概述的方式创建类。但我不确定这与模板是否相符。



我也尝试过autodoc-process-docstring事件处理程序,但是没有运气。



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

  class module.MyClass(param)

这是类文档字符串

my_method()

这是我的方法文档字符串

我只想显示:

 这是我的方法文档字符串

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

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


解决方案

在查看了源代码并进行了实验之后-这是在狮身人面像1.1中的操作方法。 / p>

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

 从sphinx.ext导入autodoc 

类SimpleDocumenter(autodoc.MethodDocumenter):
objtype =简单

#不缩进内容
content_indent =

#不向文件字符串添加标题
def add_directive_header (self,sig):
通过

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

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

然后,当您只想显示方法的文档字符串时,请在.txt或.rst文件。只需在objname前面加上auto即可。

  .. autosimple :: mod.MyClass.my_method 


I'd like to include just the docstring of a specific function in my Sphinx documentation. However there seem to be no options to just display these details without associated class and function definitions using http://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html

I've tried creating a class as outlined in Show *only* docstring in Sphinx documentation? but I'm not sure how this fits in with the templating.

I've also tried the autodoc-process-docstring event handler with no luck.

So rather than my documentation displaying (as it is currently):

class module.MyClass(param)

    This is the class doc string

    my_method()

        This is my method doc string

I just want to display:

This is my method doc string

My current template in a .txt file is:

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

解决方案

After looking through the source and experimenting - here is how to do it in Sphinx 1.1.

In your conf.py file create a new MethodDocumenter subclass. Here you can set a new "objtype", make sure the docstring is not indented, and remove the title.

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

Then make sure this is added to the available documenters with the following function (again in conf.py):

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

Then when you just want to display a method's docstring use the following format in your .txt or .rst files. Just prefix your objname with auto.

.. autosimple:: mod.MyClass.my_method

这篇关于在Sphinx文档中包含docstring的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆