Sphinx + reStructuredText 中的内联代码链接

问题描述

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

[`dict.update`](https://docs.python.org/3/library/stdtypes.html#dict.update)

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

解决方法

正确的做法是使用 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'),}

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

A simple text to :meth:`dict.update`

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

enter image description here