Sphinx 将代码格式添加到 :ref: [英] Sphinx add code formatting to :ref:
问题描述
我想向这样的引用添加代码格式:
I would like to add code formatting to a reference like this:
:ref:`__slots__ <python:slots>`
我的 intersphinx_mapping
像这样定义 python
:
My intersphinx_mapping
defines python
like this:
intersphinx_mapping = {
'python': ('https://docs.python.org/3', None),
}
所以链接是 https://docs.python.org/3/reference/datamodel.html#slots(slots
在 https://docs.python.org 的
)std:label
部分定义/3/objects.inv
so the link is to https://docs.python.org/3/reference/datamodel.html#slots (slots
is defined in the std:label
section of https://docs.python.org/3/objects.inv
)
我的目标是以 :py:attr:
或类似样式而不是 :ref:
呈现的默认文本样式来格式化呈现的链接.
My goal is to format the rendered link in the style of :py:attr:
or similar rather than the default text style with which :ref:
renders.
以下选项均无效:
... ``:ref:`__slots__ <python:slots>``` ...
产生类似的输出
... :ref:`__slots__
...
... ``:ref:`__slots__ <python:slots>` `` ...
输出如下
... __slots__ ...
... :ref:```__slots__`` <python:slots>` ...
产生类似的输出
... :ref:```__slots__``
... :ref:```__slots__`` <python:slots>` ...
在这种情况下,有两个指向 #id1
的独立内部链接.
There are two separate internal links to #id1
in this case.
... :ref:```__slots__`` <python:slots>` ...
yields 输出如
... :ref:` ``__slots__`` <python:slots>` ...
yeilds output like
... :ref:` ``__slots__`` <python:slots>` ...>
... :ref:` ``__slots__`` <python:slots>` ...
使用 :code:`...`
或 :literal:`...`
替换 ``...``
:ref:
的内部或外部也无济于事.事实上,似乎根本不允许嵌套角色.
Relpacing ``...``
with :code:`...`
or :literal:`...`
either inside or outside the :ref:
does not help any either. In fact, it appears that nested roles are not allowed at all.
我想要一个内联角色,该角色会产生具有代码样式和链接的内容,例如
I would like to have an inline role that results in something that renders with a code style and a link, like
... __slots__
...
...
__slots__
...
如何让基本的 :ref:
(或等效的)与 :py:attr:
使用的代码样式格式一起出现?
How do I get the basic :ref:
(or equivalent) to appear with the code-style formatting used by :py:attr:
?
我在 Anaconda 环境中使用 Sphinx 1.6.3 和 Python 3.6.2.
I am using Sphinx 1.6.3 with Python 3.6.2 in an Anaconda environment.
反向问题在这里:Sphinx 从自定义代码参考中删除代码格式
一个切线相关的问题:嵌套/复合角色:将多个角色应用于重叠文本
推荐答案
:any:
角色做你想做的事:
The :any:
role does what you want:
:any:`__slots__ <python:slots>`
这是一个便利角色",它在当前域中查找交叉引用目标以及与 :ref:
一起使用的目标.
This is a "convenience role" that looks for cross-reference targets in the current domain as well as targets that work with :ref:
.
Python 域有几个 特定的交叉-引用角色,用于不同类型的对象.
The Python domain has several specific cross-reference roles, for different types of objects.
这篇关于Sphinx 将代码格式添加到 :ref:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!