python - 如何将一个 Sphinx 角色转换为另一个角色?

标签 python python-sphinx cross-reference

我正在记录一组使用 Sphinx 的项目,这些项目从 intersphinx 扩展中受益匪浅。本质上,大约有 3 个公用事业项目汇集到一个较大的项目中,在较大的项目中,我需要引用较小项目的文档。我将 intersphinx 设置为

extensions = [
    'sphinx.ext.autodoc',
    'sphinx.ext.intersphinx',
    ...
]

intersphinx_mapping = {
    'project1': ('http://internal.url/to/project1/latest', None),
    'project2': ('http://internal.url/to/project2/latest', None),
    'project3': ('http://internal.url/to/project3/latest', None),
}

这让我可以正常使用 intersphinx:

:any:`project1.Class1`

它被呈现为带有文本 project1.Class1 的链接.不过,我宁愿它使用不合格的名称,所以我最后写了

:any:`Class1 <project1.Class1>`

很多。这很烦人。我真的很想设置一些角色,这样我就可以做

* :p1:`Class1` is from ``project1``
* :p2:`Class2` is from ``project2``
* :p3:`function1` is from ``project3``

哪里:p1:`{text}`:any:`{text} <project1.{text}>` 取代等

有没有简单的方法来做到这一点?我所有的搜索都得到了非常无用的信息,intersphinx 的源代码很难阅读。理想情况下,我可以通过某种方式来做

def p1_role(name, rawtext, text, lineno, inliner, options={}, content=[]):
    return sphinx.some_module.parse(':any:`{0} <project1.{0}>`'.format(text))

def setup(app):
    app.add_role('p1', p1_role)

会是最好的,但我不知道它是否在任何地方都可用。

最佳答案

third bullet point of Cross-referencing syntax下:

If you prefix the content with ~, the link text will only be the last component of the target. For example:

:py:meth:`~Queue.Queue.get`

will refer to Queue.Queue.get but only display get as the link text.

它适用于 classfuncmethattr,可能还有 any(我无法让它与 any 一起工作)。

按照模式,试试这个:

:class:`~project1.Class1`

我刚刚在 Pyramid 和 WebOb 中运行了一个测试,这成功了:

:class:`~webob.request.Request`

只有文本 Request 显示并链接到 WebOb API。

关于python - 如何将一个 Sphinx 角色转换为另一个角色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45639845/

相关文章:

javascript - Jinja2 for 循环在列表中的 javascript 中不起作用,但访问单个元素有效

Python-Sphinx:自动包含文档构建的日期和时间

pdf - 尝试理解 PDF 中交叉引用 (XRef) 流中的数据

python - 如何将矩阵乘法的乘积元素打包回去

Python字典检查键是否存在

python - 如何在 Sphinx 中扩展侧边栏目录树上的所有小节?

python-sphinx - 在 Sphinx 中链接外部文档

python - sphinx 未链接到 multiprocessing.Value

python-sphinx - 如何选择与Sphinx交叉引用中使用的文本?

python - Pandas /日期时间/总秒: numpy. timedelta6 4' object has no attribute 'total_seconds'