diff --git a/docs/configuration.rst b/docs/configuration.rst index af61df7..41779f0 100644 --- a/docs/configuration.rst +++ b/docs/configuration.rst @@ -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 `__ 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:: diff --git a/notfound/extension.py b/notfound/extension.py index 2e2b5ee..5e1297d 100644 --- a/notfound/extension.py +++ b/notfound/extension.py @@ -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): @@ -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 = { diff --git a/tests/test_urls.py b/tests/test_urls.py index 21ffb53..d028319 100644 --- a/tests/test_urls.py +++ b/tests/test_urls.py @@ -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): @@ -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 - '

Python

', - '