2011-08-11 112 views
37

我正在使用Sphinx生成一个python项目的文档。 输出html不保留文档字符串中存在的换行符。 例子:如何使用sphinx生成python文档时保留换行符

代码

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 

不知道如何解决它?

+0

例。 Sphinx正确保存了重组文本格式。 –

+0

新增示例。 –

+0

当你的docstrings是Google格式并且想要避免添加一堆\ n的时候,如何做到这一点的任何想法? –

回答

31

一般在重组文本中使用

| Vertical bars 
| like this 

保持换行符

+8

不要只加入|在两个单独的行上(比如说,如果你想要两个换行符)......确保在|之后有2个空格 因此它变成: |(space)(space) 换行。 – Augiwan

5

在我的具体情况,我试图让车博士阅读文档字符串(""" my doc string """)。最后我用\n无处不在我需要添加一个换行符:如果您添加以下到您的主.rst文件

This is the first line\n 
and this is the second line\n 
+0

关于如何在您的文档字符串处于Google格式时执行此操作的任何想法? –

8

在你的情况,你可以这样写:

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

    | Arguments: 
    | arg1: arg1 description 
    | arg2: arg2 description 

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

这个答案来得晚,但也许它仍然将是有用的人。

您可以在文档字符串中使用reStructuredText。这看起来像

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

从你的例子看起来不过看来你正在使用的谷歌风格的文档字符串(http://google-styleguide.googlecode.com/svn/trunk/pyguide.html?showone=Comments#Comments)。

狮身人面像本身不支持这些。然而,有一个名为napoleon的扩展,它可以在https://pypi.python.org/pypi/sphinxcontrib-napoleon处解析Google和Numpy风格的文档。

要使用你的扩展追加'sphinxcontrib.napoleon'extension -list在狮身人面像conf.py(通常doc/source/conf.py),因此它成为像需要

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

这是我在寻找的确切答案,同时试图了解为什么狮身人面像不会呈现Google风格correclty,但指南甚至建议使用Sphinx。 –

+2

从狮身人面像1.3开始,拿破仑的延伸将与sphinx.ext.napoleon下的狮身人面像一起包装。 sphinxcontrib.napoleon扩展将继续使用Sphinx <= 1.2。 – ash84

+1

它如何解决换行问题? – minerals

相关问题