summaryrefslogtreecommitdiff
path: root/sphinx/domains/std.py
diff options
context:
space:
mode:
authorTakeshi KOMIYA <i.tkomiya@gmail.com>2020-08-08 20:06:46 +0900
committerTakeshi KOMIYA <i.tkomiya@gmail.com>2020-11-03 02:27:43 +0900
commit0e98e9b1a8ade24acf1d91f59aa90e8b1e179b19 (patch)
tree6fdb9ca354e92e1fe28efb6fbf9fdabf45e77f15 /sphinx/domains/std.py
parent487b8436c6e8dc596db4b8d4d06e9145105a2ac2 (diff)
downloadsphinx-git-0e98e9b1a8ade24acf1d91f59aa90e8b1e179b19.tar.gz
Fix #6914: Emit a detailed warning when failed to resolve :ref:
To be clear the ambiguous warning for missing-reference :ref:, this separates the warning to missing-label and missing-caption. To emit a warning dynamically, this also adds a new event: `warn-missing-reference` to customize warning messages via event handlers.
Diffstat (limited to 'sphinx/domains/std.py')
-rw-r--r--sphinx/domains/std.py17
1 files changed, 15 insertions, 2 deletions
diff --git a/sphinx/domains/std.py b/sphinx/domains/std.py
index 39f67b54e..7ea468404 100644
--- a/sphinx/domains/std.py
+++ b/sphinx/domains/std.py
@@ -610,8 +610,6 @@ class StandardDomain(Domain):
dangling_warnings = {
'term': 'term not in glossary: %(target)s',
- 'ref': 'undefined label: %(target)s (if the link has no caption '
- 'the label must precede a section header)',
'numref': 'undefined label: %(target)s',
'keyword': 'unknown keyword: %(target)s',
'doc': 'unknown document: %(target)s',
@@ -1107,8 +1105,23 @@ class StandardDomain(Domain):
RemovedInSphinx40Warning, stacklevel=2)
+def warn_missing_reference(app: "Sphinx", domain: Domain, node: pending_xref) -> bool:
+ if domain.name != 'std' or node['reftype'] != 'ref':
+ return None
+ else:
+ target = node['reftarget']
+ if target not in domain.anonlabels: # type: ignore
+ msg = __('undefined label: %s')
+ else:
+ msg = __('Failed to create a cross reference. A title or caption not found: %s')
+
+ logger.warning(msg % target, location=node, type='ref', subtype=node['reftype'])
+ return True
+
+
def setup(app: "Sphinx") -> Dict[str, Any]:
app.add_domain(StandardDomain)
+ app.connect('warn-missing-reference', warn_missing_reference)
return {
'version': 'builtin',