问题描述
如何将代码嵌入到文档字符串中,以告诉Sphinx像使用Markdown(不同的背景颜色,等距的sans字体)一样格式化代码?例如,记录一个代码用法示例。
""" This is a module documentation
Use this module like this:
res = aFunction(something,goes,in)
print(res.avalue)
"""
解决方法
There are a few ways to do it。我认为您情况下最明智的做法是.. code-block::
""" This is a module documentation
Use this module like this:
.. code-block:: python
res = aFunction(something,goes,in)
print(res.avalue)
"""
请注意指令和代码块之间的空行-必须存在该空行才能正确呈现该块。
,突出显示代码的另一种方法(see the comment of mzjn on this post)是在代码之前的行末以两个(!)冒号结尾:
""" This is a module documentation
Use this module like this::
res = aFunction(something,in)
print(res.avalue)
"""
::
可以解决问题。