|
24 | 24 | import inspect
|
25 | 25 | import collections
|
26 | 26 | import hashlib
|
| 27 | +import itertools |
27 | 28 |
|
28 |
| -from docutils.nodes import citation, Text |
| 29 | +from docutils.nodes import citation, Text, section, comment |
29 | 30 | import sphinx
|
30 |
| -from sphinx.addnodes import pending_xref, desc_content |
| 31 | +from sphinx.addnodes import pending_xref |
31 | 32 |
|
32 | 33 | if sphinx.__version__ < '1.0.1':
|
33 | 34 | raise RuntimeError("Sphinx 1.0.1 or newer is required")
|
@@ -70,18 +71,35 @@ def rename_references(app, what, name, obj, options, lines):
|
70 | 71 | sixu('.. [%s]') % new_r)
|
71 | 72 |
|
72 | 73 |
|
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 |
77 | 97 |
|
78 | 98 |
|
79 | 99 | def relabel_references(app, doc):
|
80 | 100 | # Change 'hash-ref' to 'ref' in label text
|
81 | 101 | 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): |
85 | 103 | continue
|
86 | 104 | label_node = citation_node[0]
|
87 | 105 | prefix, _, new_label = label_node[0].astext().partition('-')
|
|
0 commit comments