Skip to content

Commit fd6aeae

Browse files
committed
FIX remove duplicated citation back-references
Fixes #169
1 parent 972b2dc commit fd6aeae

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

numpydoc/numpydoc.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525
import collections
2626
import hashlib
2727

28-
from docutils.nodes import citation, Text
28+
from docutils.nodes import citation, Text, reference
2929
import sphinx
30-
from sphinx.addnodes import pending_xref, desc_content
30+
from sphinx.addnodes import pending_xref, desc_content, only
3131

3232
if sphinx.__version__ < '1.0.1':
3333
raise RuntimeError("Sphinx 1.0.1 or newer is required")
@@ -43,7 +43,6 @@
4343

4444
HASH_LEN = 12
4545

46-
4746
def rename_references(app, what, name, obj, options, lines):
4847
# decorate reference numbers so that there are no duplicates
4948
# these are later undecorated in the doctree, in relabel_references
@@ -103,6 +102,18 @@ def matching_pending_xref(node):
103102
ref.replace(ref_text, new_text.copy())
104103

105104

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+
106117
DEDUPLICATION_TAG = ' !! processed by numpydoc !!'
107118

108119

@@ -179,6 +190,7 @@ def setup(app, get_doc_object_=get_doc_object):
179190
app.connect('autodoc-process-docstring', mangle_docstrings)
180191
app.connect('autodoc-process-signature', mangle_signature)
181192
app.connect('doctree-read', relabel_references)
193+
app.connect('doctree-resolved', clean_backrefs)
182194
app.add_config_value('numpydoc_edit_link', None, False)
183195
app.add_config_value('numpydoc_use_plots', None, False)
184196
app.add_config_value('numpydoc_use_blockquotes', None, False)

0 commit comments

Comments
 (0)