Skip to content

Commit 1e6a147

Browse files
jeanasliborjelinek
authored andcommitted
Cooperate with linkcheck builder
Redirect links that direct to an HTTP or HTTPS URL (as opposed to relative links inside the same website) are now passed to the linkcheck builder so that it can do its checking work on them. This necessitates requiring Sphinx >= 7.1 due to a restructuring of the linkcheck builder code. Fixes #3
1 parent e228780 commit 1e6a147

File tree

6 files changed

+39
-4
lines changed

6 files changed

+39
-4
lines changed

docs/rn.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,4 @@ Release notes
4141
0.0.0 (2020-09-09)
4242
******************
4343

44-
Initial release.
44+
Initial release.

docs/usage.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,16 @@ Occasionally, you have to move complete documentation to a new home. It's easy w
108108
}
109109

110110
.. tip:: To help search engines to understand the transfer, update (or set) `html_baseurl <https://www.sphinx-doc.org/en/master/usage/configuration.html#confval-html_baseurl>`_ option to the new website, too.
111+
112+
113+
Checking your redirects
114+
***********************
115+
116+
Sphinx has a linkcheck_ builder for finding broken links in your
117+
documentation. This extension cooperates with it so that redirects to
118+
external websites will be checked too.
119+
120+
.. _linkcheck: https://www.sphinx-doc.org/en/master/usage/builders/index.html#sphinx.builders.linkcheck.CheckExternalLinksBuilder
121+
122+
Checking redirects to another page in the same documentation is not
123+
supported yet.

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
sphinx
1+
sphinx>=7.1

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
long_description=open("README.rst", encoding="utf-8").read(),
1212
long_description_content_type="text/x-rst",
1313
packages=["sphinx_reredirects"],
14-
install_requires=["sphinx"],
14+
install_requires=["sphinx>=7.1"],
1515
python_requires=">=3.5",
1616
setup_requires=["wheel"],
1717
classifiers=[

sphinx_reredirects/__init__.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
from pathlib import Path
44
from string import Template
55
from typing import Dict, Mapping, Optional, Sequence
6+
from urllib.parse import urlparse
67

78
from sphinx.application import Sphinx
9+
from sphinx.builders.linkcheck import CheckExternalLinksBuilder, Hyperlink
810
from sphinx.util import logging
911
from sphinx.util.osutil import SEP
1012

@@ -28,6 +30,7 @@ def setup(app: Sphinx) -> Dict:
2830
Extension setup, called by Sphinx
2931
"""
3032
app.connect("html-collect-pages", init)
33+
app.connect("builder-inited", collect_redirects_for_linkcheck)
3134
app.add_config_value(OPTION_REDIRECTS, OPTION_REDIRECTS_DEFAULT, "env")
3235
app.add_config_value(OPTION_TEMPLATE_FILE, OPTION_TEMPLATE_FILE_DEFAULT, "env")
3336
return dict(parallel_read_safe=True)
@@ -162,3 +165,22 @@ def _render_redirect_template(self, to_uri: str) -> str:
162165
content = Template(redirect_template).substitute({"to_uri": to_uri})
163166

164167
return content
168+
169+
170+
def collect_redirects_for_linkcheck(app):
171+
if not isinstance(app.builder, CheckExternalLinksBuilder):
172+
return
173+
174+
rr = Reredirects(app)
175+
redirects = rr.grab_redirects()
176+
177+
for docname, target in redirects.items():
178+
if new_target := app.emit_firstresult("linkcheck-process-uri", target):
179+
target = new_target
180+
if urlparse(target).scheme not in ("http", "https"):
181+
# Checking redirects to other pages of the same documentation is not
182+
# supported for now.
183+
continue
184+
docpath = app.env.doc2path(docname)
185+
hyperlink = Hyperlink(uri=target, docname=docname, docpath=docpath, lineno=-1)
186+
app.builder.hyperlinks[target] = hyperlink

test-requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
sphinx==5.3.0
1+
sphinx==7.2.6

0 commit comments

Comments
 (0)