Sphinx + reStructuredText 中的内联代码链接

Hyp*_*ane 8 python restructuredtext cross-reference python-sphinx

在 Markdown 中,可以像这样创建内联代码链接

[`dict.update`](https://docs.python.org/3/library/stdtypes.html#dict.update)
Run Code Online (Sandbox Code Playgroud)

渲染效果如下dict.update。如何在 reStructuredText / Sphinx 中获得类似的行为?我尝试(1)使用转换器,但它永远不会产生类似的结果(2)嵌套外部链接`link <link>`_和内联代码块:code:`dict.update`,但这也不起作用。

bad*_*der 6

正确的方法是使用扩展sphinx.ext.intersphinx

在你的conf.py添加中

extensions = [
    'sphinx.ext.intersphinx',  # the last entry does not use a comma.
    # 'sphinx.ext.autodoc',  # just for example so there is more than 1 line.
]


intersphinx_mapping = {'python': ('https://docs.python.org/3', None)}

# If you having an `objects.inv` for local builds
# intersphinx_mapping = {"python": ("https://docs.python.org/3", 'objects.inv'),}
Run Code Online (Sandbox Code Playgroud)

然后在您的.rst文件或文件的文档字符串中.py编写一个简单的交叉引用。请注意,这是一个方法,因此在交叉引用时应使用dict.update正确的角色 ( )。:py:meth:下面的例子

A simple text to :meth:`dict.update`
Run Code Online (Sandbox Code Playgroud)

将给出以下 HTML 输出(屏幕截图中还包含交叉引用的工具提示和链接)

在此输入图像描述