2017-08-17 62 views
0

我想將以下鏈接引用到我的其中一個reST文檔中:https://docs.python.org/3/library/stdtypes.html#typecontextmanager。我試圖使用:ref:內聯指令而不是鏈接。我跑python -m sphinx.ext.intersphinx https://docs.python.org/3/objects.inv。結果表明,除其他事項外:如何通過sphinx鏈接到Python 3中的typecontextmanager標籤通過sphinx

 
... 
std:label 
    23acks     Acknowledgements    : whatsnew/2.3.html#acks 
    23section-other   Other Changes and Fixes  : whatsnew/2.3.html#section-other 
... 
    typebytearray    Bytearray Objects   : library/stdtypes.html#typebytearray 
    typebytes     Bytes Objects    : library/stdtypes.html#typebytes 
typecontextmanager  Context Manager Types  : library/stdtypes.html#typecontextmanager 
    typeiter     Iterator Types    : library/stdtypes.html#typeiter 
    typememoryview   Memory Views     : 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,但沒有鏈接。

我缺少什麼?

我使用的獅身人面像1.6.3上的蟒蛇安裝Python 3.6.2的

注1

我使用:ref:`with <python:with>`同樣的問題,這是應該指向https://docs.python.org/3/reference/compound_stmts.html#with,根據庫存線(也下std:label):

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

我猜測到的主要問題的解決方案將最有可能解決這一點。

注2

可能不是100%的相關性,但我可以在相同的部分鏈接到:py:meth:`~contextmanager.__enter__`沒有任何問題。

回答

1

其中任何一個都適合我。

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

請注意,如果您在目標周圍使用尖括號,則必須包含標題。

下面是它們的渲染對我來說:

順便說一句,我最近添加上下文管理金字塔的文檔中的Glossary,我認爲做得很好解釋他們是什麼的工作。這裏的重要來源:

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

我沒有看到我們的參考之間有任何根本的區別在這裏。我會做一個「乾淨」,看看是否有幫助。 –

+0

'乾淨'固定它。但是,您的答案中有一些有價值的提示,因此我會選擇它以保持代表流暢。 –

+1

在我對另一個問題的回答中有幾個其他選項:https://stackoverflow.com/a/43908388/2214933。 Sphinx將解析後的reST文件緩存爲「doctree pickles」,而不是Python源代碼文件,然後寫入輸出。它只查找新的和已更改的文件。所以如果你沒有改變你的reST文件並在上一次運行後保存它,那麼輸出中的變化將永遠不會顯示。我有時只是添加一個換行符並保存它以強制當前文件運行。 –