微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Sphinx + reStructuredText 中的内联代码链接

如何解决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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。