使用sphinx生成python文档时如何保留换行符

2024-04-20 10:55:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用Sphinx为python项目生成文档。 输出html没有保留docstring中的换行符。 示例:

代码

def testMethod(arg1,arg2):
    """
    This is a test method

    Arguments:
    arg1: arg1 description
    arg2: arg2 description

    Returns:
    None
    """
    print "I am a test method"

斯芬克斯O/p:

TestModule.testMethod(arg1, arg2)

This is a test method

Arguments: arg1: arg1 description arg2: arg2 description

Returns: None

知道怎么修吗?


Tags: 项目文档testnoneishtmlsphinxdescription
3条回答

这个答案来得很晚,但也许对其他人还是有用的。

您可以在docstring中使用reStructuredText。这看起来像

:param arg1: arg1 description
:type arg1: str
:param arg2: arg2 description
:type arg2: str

然而,从您的示例的外观来看,您似乎正在对docstring(http://google-styleguide.googlecode.com/svn/trunk/pyguide.html?showone=Comments#Comments)使用Google样式。

狮身人面像本身并不支持这些。但是,有一个名为napoleon的扩展名,它在https://pypi.python.org/pypi/sphinxcontrib-napoleon解析Google和Numpy风格的docstring。

要使用扩展名,必须将'sphinxcontrib.napoleon'附加到Sphinx conf.py(通常是doc/source/conf.py)中的extension列表中,这样它就变得类似于

extensions = [                                                                  
'sphinx.ext.autodoc',                                                       
'sphinxcontrib.napoleon',                                                   
'sphinx.ext.doctest',                                                                                                             
]

通常在重组文本中使用

| Vertical bars
| like this

保持换行符

如果将以下内容添加到主.rst文件中:

.. |br| raw:: html

   <br />

然后在标记中,您可以添加|br|来仅为HTML创建换行符。

I want to break this line here: |br| after the break.

发件人:http://docutils.sourceforge.net/FAQ.html#how-to-indicate-a-line-break-or-a-significant-newline

相关问题 更多 >