Skip to content

Use DEDUPLICATION_TAG to determine whether a citation node is in a docstring #179

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 8, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 30 additions & 8 deletions numpydoc/numpydoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@
except ImportError:
from collections import Callable
import hashlib
import itertools

from docutils.nodes import citation, Text, reference
from docutils.nodes import citation, Text, section, comment, reference
import sphinx
from sphinx.addnodes import pending_xref, desc_content, only

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


def _ascend(node, cls):
while node and not isinstance(node, cls):
node = node.parent
return node
def _is_cite_in_numpydoc_docstring(citation_node):
# Find DEDUPLICATION_TAG in comment as last node of sibling section

# XXX: I failed to use citation_node.traverse to do this:
section_node = citation_node.parent

def is_docstring_section(node):
return isinstance(node, (section, desc_content))

while not is_docstring_section(section_node):
section_node = section_node.parent
if section_node is None:
return False

sibling_sections = itertools.chain(section_node.traverse(is_docstring_section,
include_self=True,
descend=False,
siblings=True))
for sibling_section in sibling_sections:
if not sibling_section.children:
continue
last_child = sibling_section.children[-1]
if not isinstance(last_child, comment):
continue
if last_child.rawsource.strip() == DEDUPLICATION_TAG.strip():
return True
return False


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