Skip to content

Commit

Permalink
Filter pending_xref_condition node on failed resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
tk0miya committed Mar 5, 2021
1 parent e113097 commit d991326
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
23 changes: 20 additions & 3 deletions sphinx/transforms/post_transforms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@
from sphinx.transforms import SphinxTransform
from sphinx.util import logging
from sphinx.util.docutils import SphinxTranslator
from sphinx.util.nodes import process_only_nodes
from sphinx.util.nodes import find_pending_xref_condition, process_only_nodes

logger = logging.getLogger(__name__)

if False:
# For type annotation
from docutils.nodes import Node


class SphinxPostTransform(SphinxTransform):
"""A base class of post-transforms.
Expand Down Expand Up @@ -97,8 +101,21 @@ def run(self, **kwargs: Any) -> None:
if newnode is None:
self.warn_missing_reference(refdoc, typ, target, node, domain)
except NoUri:
newnode = contnode
node.replace_self(newnode or contnode)
newnode = None

if newnode:
newnodes = [newnode] # type: List[Node]
else:
newnodes = [contnode]
if newnode is None and isinstance(node[0], addnodes.pending_xref_condition):
matched = find_pending_xref_condition(node, "*")
if matched:
newnodes = matched.children
else:
logger.warning(__('Could not determine the fallback text for the '
'cross-reference. Might be a bug.'), location=node)

node.replace_self(newnodes)

def resolve_anyref(self, refdoc: str, node: pending_xref, contnode: Element) -> Element:
"""Resolve reference generated by the "any" role."""
Expand Down
10 changes: 10 additions & 0 deletions sphinx/util/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,16 @@ def make_id(env: "BuildEnvironment", document: nodes.document,
return node_id


def find_pending_xref_condition(node: addnodes.pending_xref, condition: str) -> Element:
"""Pick matched pending_xref_condition node up from the pending_xref."""
for subnode in node:
if (isinstance(subnode, addnodes.pending_xref_condition) and
subnode.get('condition') == condition):
return subnode
else:
return None


def make_refnode(builder: "Builder", fromdocname: str, todocname: str, targetid: str,
child: Node, title: str = None) -> nodes.reference:
"""Shortcut to create a reference node."""
Expand Down

0 comments on commit d991326

Please sign in to comment.