From a307aa7a49c7752c3dd781f30e17f57289de4d51 Mon Sep 17 00:00:00 2001 From: David Lord Date: Tue, 15 Oct 2024 17:27:12 -0700 Subject: [PATCH] use sphinx-notfound-page extension --- CHANGES.rst | 2 ++ pyproject.toml | 1 + src/pallets_sphinx_themes/__init__.py | 31 ++++++++++--------- .../themes/pocoo/404.html | 13 -------- 4 files changed, 20 insertions(+), 27 deletions(-) delete mode 100644 src/pallets_sphinx_themes/themes/pocoo/404.html diff --git a/CHANGES.rst b/CHANGES.rst index 41773f7..4e4f1f7 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -8,6 +8,8 @@ Unreleased - New version warning banner. Use JavaScript to query PyPI when viewing a page, rather than baking the warning into the build. New builds of old versions are no longer required for the banner to be correct. :pr:`117` +- Generate 404 page using the sphinx-notfound-page extension. This fixes the + URLs when the page is hosted so that it loads the CSS. :issue:`34` Version 2.1.3 diff --git a/pyproject.toml b/pyproject.toml index 4075762..a4802cd 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -21,6 +21,7 @@ requires-python = ">=3.8" dependencies = [ "packaging", "sphinx>=3", + "sphinx-notfound-page", ] [project.urls] diff --git a/src/pallets_sphinx_themes/__init__.py b/src/pallets_sphinx_themes/__init__.py index 0da01ae..7ec89aa 100644 --- a/src/pallets_sphinx_themes/__init__.py +++ b/src/pallets_sphinx_themes/__init__.py @@ -8,7 +8,6 @@ from urllib.parse import urlsplit from sphinx.application import Sphinx -from sphinx.builders._epub_base import EpubBuilder from sphinx.builders.dirhtml import DirectoryHTMLBuilder from sphinx.builders.singlehtml import SingleFileHTMLBuilder from sphinx.errors import ExtensionError @@ -28,10 +27,26 @@ def setup(app): app.add_config_value("is_pallets_theme", None, "html") + # Use the sphinx-notfound-page extension to generate a 404 page with valid + # URLs. Only configure it if it's not already configured. + if "notfound.extension" not in app.config.extensions: + app.config.extensions.append("notfound.extension") + app.config.notfound_context = { + "title": "Page Not Found", + "body": """

Page Not Found

+

+ The page you requested does not exist. You may have followed a bad + link, or the page may have been moved or removed. + """, + } + + if "READTHEDOCS" not in os.environ: + # Disable the default prefix outside of Read the Docs. + app.config.notfound_urls_prefix = None + app.connect("builder-inited", set_is_pallets_theme) app.connect("builder-inited", find_base_canonical_url) app.connect("builder-inited", add_theme_files) - app.connect("html-collect-pages", add_404_page) app.connect("html-page-context", canonical_url) try: @@ -57,18 +72,6 @@ def setup(app): return {"version": own_release, "parallel_read_safe": True} -@only_pallets_theme(default=()) -def add_404_page(app): - """Build an extra ``404.html`` page if no ``"404"`` key is in the - ``html_additional_pages`` config. - """ - is_epub = isinstance(app.builder, EpubBuilder) - config_pages = app.config.html_additional_pages - - if not is_epub and "404" not in config_pages: - yield ("404", {}, "404.html") - - @only_pallets_theme() def find_base_canonical_url(app: Sphinx) -> None: """When building on Read the Docs, build the base canonical URL from the diff --git a/src/pallets_sphinx_themes/themes/pocoo/404.html b/src/pallets_sphinx_themes/themes/pocoo/404.html deleted file mode 100644 index 1ccc53a..0000000 --- a/src/pallets_sphinx_themes/themes/pocoo/404.html +++ /dev/null @@ -1,13 +0,0 @@ -{% extends 'layout.html' %} - -{% set title = _('Page Not Found') %} - -{% block body %} -

Page Not Found

-

- The page you requested does not exist. You may have followed a bad - link, or the page may have been moved or removed. -

- Go to the overview or - search. -{% endblock %}