获得“平坦” sphinx automodule 的成员输出

发布于 2024-12-14 22:43:15 字数 558 浏览 3 评论 0原文

我正在使用 Sphinx autodoc 扩展来记录模块,并且我希望在文档输出中获得模块成员的平面列表。

我尝试使用以下内容:

.. automodule:: modname
   :members:

但是,这有两个问题:

  1. 它包括模块的文档字符串,我不想要在这里。

  2. 每个条目的名称都以“modname.”为前缀,这是完全多余的(因为此页面专门用于记录此模块)

但是,我无法找到任何配置选项来让我有选择地禁用这两个方面,同时仍然获得所有模块成员的自动列表。

我当前的计划是仅使用 autofunction (等)并显式枚举要记录的成员,但我仍然想知道我是否错过了实现我最初想要的东西的简单方法。

更新:我至少找到了第二部分的解决方法:在conf.py中设置add_module_names=False。但这是一个全球环境,所以它并不能真正回答我最初的问题。

I'm using the Sphinx autodoc extension to document a module, and I'd like to get a flat list of the module's members in the documentation output.

I tried using the following:

.. automodule:: modname
   :members:

However, there are two problems with this:

  1. It includes the module's docstring, which I don't want here.

  2. The name of each entry is prefixed with "modname.", which is completely redundant (since this page is specifically for documenting this module)

However, I haven't been able to find any config options that would let me selectively disable these two aspects while still getting the automatic listing of all of the module members.

My current plan is to just use autofunction (etc) and explicitly enumerate the members to be documented, but I'd still like to know if I missed an easy way to achieve what I originally wanted.

Update: I at least found a workaround for the second part: set add_module_names=False in conf.py. That's a global setting though, so it doesn't really answer my original question.

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

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

发布评论

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

评论(1

无远思近则忧 2024-12-21 22:43:15

查看此类似问题的答案,我发现您可以使用autodoc-process-docstring 事件以删除来自模块的文档字符串将以下代码附加到您的 conf.py

def skip_modules_docstring(app, what, name, obj, options, lines):
    if what == 'module':
        del lines[:]

def setup(app):
    app.connect('autodoc-process-docstring', skip_modules_docstring)

请注意,需要 del 语句,因为根据文档,对 lines 的修改code> 必须就地发生(如果你创建一个新对象,它不起作用)。

最后,您还可以使用 name 来过滤少数模块的文档字符串,同时保留其他模块的文档字符串。

Looking at this answer to a similar question, I've found that you can use the autodoc-process-docstring event to remove the docstrings from modules appending the following code to your conf.py:

def skip_modules_docstring(app, what, name, obj, options, lines):
    if what == 'module':
        del lines[:]

def setup(app):
    app.connect('autodoc-process-docstring', skip_modules_docstring)

Note that the del statement is needed because, according to the documentation, the modification to lines must happend in place (it you create a new object, it doesn't work).

Finally, you can also use name to filter the docstrings of just a few modules while keeping the ones from others.

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