diff options
author | Andrew Godwin | 2022-11-27 16:16:01 -0700 |
---|---|---|
committer | Andrew Godwin | 2022-11-27 16:16:01 -0700 |
commit | ba36f9b92a4e5ca638f35a9ed5d6c75228de53ec (patch) | |
tree | 0b3492ed91c5746430cd083e5ba5a9ec2511febd | |
parent | 348c03e7da2478def83e7d6d9e1f7f1fad265fcd (diff) | |
download | takahe-ba36f9b92a4e5ca638f35a9ed5d6c75228de53ec.tar.gz takahe-ba36f9b92a4e5ca638f35a9ed5d6c75228de53ec.tar.bz2 takahe-ba36f9b92a4e5ca638f35a9ed5d6c75228de53ec.zip |
Fix Sphinx canonical links
-rw-r--r-- | docs/conf.py | 8 | ||||
-rw-r--r-- | docs/extensions/canonical_fix.py | 30 |
2 files changed, 37 insertions, 1 deletions
diff --git a/docs/conf.py b/docs/conf.py index f269972..0052026 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -3,6 +3,11 @@ # For the full list of built-in configuration values, see the documentation: # https://www.sphinx-doc.org/en/master/usage/configuration.html +import pathlib +import sys + +sys.path.insert(0, str(pathlib.Path(__file__).parent / "extensions")) + # -- Project information ----------------------------------------------------- # https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information @@ -13,7 +18,7 @@ author = "Andrew Godwin" # -- General configuration --------------------------------------------------- # https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration -extensions: list = [] +extensions: list = ["canonical_fix"] templates_path = ["_templates"] exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"] @@ -26,3 +31,4 @@ html_theme = "furo" html_static_path = ["_static"] html_logo = "../static/img/logo-128.png" html_favicon = "../static/img/icon-32.png" +html_baseurl = "https://docs.jointakahe.org/" diff --git a/docs/extensions/canonical_fix.py b/docs/extensions/canonical_fix.py new file mode 100644 index 0000000..509a546 --- /dev/null +++ b/docs/extensions/canonical_fix.py @@ -0,0 +1,30 @@ +from sphinx.application import Sphinx +from sphinx.builders.dirhtml import DirectoryHTMLBuilder + + +def setup(app: Sphinx): + app.connect("html-page-context", canonical_url) + + +def canonical_url(app: Sphinx, pagename, templatename, context, doctree): + """Sphinx 1.8 builds a canonical URL if ``html_baseurl`` config is + set. However, it builds a URL ending with ".html" when using the + dirhtml builder, which is incorrect. Detect this and generate the + correct URL for each page. + Also accepts the custom, deprecated ``canonical_url`` config as the + base URL. This will be removed in version 2.1. + """ + base = app.config.html_baseurl + + if ( + not base + or not isinstance(app.builder, DirectoryHTMLBuilder) + or not context["pageurl"] + or not context["pageurl"].endswith(".html") + ): + return + + # Fix pageurl for dirhtml builder if this version of Sphinx still + # generates .html URLs. + target = app.builder.get_target_uri(pagename) + context["pageurl"] = base + target |