|
25 | 25 | import collections
|
26 | 26 | import hashlib
|
27 | 27 |
|
28 |
| -from docutils.nodes import citation, Text |
| 28 | +from docutils.nodes import citation, Text, reference |
29 | 29 | import sphinx
|
30 |
| -from sphinx.addnodes import pending_xref, desc_content |
| 30 | +from sphinx.addnodes import pending_xref, desc_content, only |
31 | 31 |
|
32 | 32 | if sphinx.__version__ < '1.0.1':
|
33 | 33 | raise RuntimeError("Sphinx 1.0.1 or newer is required")
|
|
43 | 43 |
|
44 | 44 | HASH_LEN = 12
|
45 | 45 |
|
46 |
| - |
47 | 46 | def rename_references(app, what, name, obj, options, lines):
|
48 | 47 | # decorate reference numbers so that there are no duplicates
|
49 | 48 | # these are later undecorated in the doctree, in relabel_references
|
@@ -103,6 +102,18 @@ def matching_pending_xref(node):
|
103 | 102 | ref.replace(ref_text, new_text.copy())
|
104 | 103 |
|
105 | 104 |
|
| 105 | +def clean_backrefs(app, doc, docname): |
| 106 | + # only::latex directive has resulted in citation backrefs without reference |
| 107 | + known_ref_ids = set() |
| 108 | + for ref in doc.traverse(reference, descend=True): |
| 109 | + for id in ref['ids']: |
| 110 | + known_ref_ids.add(id) |
| 111 | + for citation_node in doc.traverse(citation, descend=True): |
| 112 | + # remove backrefs to non-existant refs |
| 113 | + citation_node['backrefs'] = [id for id in citation_node['backrefs'] |
| 114 | + if id in known_ref_ids] |
| 115 | + |
| 116 | + |
106 | 117 | DEDUPLICATION_TAG = ' !! processed by numpydoc !!'
|
107 | 118 |
|
108 | 119 |
|
@@ -179,6 +190,7 @@ def setup(app, get_doc_object_=get_doc_object):
|
179 | 190 | app.connect('autodoc-process-docstring', mangle_docstrings)
|
180 | 191 | app.connect('autodoc-process-signature', mangle_signature)
|
181 | 192 | app.connect('doctree-read', relabel_references)
|
| 193 | + app.connect('doctree-resolved', clean_backrefs) |
182 | 194 | app.add_config_value('numpydoc_edit_link', None, False)
|
183 | 195 | app.add_config_value('numpydoc_use_plots', None, False)
|
184 | 196 | app.add_config_value('numpydoc_use_blockquotes', None, False)
|
|
0 commit comments