Skip to content

Commit d991326

Browse files
committed
Filter pending_xref_condition node on failed resolution
1 parent e113097 commit d991326

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

sphinx/transforms/post_transforms/__init__.py

+20-3
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,14 @@
2222
from sphinx.transforms import SphinxTransform
2323
from sphinx.util import logging
2424
from sphinx.util.docutils import SphinxTranslator
25-
from sphinx.util.nodes import process_only_nodes
25+
from sphinx.util.nodes import find_pending_xref_condition, process_only_nodes
2626

2727
logger = logging.getLogger(__name__)
2828

29+
if False:
30+
# For type annotation
31+
from docutils.nodes import Node
32+
2933

3034
class SphinxPostTransform(SphinxTransform):
3135
"""A base class of post-transforms.
@@ -97,8 +101,21 @@ def run(self, **kwargs: Any) -> None:
97101
if newnode is None:
98102
self.warn_missing_reference(refdoc, typ, target, node, domain)
99103
except NoUri:
100-
newnode = contnode
101-
node.replace_self(newnode or contnode)
104+
newnode = None
105+
106+
if newnode:
107+
newnodes = [newnode] # type: List[Node]
108+
else:
109+
newnodes = [contnode]
110+
if newnode is None and isinstance(node[0], addnodes.pending_xref_condition):
111+
matched = find_pending_xref_condition(node, "*")
112+
if matched:
113+
newnodes = matched.children
114+
else:
115+
logger.warning(__('Could not determine the fallback text for the '
116+
'cross-reference. Might be a bug.'), location=node)
117+
118+
node.replace_self(newnodes)
102119

103120
def resolve_anyref(self, refdoc: str, node: pending_xref, contnode: Element) -> Element:
104121
"""Resolve reference generated by the "any" role."""

sphinx/util/nodes.py

+10
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,16 @@ def make_id(env: "BuildEnvironment", document: nodes.document,
531531
return node_id
532532

533533

534+
def find_pending_xref_condition(node: addnodes.pending_xref, condition: str) -> Element:
535+
"""Pick matched pending_xref_condition node up from the pending_xref."""
536+
for subnode in node:
537+
if (isinstance(subnode, addnodes.pending_xref_condition) and
538+
subnode.get('condition') == condition):
539+
return subnode
540+
else:
541+
return None
542+
543+
534544
def make_refnode(builder: "Builder", fromdocname: str, todocname: str, targetid: str,
535545
child: Node, title: str = None) -> nodes.reference:
536546
"""Shortcut to create a reference node."""

0 commit comments

Comments
 (0)