【问题标题】:How to link to typecontextmanager label in Python 3 docs via sphinx如何通过 sphinx 链接到 Python 3 文档中的 typecontextmanager 标签
【发布时间】:2017-08-17 02:07:50
【问题描述】:

我正在尝试在我的一份 reST 文档中引用以下链接:https://docs.python.org/3/library/stdtypes.html#typecontextmanager。我正在尝试使用 :ref: 内联指令而不是链接。

我跑了python -m sphinx.ext.intersphinx https://docs.python.org/3/objects.inv。结果显示,除其他外:

... 标准:标签 23acks 致谢:whatsnew/2.3.html#acks 23section-other 其他更改和修复:whatsnew/2.3.html#section-other ... typebytearray 字节数组对象:库/stdtypes.html#typebytearray typebytes 字节对象:库/stdtypes.html#typebytes typecontextmanager 上下文管理器类型:library/stdtypes.html#typecontextmanager typeiter 迭代器类型:library/stdtypes.html#typeiter typememoryview 内存视图:library/stdtypes.html#typememoryview ...

加粗的 URL 正是我正在寻找的,因为我的 intersphinx_mapping 看起来像这样:

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

我使用以下指令:

:ref:`context manager <python:typecontextmanager>`

这似乎指向正确的标签,但我收到以下警告:

WARNING: undefined label: python:typecontextmanager (if the link has no caption the label must precede a section header)

:ref: 被字符串 context manager 替换,但没有链接。

我错过了什么?

我在 Python 3.6.2 的 Anaconda 安装上使用 sphinx 1.6.3

注 1

根据库存行(也在std:label下),我使用:ref:`with &lt;python:with&gt;`也有同样的问题,它应该指向https://docs.python.org/3/reference/compound_stmts.html#with

     with                       The with statement          : reference/compound_stmts.html#with

我猜主要问题的解决方案很可能也能解决这个问题。

注2

可能不是 100% 相关,但我可以毫无问题地链接到同一部分中的 :py:meth:`~contextmanager.__enter__`

【问题讨论】:

    标签: python python-sphinx restructuredtext


    【解决方案1】:

    这些都适合我。

    :ref:`python:typecontextmanager`
    :ref:`typecontextmanager <python:typecontextmanager>`
    

    请注意,如果您在目标周围使用尖括号,则必须包含标题。

    这是它们为我呈现的方式:

    顺便说一句,我最近在 Pyramid 的 Glossary 文档中添加了上下文管理器,我认为这很好地解释了它们是什么。这是reST来源:

    :ref:`With Statement Context Managers <python:context-managers>`
    :ref:`with <python:with>`
    

    【讨论】:

    • 我在这里没有看到我们的参考文献之间有任何根本区别。我会做一个make clean 看看是否有帮助。
    • A make clean 修复了它。但是,您的回答中包含一些有价值的提示,因此我将选择它以保持该代表畅通无阻。
    • 在我对另一个问题的回答中还有几个其他选项:stackoverflow.com/a/43908388/2214933。 Sphinx 将解析的 reST 文件缓存为“doctree pickles”,而不是 Python 源代码文件,然后写入输出。它只查找新的和更改的文件。因此,如果您在上次运行后没有更改您的 reST 文件并保存它,那么输出中的更改将永远不会显示。我有时只是添加一个换行符并保存它以强制当前文件运行。
    猜你喜欢
    • 2016-05-26
    • 1970-01-01
    • 2015-11-27
    • 1970-01-01
    • 2016-07-14
    • 1970-01-01
    • 2012-07-02
    • 1970-01-01
    • 2017-05-28
    相关资源
    最近更新 更多