在 Sphinx 文档中包含文档字符串

发布于 2024-12-10 23:52:14 字数 845 浏览 0 评论 0 原文

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

我已经尝试创建一个类,如在 Sphinx 文档中显示 *仅* 文档字符串? 但我不确定这如何适合与模板一起使用。

我还尝试了 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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

那请放手 2024-12-17 23:52:14

在查看了源代码并进行了实验之后 - 以下是如何在 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

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
假扮的天使 2024-12-17 23:52:14

我在 Sphinx 5.3 中使用了这种方法。

如果您不想覆盖类 API 文档的默认 MethodDocumenter,则还需要覆盖以下 can_document_member 并将其设置为 False。生成的类如下所示

class SimpleDocumenter(autodoc.MethodDocumenter):
"""
Reference a class or method docstring only.
see https://stackoverflow.com/a/7832437/5726546
"""
  objtype = "simple"

    content_indent = ""

    @classmethod
    def can_document_member(cls, member: Any, membername: str, isattr: bool, parent: Any) -> bool:
        return False

    # do not add a header to the docstring
    def add_directive_header(self, sig: str) -> None:
        pass

设置和指令与 geographika 的回答中的相同。

I used this approach with Sphinx 5.3.

If you don't want to override the default MethodDocumenter for your class API documentation, you need to also override the following can_document_member and set it to False. The resulting class looks as follows

class SimpleDocumenter(autodoc.MethodDocumenter):
"""
Reference a class or method docstring only.
see https://stackoverflow.com/a/7832437/5726546
"""
  objtype = "simple"

    content_indent = ""

    @classmethod
    def can_document_member(cls, member: Any, membername: str, isattr: bool, parent: Any) -> bool:
        return False

    # do not add a header to the docstring
    def add_directive_header(self, sig: str) -> None:
        pass

Setup and directive are the same as in the answer by geographika.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文