summaryrefslogtreecommitdiffstats
path: root/docs/extensions/canonical_fix.py
blob: 509a546a6fd56c1898f1b06d73b1f12898483193 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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