Skip to content

Commit 6e2dd23

Browse files
committed
Use DEDUPLICATION_TAG to determine whether a citation node is in a numpydoc docstring
Fixes numpy#177
1 parent 972b2dc commit 6e2dd23

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

numpydoc/numpydoc.py

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@
2424
import inspect
2525
import collections
2626
import hashlib
27+
import itertools
2728

28-
from docutils.nodes import citation, Text
29+
from docutils.nodes import citation, Text, section, comment
2930
import sphinx
30-
from sphinx.addnodes import pending_xref, desc_content
31+
from sphinx.addnodes import pending_xref
3132

3233
if sphinx.__version__ < '1.0.1':
3334
raise RuntimeError("Sphinx 1.0.1 or newer is required")
@@ -70,18 +71,35 @@ def rename_references(app, what, name, obj, options, lines):
7071
sixu('.. [%s]') % new_r)
7172

7273

73-
def _ascend(node, cls):
74-
while node and not isinstance(node, cls):
75-
node = node.parent
76-
return node
74+
def _is_cite_in_numpydoc_docstring(citation_node):
75+
# Find DEDUPLICATION_TAG in comment as last node of sibling section
76+
77+
# XXX: I failed to use citation_node.traverse to do this:
78+
section_node = citation_node.parent
79+
while not isinstance(section_node, section):
80+
section_node = section_node.parent
81+
if section_node is None:
82+
return False
83+
84+
sibling_sections = itertools.chain(section_node.traverse(section,
85+
include_self=True,
86+
descend=False,
87+
siblings=True))
88+
for sibling_section in sibling_sections:
89+
if not sibling_section.children:
90+
continue
91+
last_child = sibling_section.children[-1]
92+
if not isinstance(last_child, comment):
93+
continue
94+
if last_child.rawsource.strip() == DEDUPLICATION_TAG.strip():
95+
return True
96+
return False
7797

7898

7999
def relabel_references(app, doc):
80100
# Change 'hash-ref' to 'ref' in label text
81101
for citation_node in doc.traverse(citation):
82-
if _ascend(citation_node, desc_content) is None:
83-
# no desc node in ancestry -> not in a docstring
84-
# XXX: should we also somehow check it's in a References section?
102+
if not _is_cite_in_numpydoc_docstring(citation_node):
85103
continue
86104
label_node = citation_node[0]
87105
prefix, _, new_label = label_node[0].astext().partition('-')

0 commit comments

Comments
 (0)