python - 使用 Sphinx 格式化多行文档字符串

标签 python python-sphinx autodoc

使用 sphinx autodoc , 有没有办法以特殊方式格式化多行文档字符串的第一行?

考虑:

def whatever():
    """This function does something.

    This really should have a full function definition, but I am too lazy.
    Some more stuff.
    """

正在生成的 html 代码:

<dd>
<p>This function does something.</p>
<p>This really should have a full function definition, but I am too lazy. Some more stuff.</p>
</dd>

我希望它是这样的:

<dd>
<p class='headline'>This function does something.</p>
<p>This really should have a full function definition, but I am too lazy. Some more stuff.</p>
</dd>

最佳答案

据我所知,autodoc 并没有为您提供很多标记文档字符串的功能,尤其是在向文档字符串添加自定义样式方面。我可以想到两种方法来解决这个问题:1) 将第一行包装在 **This function dos something** 中,以便将其加粗。 2) 编写一个自定义的 sphinx 扩展,它在 autodoc 解析文档字符串之前拦截它们,并相应地进行处理。

(我最终选择了选项 2,以便在我的文档字符串中包含节标题...这是该扩展的 source。它不满足您的需要,但它可能很有用一个起点,特别是 _remove_oneline 函数对模块文档字符串的作用)。

关于python - 使用 Sphinx 格式化多行文档字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12017622/

相关文章:

python - 如何在Python中以正确的方式将一列分成两部分?

python - 访问剪贴板 Windows (7) 64 位

python - 在 python 中使用 2 个参数下标

python - Sphinx 无法处理类定义内的函数分配

makefile - 为什么 toctree 没有用 RTD 主题更新?

python - 如何使用 sphinx autodoc 阻止 "Index"和 "Python Module Index"出现?

python - 使用Python字典值返回键作为结果

python - 阅读文档搜索损坏

python-sphinx - 使用 sphinx/reStructuredText 锚定在任意位置?

python - 使用 Sphinx 通过命令行参数记录脚本时出现异常