From 3316ac780adf66580eba9308d5946b3acecc7fbf Mon Sep 17 00:00:00 2001 From: "Antoine Vandevenne (anv)" Date: Tue, 27 Apr 2021 17:37:13 +0200 Subject: [PATCH] allow multi-version and multi-lang in local --- Makefile | 23 ++++++++++++----------- conf.py | 20 +++++++++++++++++--- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/Makefile b/Makefile index 5dc773ed3..32adf3d0e 100644 --- a/Makefile +++ b/Makefile @@ -4,15 +4,17 @@ SPHINX_BUILD = sphinx-build CONFIG_DIR = . SPHINXOPTS = -D project_root=$(ROOT) -D canonical_version=$(CANONICAL_VERSION) \ -D versions=$(VERSIONS) -D languages=$(LANGUAGES) -D language=$(CURRENT_LANG) \ + -D is_remote_build=$(IS_REMOTE_BUILD) \ -A google_analytics_key=$(GOOGLE_ANALYTICS_KEY) SOURCE_DIR = content BUILD_DIR = _build -# TODO ANVFE consider current_version==canonical_version to include (or not) in URL? -ifeq ($(CURRENT_LANG),en) - L10N_HTML_BUILD_DIR = $(BUILD_DIR)/html -else - L10N_HTML_BUILD_DIR = $(BUILD_DIR)/html/$(CURRENT_LANG) +HTML_BUILD_DIR = $(BUILD_DIR)/html +ifdef VERSIONS + HTML_BUILD_DIR := $(HTML_BUILD_DIR)/12.0 +endif +ifneq ($(CURRENT_LANG),en) + HTML_BUILD_DIR := $(HTML_BUILD_DIR)/$(CURRENT_LANG) endif #=== Standard rules ===# @@ -28,13 +30,13 @@ help: clean: @echo "Cleaning build files..." - $(RM_CMD) $(BUILD_DIR)/* - $(RM_CMD) extensions/odoo_theme/static/style.css + rm -rf $(BUILD_DIR)/* + rm extensions/odoo_theme/static/style.css @echo "Cleaning finished." html: extensions/odoo_theme/static/style.css @echo "Starting build..." - $(SPHINX_BUILD) -c $(CONFIG_DIR) -b html $(SPHINXOPTS) $(SOURCE_DIR) $(L10N_HTML_BUILD_DIR) + $(SPHINX_BUILD) -c $(CONFIG_DIR) -b html $(SPHINXOPTS) $(SOURCE_DIR) $(HTML_BUILD_DIR) @echo "Build finished." # To call *after* `make html` @@ -71,7 +73,6 @@ static: extensions/odoo_theme/static/style.css lang: make clean - make fast CANONICAL_VERSION=12.0 VERSIONS=12.0,13.0,14.0,master LANGUAGES=en,fr,es CURRENT_LANG=en - make fast CANONICAL_VERSION=12.0 VERSIONS=12.0,13.0,14.0,master LANGUAGES=en,fr,es CURRENT_LANG=fr - make fast CANONICAL_VERSION=12.0 VERSIONS=12.0,13.0,14.0,master LANGUAGES=en,fr,es CURRENT_LANG=es + make fast CANONICAL_VERSION=12.0 VERSIONS=12.0,13.0,14.0,master LANGUAGES=en,fr CURRENT_LANG=en + make fast CANONICAL_VERSION=12.0 VERSIONS=12.0,13.0,14.0,master LANGUAGES=en,fr CURRENT_LANG=fr diff --git a/conf.py b/conf.py index 682905bdc..9b6af150a 100644 --- a/conf.py +++ b/conf.py @@ -1,3 +1,4 @@ +import re import os import sys from pathlib import Path @@ -231,6 +232,7 @@ def setup(app): app.add_config_value('canonical_version', None, 'env') app.add_config_value('versions', None, 'env') app.add_config_value('languages', None, 'env') + app.add_config_value('is_remote_build', None, 'env') # Whether the build is remotely deployed app.add_lexer('json', JsonLexer) app.add_lexer('xml', XmlLexer) @@ -302,11 +304,23 @@ def _generate_alternate_urls(app, pagename, templatename, context, doctree): ] def _build_url(_version=None, _lang=None): - _root = app.config.project_root or str(Path(__file__).parent) + if app.config.is_remote_build: + # Project root like https://odoo.com/documentation/14.0/fr + _root = app.config.project_root + else: + # Project root like .../documentation/_build/html/14.0/fr + _root = re.sub(rf'(/{app.config.version})?(/{app.config.language})?$', '', app.outdir) + # If the canonical version is not set, assume that the project has a single version + _canonical_version = app.config.canonical_version or app.config.version _version = _version or app.config.version _lang = _lang or app.config.language or 'en' - _canonical_page = (pagename + '.html').replace('index.html', '').replace('index/', '') - return f'{_root}/{_version}{f"/{_lang}" if _lang != "en" else ""}/{_canonical_page}' + _canonical_page = f'{pagename}.html' + if app.config.is_remote_build: + _canonical_page = _canonical_page.replace('index.html', '') + return f'{_root}' \ + f'{f"/{_version}" if app.config.versions else ""}' \ + f'{f"/{_lang}" if _lang != "en" else ""}' \ + f'/{_canonical_page}' _canonicalize() _versionize()