我正在尝试改进我的Python模块文档的结构。
目前我有一个Sphinx的index.rst
文件,看起来像这样:
Contents:
.. toctree::
:maxdepth: 3
.. automodule:: myModule1
:members:
.. automodule:: myModule2
:members:
等等。这会生成一个包含所有已记录函数的混乱长列表的HTML页面。这意味着文档在代码中比在HTML输出中更容易阅读。
第一个改进很容易:我可以在每个模块的开头添加标题和描述,以便在HTML输出中更清楚地分离模块。就像这样:
"""
##############################################
myModule1 Title
##############################################
Description of myModule1.
"""
等等,现在我想进一步把我的模块分成各自具有标题和描述的区域,这些区域属于模块功能的一个子集。我尝试了下面的方法:
"""
##############################################
myModule1 Title
##############################################
Description of myModule1.
"""
... # meaning import statements etc
"""
**********************************************
First Section title
**********************************************
Some descriptive text
"""
def myFunction1()
""" function doc """
... # meaning the actual function implementation
def myFunction2()
""" function doc """
... # meaning the actual function implementation
"""
**********************************************
Second Section title
**********************************************
Some descriptive text
"""
def myFunction3()
""" function doc """
... # meaning the actual function implementation
def myFunction4()
""" function doc """
... # meaning the actual function implementation
但这会导致一个错误:
'SEVERE: 意外的章节标题或转换.'
运行make HTML
时出现错误。
那么,我如何在不将文档与其所描述的代码分开的情况下获得所需的结构?
sphinx.ext.napoleon
扩展是解决此问题的一种方法。我不确定它能帮助多少,但它支持许多预定义的节标题。请参见http://www.sphinx-doc.org/en/stable/ext/napoleon.html。 - mzjnsphinx.ext.napoleon
看起来确实很有用,但我无法让它与我请求的类型的部分配合工作。 - 5Ke__doc__
属性是该模块中第一条语句作为字符串字面值的文本。请参见此被拒绝的Sphinx增强建议链接:https://github.com/sphinx-doc/sphinx/issues/442。 - mzjn