5

我最近开始向我的项目添加文档,并且正在尝试遵循 Google 样式指南。我正在使用 Sphinx 生成文档,并使用 Sphinx 扩展名拿破仑来弥合 Google 样式指南和 reST 之间的差距。

我在渲染参数和注释时没有问题,但我似乎无法让示例部分呈现代码片段。

class Chicken(object):
      """Animal that lays egg and has feathers

         Note:
             Chickens love to eat feed

         Example:
             chicken.eats(feed)
      """

我还尝试了示例部分的双冒号。

Example::
4

2 回答 2

6

Example::在分节符和文字块之间需要一个双冒号和一个空行。

请参阅拿破仑文档中的示例:

"""Example Google style docstrings.

This module demonstrates documentation as specified by the `Google Python
Style Guide`_. Docstrings may extend over multiple lines. Sections are created
with a section header and a colon followed by a block of indented text.

Example:
    Examples can be given using either the ``Example`` or ``Examples``
    sections. Sections support any reStructuredText formatting, including
    literal blocks::

        $ python example_google.py

Section breaks are created by resuming unindented text. Section breaks
are also implicitly created anytime a new section starts.
"""

所以,在你的例子中,试试这个:

class Chicken(object):
      """Animal that lays egg and has feathers

         Note:
             Chickens love to eat feed

         Example::

             chicken.eats(feed)
      """
于 2017-07-07T14:30:04.673 回答
1

建立@Brown的答案,它似乎是为了让示例部分呈现为可识别的分节符和代码片段,您将使用“示例:”,后跟缩进的“::”,后跟空白行和一个双缩进的代码片段。对我来说,以下两个都在输出中引入了一个以粗体“示例”开头的代码块。

class Chicken(object):
      """Animal that lays egg and has feathers

         Note:
             Chickens love to eat feed

         Example:
             Detail about example (I'm feeding the chicken)::

                 chicken.eats(feed)
      """

或者:

class Chicken(object):
      """Animal that lays egg and has feathers

         Note:
             Chickens love to eat feed

         Example:
             ::

                 chicken.eats(feed)
      """
于 2020-02-11T23:10:43.370 回答