Skip to content

Commit c8513c5

Browse files
jnothmanlarsoner
authored andcommitted
Use DEDUPLICATION_TAG to determine whether a citation node is in a docstring (#179)
* Use DEDUPLICATION_TAG to determine whether a citation node is in a numpydoc docstring Fixes #177 * Fix regression for citations in class/function docstrings * Remove debug print
1 parent 40b3733 commit c8513c5

File tree

1 file changed

+30
-8
lines changed

1 file changed

+30
-8
lines changed

numpydoc/numpydoc.py

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@
2727
except ImportError:
2828
from collections import Callable
2929
import hashlib
30+
import itertools
3031

31-
from docutils.nodes import citation, Text, reference
32+
from docutils.nodes import citation, Text, section, comment, reference
3233
import sphinx
3334
from sphinx.addnodes import pending_xref, desc_content, only
3435

@@ -73,18 +74,39 @@ def rename_references(app, what, name, obj, options, lines):
7374
sixu('.. [%s]') % new_r)
7475

7576

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

81105

82106
def relabel_references(app, doc):
83107
# Change 'hash-ref' to 'ref' in label text
84108
for citation_node in doc.traverse(citation):
85-
if _ascend(citation_node, desc_content) is None:
86-
# no desc node in ancestry -> not in a docstring
87-
# XXX: should we also somehow check it's in a References section?
109+
if not _is_cite_in_numpydoc_docstring(citation_node):
88110
continue
89111
label_node = citation_node[0]
90112
prefix, _, new_label = label_node[0].astext().partition('-')

0 commit comments

Comments
 (0)