Close #10125: extlinks: Improve suggestion message for a reference having title

This commit is contained in:
Takeshi KOMIYA
2022-01-23 20:09:47 +09:00
parent 2be0630951
commit d8a398bbdd
3 changed files with 24 additions and 17 deletions

View File

@@ -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
----------

View File

@@ -38,7 +38,7 @@ from sphinx.application import Sphinx
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 @@ class ExternalLinksChecker(SphinxPostTransform):
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<value>.+)'))
@@ -75,7 +76,11 @@ class ExternalLinksChecker(SphinxPostTransform):
# 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)

View File

@@ -5,14 +5,15 @@ import pytest
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 <octocat>`") in warning_output
assert message % (18, ":user:`replaceable link <octocat>`") 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 <octocat>`") in warning_output
assert message % (18, ":repo:`replaceable link <octocat>`") in warning_output