Skip to content
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

use sphinx-notfound-page extension #118

Merged
merged 1 commit into from
Oct 16, 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: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ requires-python = ">=3.8"
dependencies = [
"packaging",
"sphinx>=3",
"sphinx-notfound-page",
]

[project.urls]
Expand Down
31 changes: 17 additions & 14 deletions src/pallets_sphinx_themes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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": """<h1>Page Not Found</h1>
<p>
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:
Expand All @@ -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
Expand Down
13 changes: 0 additions & 13 deletions src/pallets_sphinx_themes/themes/pocoo/404.html

This file was deleted.