diff --git a/CHANGES b/CHANGES index a193f3749..e5963cfcc 100644 --- a/CHANGES +++ b/CHANGES @@ -38,8 +38,8 @@ Features added :confval:`locale_dirs` * #6525: linkcheck: Add :confval:`linkcheck_warn_redirects` to emit a warning when the hyperlink is redirected -* #6525: linkcheck: Add :confval:`linkcheck_ignore_redirects` to ignore - hyperlinks that are redirected to expected URLs +* #6525: linkcheck: Add :confval:`linkcheck_allowed_redirects` to mark + hyperlinks that are redirected to expected URLs as "working" * #9097: Optimize the paralell build * #9131: Add :confval:`nitpick_ignore_regex` to ignore nitpicky warnings using regular expressions diff --git a/doc/usage/configuration.rst b/doc/usage/configuration.rst index 463199170..9f110f656 100644 --- a/doc/usage/configuration.rst +++ b/doc/usage/configuration.rst @@ -2522,17 +2522,17 @@ Options for the linkcheck builder .. versionadded:: 1.1 -.. confval:: linkcheck_ignore_redirects +.. confval:: linkcheck_allowed_redirects A dictionary that maps a pattern of the source URI to a pattern of the canonical - URI. If set, linkcheck builder treated as "ignored" if a hyperlink in the source + URI. If set, linkcheck builder treated as "working" if a hyperlink in the source document that matches to the pattern of the source URI, and it redirects to the URL that matches to the pattern of the canonical URI. Example: .. code-block:: python - linkcheck_ignore_redirects = { - # All HTTP redirections from the source URI to the canonical URI will be ignored. + linkcheck_working_redirects = { + # All HTTP redirections from the source URI to the canonical URI will be treated as "working". 'http://sphinx-doc.org/.*': 'https://sphinx-doc.org/en/master/.*' } diff --git a/sphinx/builders/linkcheck.py b/sphinx/builders/linkcheck.py index e96b45d25..2da438cbf 100644 --- a/sphinx/builders/linkcheck.py +++ b/sphinx/builders/linkcheck.py @@ -499,7 +499,7 @@ class HyperlinkAvailabilityCheckWorker(Thread): if anchor: new_url += '#' + anchor - if ignored_redirect(req_url, new_url): + if allowed_redirect(req_url, new_url): return 'working', '', 0 elif response.history: # history contains any redirects, get last @@ -508,8 +508,8 @@ class HyperlinkAvailabilityCheckWorker(Thread): else: return 'redirected', new_url, 0 - def ignored_redirect(url: str, new_url: str) -> bool: - for from_url, to_url in self.config.linkcheck_ignore_redirects.items(): + def allowed_redirect(url: str, new_url: str) -> bool: + for from_url, to_url in self.config.linkcheck_allowed_redirects.items(): if from_url.match(url) and to_url.match(new_url): return True @@ -656,17 +656,17 @@ class HyperlinkCollector(SphinxPostTransform): hyperlinks[uri] = uri_info -def compile_linkcheck_ignore_redirects(app: Sphinx, config: Config) -> None: - """Compile patterns in linkcheck_ignore_redirects to the regexp objects.""" - for url, pattern in list(app.config.linkcheck_ignore_redirects.items()): +def compile_linkcheck_allowed_redirects(app: Sphinx, config: Config) -> None: + """Compile patterns in linkcheck_allowed_redirects to the regexp objects.""" + for url, pattern in list(app.config.linkcheck_allowed_redirects.items()): try: - app.config.linkcheck_ignore_redirects[re.compile(url)] = re.compile(pattern) + app.config.linkcheck_allowed_redirects[re.compile(url)] = re.compile(pattern) except re.error as exc: - logger.warning(__('Failed to compile regex in linkcheck_ignore_redirects: %r %s'), + logger.warning(__('Failed to compile regex in linkcheck_allowed_redirects: %r %s'), exc.pattern, exc.msg) finally: # Remove the original regexp-string - app.config.linkcheck_ignore_redirects.pop(url) + app.config.linkcheck_allowed_redirects.pop(url) def setup(app: Sphinx) -> Dict[str, Any]: @@ -674,7 +674,7 @@ def setup(app: Sphinx) -> Dict[str, Any]: app.add_post_transform(HyperlinkCollector) app.add_config_value('linkcheck_ignore', [], None) - app.add_config_value('linkcheck_ignore_redirects', {}, None) + app.add_config_value('linkcheck_allowed_redirects', {}, None) app.add_config_value('linkcheck_auth', [], None) app.add_config_value('linkcheck_request_headers', {}, None) app.add_config_value('linkcheck_retries', 1, None) @@ -687,7 +687,7 @@ def setup(app: Sphinx) -> Dict[str, Any]: app.add_config_value('linkcheck_rate_limit_timeout', 300.0, None) app.add_config_value('linkcheck_warn_redirects', False, None) - app.connect('config-inited', compile_linkcheck_ignore_redirects, priority=800) + app.connect('config-inited', compile_linkcheck_allowed_redirects, priority=800) return { 'version': 'builtin', diff --git a/tests/test_build_linkcheck.py b/tests/test_build_linkcheck.py index 604856308..7d03d1df4 100644 --- a/tests/test_build_linkcheck.py +++ b/tests/test_build_linkcheck.py @@ -300,9 +300,9 @@ def test_linkcheck_warn_redirects(app, warning): @pytest.mark.sphinx('linkcheck', testroot='linkcheck-localserver', freshenv=True, confoverrides={ - 'linkcheck_ignore_redirects': {'http://localhost:.*/': '.*'} + 'linkcheck_allowed_redirects': {'http://localhost:.*/': '.*'} }) -def test_linkcheck_ignore_redirects(app): +def test_linkcheck_allowed_redirects(app): with http_server(make_redirect_handler(support_head=False)): app.build()