Skip to content

Commit c14b3dc

Browse files
committed
[IMP] conf.py: allow referencing unlabelled versions and languages
This commit removes the requirement on versions and languages to have a label in `versions_names`/`languages_names` to be displayed in the version/language switcher. Instead, if a version/language has no label defined, the raw version/language string will be used as fallback. This change makes it easier to add new versions or languages to the documentation without having to explicitly define display labels for them in `conf.py`. The responsibility of ensuring that referenced versions and languages actually exist is shifted to the build configuration. X-original-commit: e57530e Part-of: #12339 Signed-off-by: Antoine Vandevenne (anv) <[email protected]>
1 parent b146ea7 commit c14b3dc

File tree

2 files changed

+50
-49
lines changed

2 files changed

+50
-49
lines changed

Diff for: Makefile

+2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ endif
3535

3636
#=== Standard rules ===#
3737

38+
.PHONY: all help clean html latexpdf gettext fast static test review
39+
3840
# In first position to build the documentation from scratch by default
3941
all: html
4042

Diff for: conf.py

+48-49
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@
228228
) = lambda docname, compact: docname.split('/')[1 if docname.startswith('applications/') else 0]
229229

230230
# The version names that should be shown in the version switcher, if the config option `versions`
231-
# is populated. If a version is passed to `versions` but is not listed here, it will not be shown.
231+
# is populated.
232232
versions_names = {
233233
'master': "Master",
234234
'saas-18.1': "Odoo Online",
@@ -241,7 +241,7 @@
241241
}
242242

243243
# The language names that should be shown in the language switcher, if the config option `languages`
244-
# is populated. If a language is passed to `languages` but is not listed here, it will not be shown.
244+
# is populated.
245245
languages_names = {
246246
'de': 'DE',
247247
'en': 'EN',
@@ -431,7 +431,7 @@ def _generate_alternate_urls(app, pagename, templatename, context, doctree):
431431
- The language switcher and related link tags
432432
"""
433433

434-
def _canonicalize():
434+
def canonicalize():
435435
""" Add the canonical URL for the current document in the rendering context.
436436
437437
The canonical version is the last released version of the documentation.
@@ -443,94 +443,93 @@ def _canonicalize():
443443
- /documentation/11.0/fr/website.html -> canonical = /documentation/14.0/fr/website.html
444444
"""
445445
# If the canonical version is not set, assume that the project has a single version
446-
_canonical_version = app.config.canonical_version or app.config.version
447-
_canonical_lang = 'en' # Always 'en'. Don't take the value of the config option.
448-
context['canonical'] = _build_url(_version=_canonical_version, _lang=_canonical_lang)
446+
canonical_version_ = app.config.canonical_version or app.config.version
447+
canonical_lang_ = 'en' # Always 'en'. Don't take the value of the config option.
448+
context['canonical'] = build_url(version_=canonical_version_, lang_=canonical_lang_)
449449

450-
def _versionize():
450+
def versionize():
451451
""" Add the pairs of (version, url) for the current document in the rendering context.
452452
453453
The entry 'version' is added by Sphinx in the rendering context.
454454
"""
455-
context['version_display_name'] = versions_names[version]
455+
context['version_display_name'] = versions_names.get(version, version)
456456

457-
# If the list of versions is not set, assume the project has no alternate version
458-
_provided_versions = app.config.versions and app.config.versions.split(',') or []
457+
# If the list of versions is not set, assume the project has no alternate version.
458+
provided_versions_ = app.config.versions and app.config.versions.split(',') or []
459459

460460
# Map alternate versions to their display names and URLs.
461461
context['alternate_versions'] = []
462-
for _alternate_version, _display_name in versions_names.items():
463-
if _alternate_version in _provided_versions and _alternate_version != version:
464-
context['alternate_versions'].append(
465-
(_display_name, _build_url(_alternate_version))
466-
)
462+
for alternate_version_ in provided_versions_:
463+
if alternate_version_ != version:
464+
display_name_ = versions_names.get(alternate_version_, alternate_version_)
465+
context['alternate_versions'].append((display_name_, build_url(alternate_version_)))
467466

468-
def _localize():
467+
def localize():
469468
""" Add the pairs of (lang, code, url) for the current document in the rendering context.
470469
471470
E.g.: ('French', 'fr', 'https://.../fr_BE/...')
472471
473472
The entry 'language' is added by Sphinx in the rendering context.
474473
"""
475-
_current_lang = app.config.language or 'en'
474+
current_lang_ = app.config.language or 'en'
476475
# Replace the context value by its upper-cased value ("FR" instead of "fr")
477-
context['language'] = languages_names.get(_current_lang, _current_lang.upper())
478-
context['language_code'] = _current_lang
476+
context['language'] = languages_names.get(current_lang_, current_lang_.upper())
477+
context['language_code'] = current_lang_
479478

480479
# If the list of languages is not set, assume that the project has no alternate language
481-
_provided_languages = app.config.languages and app.config.languages.split(',') or []
480+
provided_languages_ = app.config.languages and app.config.languages.split(',') or []
482481

483482
# Map alternate languages to their display names and URLs.
484483
context['alternate_languages'] = []
485-
for _alternate_lang, _display_name in languages_names.items():
486-
if _alternate_lang in _provided_languages and _alternate_lang != _current_lang:
484+
for alternate_lang_ in provided_languages_:
485+
if alternate_lang_ != current_lang_:
486+
display_name_ = languages_names.get(alternate_lang_, alternate_lang_.upper())
487487
context['alternate_languages'].append(
488488
(
489-
_display_name,
490-
_alternate_lang.split('_')[0] if _alternate_lang != 'en' else 'x-default',
491-
_build_url(_lang=_alternate_lang),
489+
display_name_,
490+
alternate_lang_.split('_')[0] if alternate_lang_ != 'en' else 'x-default',
491+
build_url(lang_=alternate_lang_),
492492
)
493493
)
494494

495495
# Dynamic generation of localized legal doc links
496496
context['legal_translations'] = legal_translations
497497

498-
499-
def _build_url(_version=None, _lang=None):
498+
def build_url(version_=None, lang_=None):
500499
if app.config.is_remote_build:
501500
# Project root like https://www.odoo.com/documentation
502-
_root = app.config.project_root
501+
root_ = app.config.project_root
503502
else:
504503
# Project root like .../documentation/_build/html/14.0/fr
505-
_root = re.sub(rf'(/{app.config.version})?(/{app.config.language})?$', '', app.outdir)
504+
root_ = re.sub(rf'(/{app.config.version})?(/{app.config.language})?$', '', app.outdir)
506505
# If the canonical version is not set, assume that the project has a single version
507-
_canonical_version = app.config.canonical_version or app.config.version
508-
_version = _version or app.config.version
509-
_lang = _lang or app.config.language or 'en'
510-
_canonical_page = f'{pagename}.html'
506+
canonical_version_ = app.config.canonical_version or app.config.version
507+
version_ = version_ or app.config.version
508+
lang_ = lang_ or app.config.language or 'en'
509+
canonical_page_ = f'{pagename}.html'
511510

512511
# legal translations have different URLs schemes as they are not managed on transifex
513512
# e.g. FR translation of /terms/enterprise => /fr/terms/enterprise_fr
514513
if pagename.startswith('legal/terms/'):
515-
if _lang in legal_translations and not pagename.endswith(f"_{_lang}"):
514+
if lang_ in legal_translations and not pagename.endswith(f"_{lang_}"):
516515
# remove language code for current translation, set target one
517-
_page = re.sub("_[a-z]{2}$", "", pagename)
518-
if 'terms/i18n' not in _page:
519-
_page = _page.replace("/terms/", "/terms/i18n/")
520-
_canonical_page = f'{_page}_{_lang}.html'
521-
elif _lang == 'en' and pagename.endswith(tuple(f"_{l}" for l in legal_translations)):
516+
page_ = re.sub("_[a-z]{2}$", "", pagename)
517+
if 'terms/i18n' not in page_:
518+
page_ = page_.replace("/terms/", "/terms/i18n/")
519+
canonical_page_ = f'{page_}_{lang_}.html'
520+
elif lang_ == 'en' and pagename.endswith(tuple(f"_{l}" for l in legal_translations)):
522521
# remove language code for current translation, link to original EN one
523-
_page = re.sub("_[a-z]{2}$", "", pagename)
524-
_canonical_page = f'{_page.replace("/i18n/", "/")}.html'
522+
page_ = re.sub("_[a-z]{2}$", "", pagename)
523+
canonical_page_ = f'{page_.replace("/i18n/", "/")}.html'
525524

526525
if app.config.is_remote_build:
527-
_canonical_page = _canonical_page.replace('index.html', '')
526+
canonical_page_ = canonical_page_.replace('index.html', '')
528527

529-
return f'{_root}' \
530-
f'{f"/{_version}" if app.config.versions else ""}' \
531-
f'{f"/{_lang}" if _lang != "en" else ""}' \
532-
f'/{_canonical_page}'
528+
return f'{root_}' \
529+
f'{f"/{version_}" if app.config.versions else ""}' \
530+
f'{f"/{lang_}" if lang_ != "en" else ""}' \
531+
f'/{canonical_page_}'
533532

534-
_canonicalize()
535-
_versionize()
536-
_localize()
533+
canonicalize()
534+
versionize()
535+
localize()

0 commit comments

Comments
 (0)