简体   繁体   English

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

[英]Formatting multiline docstrings with Sphinx

Using sphinx autodoc , is there a way to format a first line of a multiline docstring in a special way?使用sphinx autodoc ,有没有办法以特殊方式格式化多行文档字符串的第一行?

Consider:考虑:

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

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

The html code being generated:生成的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>

I want it to be something like:我希望它是这样的:

<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>

To my knowledge, autodoc doesn't give you very many abilities to mark up the docstrings, especially with regards to adding custom styling to the docstring.据我所知,autodoc 并没有给您很多标记文档字符串的能力,尤其是在向文档字符串添加自定义样式方面。 There's two ways to get around this I can think of: 1) wrap the first line in **This function does something** so it'll be bolded.我能想到有两种方法可以解决这个问题:1) 将第一行包装在**This function does something**因此它会被加粗。 2) write a custom sphinx extension which intercepts the docstrings before autodoc has parsed them, and mangle things accordingly. 2) 编写一个自定义的 sphinx 扩展,在 autodoc 解析它们之前拦截文档字符串,并相应地处理内容。

(I ended up going down the road of option 2 in order to have section headers in my docstrings... here's the source for that extension. It doesn't do what you need, but it might be useful as a starting point, particularly what the _remove_oneline function does to module docstrings). (我最终选择了选项 2 的道路,以便在我的文档字符串中有部分标题......这是该扩展的源代码。它不能满足您的需求,但作为起点可能很有用,特别是_remove_oneline函数对模块文档字符串做了什么)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM