Skip to content

Cooperate with linkcheck builder #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/rn.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ Release notes
0.0.0 (2020-09-09)
******************

Initial release.
Initial release.
13 changes: 13 additions & 0 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,16 @@ Occasionally, you have to move complete documentation to a new home. It's easy w
}

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


Checking your redirects
***********************

Sphinx has a linkcheck_ builder for finding broken links in your
documentation. This extension cooperates with it so that redirects to
external websites will be checked too.

.. _linkcheck: https://www.sphinx-doc.org/en/master/usage/builders/index.html#sphinx.builders.linkcheck.CheckExternalLinksBuilder

Checking redirects to another page in the same documentation is not
supported yet.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sphinx
sphinx>=7.1
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
long_description=open("README.rst", encoding="utf-8").read(),
long_description_content_type="text/x-rst",
packages=["sphinx_reredirects"],
install_requires=["sphinx"],
install_requires=["sphinx>=7.1"],
python_requires=">=3.5",
setup_requires=["wheel"],
classifiers=[
Expand Down
22 changes: 22 additions & 0 deletions sphinx_reredirects/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
from pathlib import Path
from string import Template
from typing import Dict, Mapping, Optional, Sequence
from urllib.parse import urlparse

from sphinx.application import Sphinx
from sphinx.builders.linkcheck import CheckExternalLinksBuilder, Hyperlink
from sphinx.util import logging
from sphinx.util.osutil import SEP

Expand All @@ -26,6 +28,7 @@ def setup(app: Sphinx) -> Dict:
Extension setup, called by Sphinx
"""
app.connect("html-collect-pages", init)
app.connect("builder-inited", collect_redirects_for_linkcheck)
app.add_config_value(OPTION_REDIRECTS, OPTION_REDIRECTS_DEFAULT, "env")
app.add_config_value(OPTION_TEMPLATE_FILE, OPTION_TEMPLATE_FILE_DEFAULT, "env")
return dict(parallel_read_safe=True)
Expand Down Expand Up @@ -160,3 +163,22 @@ def _render_redirect_template(self, to_uri: str) -> str:
content = Template(redirect_template).substitute({"to_uri": to_uri})

return content


def collect_redirects_for_linkcheck(app):
if not isinstance(app.builder, CheckExternalLinksBuilder):
return

rr = Reredirects(app)
redirects = rr.grab_redirects()

for docname, target in redirects.items():
if new_target := app.emit_firstresult("linkcheck-process-uri", target):
target = new_target
if urlparse(target).scheme not in ("http", "https"):
# Checking redirects to other pages of the same documentation is not
# supported for now.
continue
docpath = app.env.doc2path(docname)
hyperlink = Hyperlink(uri=target, docname=docname, docpath=docpath, lineno=-1)
app.builder.hyperlinks[target] = hyperlink
2 changes: 1 addition & 1 deletion test-requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sphinx==5.3.0
sphinx==7.2.6