Skip to content

Commit 40b3733

Browse files
authored
Merge pull request #180 from jnothman/fix169
FIX remove duplicated citation back-references
2 parents e7fce86 + c7b9439 commit 40b3733

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

numpydoc/numpydoc.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828
from collections import Callable
2929
import hashlib
3030

31-
from docutils.nodes import citation, Text
31+
from docutils.nodes import citation, Text, reference
3232
import sphinx
33-
from sphinx.addnodes import pending_xref, desc_content
33+
from sphinx.addnodes import pending_xref, desc_content, only
3434

3535
if sphinx.__version__ < '1.0.1':
3636
raise RuntimeError("Sphinx 1.0.1 or newer is required")
@@ -46,7 +46,6 @@
4646

4747
HASH_LEN = 12
4848

49-
5049
def rename_references(app, what, name, obj, options, lines):
5150
# decorate reference numbers so that there are no duplicates
5251
# these are later undecorated in the doctree, in relabel_references
@@ -93,8 +92,8 @@ def relabel_references(app, doc):
9392
new_text = Text(new_label)
9493
label_node.replace(label_node[0], new_text)
9594

96-
for id in citation_node['backrefs']:
97-
ref = doc.ids[id]
95+
for id_ in citation_node['backrefs']:
96+
ref = doc.ids[id_]
9897
ref_text = ref[0]
9998

10099
# Sphinx has created pending_xref nodes with [reftext] text.
@@ -107,6 +106,18 @@ def matching_pending_xref(node):
107106
ref.replace(ref_text, new_text.copy())
108107

109108

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

112123

@@ -185,6 +196,7 @@ def setup(app, get_doc_object_=get_doc_object):
185196
app.connect('autodoc-process-docstring', mangle_docstrings)
186197
app.connect('autodoc-process-signature', mangle_signature)
187198
app.connect('doctree-read', relabel_references)
199+
app.connect('doctree-resolved', clean_backrefs)
188200
app.add_config_value('numpydoc_edit_link', None, False)
189201
app.add_config_value('numpydoc_use_plots', None, False)
190202
app.add_config_value('numpydoc_use_blockquotes', None, False)

0 commit comments

Comments
 (0)