Skip to content
Open
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
3 changes: 3 additions & 0 deletions docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ For other use cases, you can customize these configuration options in your ``con
Default: The path from the `READTHEDOCS_CANONICAL_URL <https://docs.readthedocs.com/platform/stable/reference/environment-variables.html#envvar-READTHEDOCS_CANONICAL_URL>`__ environment variable.
In case that variables is not defined, it defaults to ``/en/latest/``.

You can use `{language}` substitution to include the build's language urified (lowercased, with `_` and `@` converted
to `-`).

Type: string

.. warning::
Expand Down
12 changes: 11 additions & 1 deletion notfound/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ def finalize_media(app, pagename, templatename, context, doctree):
:type doctree: docutils.nodes.document
"""

default_baseuri = app.config.notfound_urls_prefix or '/'
language_uri = app.config.language.lower().replace('_', '-').replace('@', '-')
if app.config.notfound_urls_prefix:
default_baseuri = app.config.notfound_urls_prefix.format(language=language_uri)
else:
default_baseuri = '/'

# https://github.com/sphinx-doc/sphinx/blob/v7.2.3/sphinx/builders/html/__init__.py#L1024-L1036
def pathto(otheruri: str, resource: bool = False, baseuri: str = default_baseuri):
Expand Down Expand Up @@ -295,6 +299,12 @@ def validate_configs(app, *args, **kwargs):
message = 'notfound_urls_prefix should start and end with "/" (slash)'
warnings.warn(message, UserWarning, stacklevel=2)

if notfound_urls_prefix != default and notfound_urls_prefix:
no_language = notfound_urls_prefix.replace('{language}', '')
if '{' in no_language and '}' in no_language:
message = 'notfound_urls_prefix supports only "{language}" substitution'
warnings.warn(message, UserWarning, stacklevel=2)


def setup(app):
default_context = {
Expand Down
21 changes: 11 additions & 10 deletions tests/test_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,9 @@ def test_pagename_setting(app, status, warning):
@pytest.mark.sphinx(
srcdir=srcdir,
confoverrides={
'notfound_urls_prefix': '/language/version/',
'notfound_urls_prefix': '/{language}/version/',
'html_theme': 'alabaster',
'language': 'pt_BR',
},
)
def test_urls_prefix_setting(app, status, warning):
Expand All @@ -154,22 +155,22 @@ def test_urls_prefix_setting(app, status, warning):
if sphinx.version_info < (7, 4):
alt = "Logo"
else:
alt = "Logo of Python"
alt = "Logo de Python"

chunks = [
# sidebar URLs
'<h1 class="logo"><a href="/language/version/index.html">Python</a></h1>',
'<form class="search" action="/language/version/search.html" method="get">',
'<li><a href="/language/version/index.html">Documentation overview</a><ul>',
'<h1 class="logo"><a href="/pt-br/version/index.html">Python</a></h1>',
'<form class="search" action="/pt-br/version/search.html" method="get">',
'<li><a href="/pt-br/version/index.html">Documentation overview</a><ul>',

# favicon and logo
f'<link rel="{cssclass}icon" href="/language/version/_static/favicon.png"/>',
f'<img class="logo" src="/language/version/_static/logo.svg" alt="{alt}"/>',
f'<link rel="{cssclass}icon" href="/pt-br/version/_static/favicon.png"/>',
f'<img class="logo" src="/pt-br/version/_static/logo.svg" alt="{alt}"/>',

# resources
_get_css_html_link_tag(app, 'language', 'version', 'alabaster.css'),
_get_css_html_link_tag(app, 'language', 'version', 'pygments.css'),
'<link rel="stylesheet" href="/language/version/_static/custom.css" type="text/css" />',
_get_css_html_link_tag(app, 'pt-br', 'version', 'alabaster.css'),
_get_css_html_link_tag(app, 'pt-br', 'version', 'pygments.css'),
'<link rel="stylesheet" href="/pt-br/version/_static/custom.css" type="text/css" />',
]

for chunk in chunks:
Expand Down