使用sphinx autodoc,是否有方法以特殊方式格式化多行docstring的第一行?
考虑:

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并没有给您很多标记docstring的能力,特别是在向docstring添加自定义样式方面。我可以想出两种方法来解决这个问题:1)将第一行用**This function does something**括起来,这样它就会加粗。2)编写一个自定义sphinx扩展,在autodoc解析docstring之前截取docstring,并相应地对其进行处理。
(为了在文档字符串中包含节头,我最终选择了选项2。。。这是那个分机的source。它不做您需要的事情,但作为一个起点,它可能很有用,特别是_remove_oneline函数对docstring模块所做的事情)。

08-19 21:40