diff --git a/CHANGES b/CHANGES index 931c0e0dcd..632446431a 100644 --- a/CHANGES +++ b/CHANGES @@ -15,6 +15,7 @@ Features added * #9494, #9456: html search: Add a config variable :confval:`html_show_search_summary` to enable/disable the search summaries +* #10125: extlinks: Improve suggestion message for a reference having title Bugs fixed ---------- diff --git a/sphinx/ext/extlinks.py b/sphinx/ext/extlinks.py index a14c396b60..8caba88429 100644 --- a/sphinx/ext/extlinks.py +++ b/sphinx/ext/extlinks.py @@ -38,7 +38,7 @@ from sphinx.deprecation import RemovedInSphinx60Warning from sphinx.locale import __ from sphinx.transforms.post_transforms import SphinxPostTransform -from sphinx.util import logging +from sphinx.util import logging, rst from sphinx.util.nodes import split_explicit_title from sphinx.util.typing import RoleFunction @@ -67,6 +67,7 @@ def check_uri(self, refnode: nodes.reference) -> None: return uri = refnode['refuri'] + title = refnode.astext() for alias, (base_uri, _caption) in self.app.config.extlinks.items(): uri_pattern = re.compile(base_uri.replace('%s', '(?P.+)')) @@ -75,7 +76,11 @@ def check_uri(self, refnode: nodes.reference) -> None: # build a replacement suggestion msg = __('hardcoded link %r could be replaced by an extlink ' '(try using %r instead)') - replacement = f":{alias}:`{match.groupdict().get('value')}`" + value = match.groupdict().get('value') + if uri != title: + replacement = f":{alias}:`{rst.escape(title)} <{value}>`" + else: + replacement = f":{alias}:`{value}`" logger.warning(msg, uri, replacement, location=refnode) diff --git a/tests/test_ext_extlinks.py b/tests/test_ext_extlinks.py index 2be9789f06..9b0e96cb0b 100644 --- a/tests/test_ext_extlinks.py +++ b/tests/test_ext_extlinks.py @@ -5,14 +5,15 @@ def test_replaceable_uris_emit_extlinks_warnings(app, warning): app.build() warning_output = warning.getvalue() + # there should be exactly three warnings for replaceable URLs message = ( - "WARNING: hardcoded link 'https://github.com/sphinx-doc/sphinx/issues/1' " - "could be replaced by an extlink (try using ':issue:`1`' instead)" + "index.rst:%d: WARNING: hardcoded link 'https://github.com/sphinx-doc/sphinx/issues/1' " + "could be replaced by an extlink (try using '%s' instead)" ) - assert f"index.rst:11: {message}" in warning_output - assert f"index.rst:13: {message}" in warning_output - assert f"index.rst:15: {message}" in warning_output + assert message % (11, ":issue:`1`") in warning_output + assert message % (13, ":issue:`inline replaceable link <1>`") in warning_output + assert message % (15, ":issue:`replaceable link <1>`") in warning_output @pytest.mark.sphinx('html', testroot='ext-extlinks-hardcoded-urls-multiple-replacements') @@ -21,16 +22,16 @@ def test_all_replacements_suggested_if_multiple_replacements_possible(app, warni warning_output = warning.getvalue() # there should be six warnings for replaceable URLs, three pairs per link message = ( - "WARNING: hardcoded link 'https://github.com/octocat' " - "could be replaced by an extlink (try using ':user:`octocat`' instead)" + "index.rst:%d: WARNING: hardcoded link 'https://github.com/octocat' " + "could be replaced by an extlink (try using '%s' instead)" ) - assert f"index.rst:14: {message}" in warning_output - assert f"index.rst:16: {message}" in warning_output - assert f"index.rst:18: {message}" in warning_output + assert message % (14, ":user:`octocat`") in warning_output + assert message % (16, ":user:`inline replaceable link `") in warning_output + assert message % (18, ":user:`replaceable link `") in warning_output message = ( - "WARNING: hardcoded link 'https://github.com/octocat' " - "could be replaced by an extlink (try using ':repo:`octocat`' instead)" + "index.rst:%d: WARNING: hardcoded link 'https://github.com/octocat' " + "could be replaced by an extlink (try using '%s' instead)" ) - assert f"index.rst:14: {message}" in warning_output - assert f"index.rst:16: {message}" in warning_output - assert f"index.rst:18: {message}" in warning_output + assert message % (14, ":repo:`octocat`") in warning_output + assert message % (16, ":repo:`inline replaceable link `") in warning_output + assert message % (18, ":repo:`replaceable link `") in warning_output