28
28
from collections import Callable
29
29
import hashlib
30
30
31
- from docutils .nodes import citation , Text
31
+ from docutils .nodes import citation , Text , reference
32
32
import sphinx
33
- from sphinx .addnodes import pending_xref , desc_content
33
+ from sphinx .addnodes import pending_xref , desc_content , only
34
34
35
35
if sphinx .__version__ < '1.0.1' :
36
36
raise RuntimeError ("Sphinx 1.0.1 or newer is required" )
46
46
47
47
HASH_LEN = 12
48
48
49
-
50
49
def rename_references (app , what , name , obj , options , lines ):
51
50
# decorate reference numbers so that there are no duplicates
52
51
# these are later undecorated in the doctree, in relabel_references
@@ -93,8 +92,8 @@ def relabel_references(app, doc):
93
92
new_text = Text (new_label )
94
93
label_node .replace (label_node [0 ], new_text )
95
94
96
- for id in citation_node ['backrefs' ]:
97
- ref = doc .ids [id ]
95
+ for id_ in citation_node ['backrefs' ]:
96
+ ref = doc .ids [id_ ]
98
97
ref_text = ref [0 ]
99
98
100
99
# Sphinx has created pending_xref nodes with [reftext] text.
@@ -107,6 +106,18 @@ def matching_pending_xref(node):
107
106
ref .replace (ref_text , new_text .copy ())
108
107
109
108
109
+ def clean_backrefs (app , doc , docname ):
110
+ # only::latex directive has resulted in citation backrefs without reference
111
+ known_ref_ids = set ()
112
+ for ref in doc .traverse (reference , descend = True ):
113
+ for id_ in ref ['ids' ]:
114
+ known_ref_ids .add (id_ )
115
+ for citation_node in doc .traverse (citation , descend = True ):
116
+ # remove backrefs to non-existant refs
117
+ citation_node ['backrefs' ] = [id_ for id_ in citation_node ['backrefs' ]
118
+ if id_ in known_ref_ids ]
119
+
120
+
110
121
DEDUPLICATION_TAG = ' !! processed by numpydoc !!'
111
122
112
123
@@ -185,6 +196,7 @@ def setup(app, get_doc_object_=get_doc_object):
185
196
app .connect ('autodoc-process-docstring' , mangle_docstrings )
186
197
app .connect ('autodoc-process-signature' , mangle_signature )
187
198
app .connect ('doctree-read' , relabel_references )
199
+ app .connect ('doctree-resolved' , clean_backrefs )
188
200
app .add_config_value ('numpydoc_edit_link' , None , False )
189
201
app .add_config_value ('numpydoc_use_plots' , None , False )
190
202
app .add_config_value ('numpydoc_use_blockquotes' , None , False )
0 commit comments