From 481170838aca290e70fd53bc2864f1459b04eda0 Mon Sep 17 00:00:00 2001 From: Joan Puigcerver Date: Fri, 8 Nov 2024 20:52:20 +0100 Subject: [PATCH 1/4] Added admonitions translations to pymdownx.details --- mkdocs_static_i18n/plugin.py | 32 +++++++++++++++++++++---- tests/details/index.fr.md | 13 ++++++++++ tests/details/index.md | 13 ++++++++++ tests/test_details.py | 46 ++++++++++++++++++++++++++++++++++++ 4 files changed, 99 insertions(+), 5 deletions(-) create mode 100644 tests/details/index.fr.md create mode 100644 tests/details/index.md create mode 100644 tests/test_details.py diff --git a/mkdocs_static_i18n/plugin.py b/mkdocs_static_i18n/plugin.py index 55c0649..c3bb758 100644 --- a/mkdocs_static_i18n/plugin.py +++ b/mkdocs_static_i18n/plugin.py @@ -151,15 +151,29 @@ def on_page_markdown(self, markdown, page, config, files): The page_markdown event is called after the page's markdown is loaded from file and can be used to alter the Markdown source text. - Here we translate admonition types + Here we translate admonition and details titles. """ admonition_translations = self.current_language_config.admonition_translations or {} - RE = re.compile( + + RE_ADMONITION = re.compile( r'^(!!! ?)([\w\-]+(?: +[\w\-]+)*)(?: +"(.*?)")? *$' ) # Copied from https://github.com/Python-Markdown/markdown/blob/master/markdown/extensions/admonition.py and modified for a single-line processing - out = [] - for line in markdown.splitlines(): - m = RE.match(line) + def handle_admonition_translations(line): + m = RE_ADMONITION.match(line) + if m: + type = m.group(2) + if ( + m.group(3) is None or m.group(3).strip() == '' + ) and type in admonition_translations: + title = admonition_translations[type] + line = m.group(1) + m.group(2) + f' "{title}"' + return line + + RE_DETAILS = re.compile( + r'^(\?\?\? ?)([\w\-]+(?: +[\w\-]+)*)(?: +"(.*?)")? *$' + ) # Copied from https://github.com/Python-Markdown/markdown/blob/master/markdown/extensions/admonition.py and modified for a single-line processing + def handle_details_translations(line): + m = RE_DETAILS.match(line) if m: type = m.group(2) if ( @@ -167,7 +181,15 @@ def on_page_markdown(self, markdown, page, config, files): ) and type in admonition_translations: title = admonition_translations[type] line = m.group(1) + m.group(2) + f' "{title}"' + return line + + out = [] + for line in markdown.splitlines(): + line = handle_admonition_translations(line) + if "pymdownx.details" in config["markdown_extensions"]: + line = handle_details_translations(line) out.append(line) + markdown = "\n".join(out) return markdown diff --git a/tests/details/index.fr.md b/tests/details/index.fr.md new file mode 100644 index 0000000..b594fb7 --- /dev/null +++ b/tests/details/index.fr.md @@ -0,0 +1,13 @@ +# Page d'accueil (french version) + +??? tip + Titre implicite dérivé du type d'avertissement, peut être remplacé en définissant `admonition_translations` + +???tip + Pareil sans espaces + +??? warning + Titre implicite dérivé du type d'avertissement, peut être remplacé en définissant `admonition_translations` + +??? warning "Heey" + Titre explicite, n'est pas traduit par `admonition_translations` diff --git a/tests/details/index.md b/tests/details/index.md new file mode 100644 index 0000000..fd06493 --- /dev/null +++ b/tests/details/index.md @@ -0,0 +1,13 @@ +# Home page + +??? tip + Implicit title derived from admonition type, can be overriden by setting `admonition_translations` + +???tip + Same without space + +??? warning + Implicit title derived from admonition type, can be overriden by setting `admonition_translations` + +??? warning "Heey" + Explicit title, isn't translated by `admonition_translations` diff --git a/tests/test_details.py b/tests/test_details.py new file mode 100644 index 0000000..162b018 --- /dev/null +++ b/tests/test_details.py @@ -0,0 +1,46 @@ +import re +import logging +from pathlib import Path + +from mkdocs.commands.build import build +from mkdocs.config.base import load_config + +def test_plugin_no_use_directory_urls_default_language_only(): + mkdocs_config = load_config( + "tests/mkdocs.yml", + theme={"name": "material"}, + docs_dir="details/", + plugins={ + "i18n": { + "languages": [ + { + "locale": "en", + "name": "english", + "default": True, + }, + { + "locale": "fr", + "name": "français", + "build": True, + "admonition_translations": { + "tip": "Conseil", + "warning": "Avertissement", + } + }, + ], + }, + }, + markdown_extensions=["admonition", "pymdownx.details"], + ) + + build(mkdocs_config) + + site_dir = mkdocs_config["site_dir"] + + with open(site_dir+'/index.html') as f: + admonition_titles = re.findall(r"([^<]*)", f.read()) + assert(admonition_titles == ['Tip', 'Tip', 'Warning', 'Heey']) + + with open(site_dir+'/fr/index.html') as f: + admonition_titles = re.findall(r"([^<]*)", f.read()) + assert(admonition_titles == ['Conseil', 'Conseil', 'Avertissement', 'Heey']) From 14efc70fa95eb0d36a916a3d9f06b29d87a7e78a Mon Sep 17 00:00:00 2001 From: Joan Puigcerver Date: Sat, 9 Nov 2024 13:35:27 +0100 Subject: [PATCH 2/4] admonition and details refactor --- mkdocs_static_i18n/plugin.py | 32 ++++++++++++-------------------- 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/mkdocs_static_i18n/plugin.py b/mkdocs_static_i18n/plugin.py index c3bb758..a3b6360 100644 --- a/mkdocs_static_i18n/plugin.py +++ b/mkdocs_static_i18n/plugin.py @@ -155,25 +155,19 @@ def on_page_markdown(self, markdown, page, config, files): """ admonition_translations = self.current_language_config.admonition_translations or {} - RE_ADMONITION = re.compile( - r'^(!!! ?)([\w\-]+(?: +[\w\-]+)*)(?: +"(.*?)")? *$' - ) # Copied from https://github.com/Python-Markdown/markdown/blob/master/markdown/extensions/admonition.py and modified for a single-line processing - def handle_admonition_translations(line): - m = RE_ADMONITION.match(line) - if m: - type = m.group(2) - if ( - m.group(3) is None or m.group(3).strip() == '' - ) and type in admonition_translations: - title = admonition_translations[type] - line = m.group(1) + m.group(2) + f' "{title}"' - return line + marker = r"!{3}" # Admonition marker + if "pymdownx.details" in config["markdown_extensions"]: + marker = r"(?:\?{3}|!{3})" # Admonition or Details marker - RE_DETAILS = re.compile( - r'^(\?\?\? ?)([\w\-]+(?: +[\w\-]+)*)(?: +"(.*?)")? *$' - ) # Copied from https://github.com/Python-Markdown/markdown/blob/master/markdown/extensions/admonition.py and modified for a single-line processing - def handle_details_translations(line): - m = RE_DETAILS.match(line) + RE = re.compile( + r'^(' + + marker + + r' ?)([\w\-]+(?: +[\w\-]+)*)(?: +"(.*?)")? *$' + ) # Copied from https://github.com/Python-Markdown/markdown/blob/master/markdown/extensions/admonition.py and modified for a single-line processing + # Adapted to match the details extension as well + + def handle_admonition_translations(line): + m = RE.match(line) if m: type = m.group(2) if ( @@ -186,8 +180,6 @@ def handle_details_translations(line): out = [] for line in markdown.splitlines(): line = handle_admonition_translations(line) - if "pymdownx.details" in config["markdown_extensions"]: - line = handle_details_translations(line) out.append(line) markdown = "\n".join(out) From 72c4e7a8bc002988b3ef3ab02c4f7c5f436c8680 Mon Sep 17 00:00:00 2001 From: Joan Puigcerver Date: Sat, 9 Nov 2024 14:00:26 +0100 Subject: [PATCH 3/4] translate details opened by default --- mkdocs_static_i18n/plugin.py | 2 +- tests/details/index.fr.md | 6 ++++++ tests/details/index.md | 6 ++++++ tests/test_details.py | 4 ++-- 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/mkdocs_static_i18n/plugin.py b/mkdocs_static_i18n/plugin.py index a3b6360..261a151 100644 --- a/mkdocs_static_i18n/plugin.py +++ b/mkdocs_static_i18n/plugin.py @@ -157,7 +157,7 @@ def on_page_markdown(self, markdown, page, config, files): marker = r"!{3}" # Admonition marker if "pymdownx.details" in config["markdown_extensions"]: - marker = r"(?:\?{3}|!{3})" # Admonition or Details marker + marker = r"(?:\?{3}\+?|!{3})" # Admonition or Details marker RE = re.compile( r'^(' diff --git a/tests/details/index.fr.md b/tests/details/index.fr.md index b594fb7..eb1e4f1 100644 --- a/tests/details/index.fr.md +++ b/tests/details/index.fr.md @@ -6,6 +6,12 @@ ???tip Pareil sans espaces +???+ tip + Same but opened by default + +???+tip + Same without space + ??? warning Titre implicite dérivé du type d'avertissement, peut être remplacé en définissant `admonition_translations` diff --git a/tests/details/index.md b/tests/details/index.md index fd06493..f74851f 100644 --- a/tests/details/index.md +++ b/tests/details/index.md @@ -6,6 +6,12 @@ ???tip Same without space +???+ tip + Same but opened by default + +???+tip + Same without space + ??? warning Implicit title derived from admonition type, can be overriden by setting `admonition_translations` diff --git a/tests/test_details.py b/tests/test_details.py index 162b018..2fba3b9 100644 --- a/tests/test_details.py +++ b/tests/test_details.py @@ -39,8 +39,8 @@ def test_plugin_no_use_directory_urls_default_language_only(): with open(site_dir+'/index.html') as f: admonition_titles = re.findall(r"([^<]*)", f.read()) - assert(admonition_titles == ['Tip', 'Tip', 'Warning', 'Heey']) + assert(admonition_titles == ['Tip', 'Tip', 'Tip', 'Tip', 'Warning', 'Heey']) with open(site_dir+'/fr/index.html') as f: admonition_titles = re.findall(r"([^<]*)", f.read()) - assert(admonition_titles == ['Conseil', 'Conseil', 'Avertissement', 'Heey']) + assert(admonition_titles == ['Conseil', 'Conseil', 'Conseil', 'Conseil', 'Avertissement', 'Heey']) From e8d9d58f507f9d0994c1ed3417e2d0dbf28ae999 Mon Sep 17 00:00:00 2001 From: Joan Puigcerver Date: Tue, 12 Nov 2024 12:24:26 +0100 Subject: [PATCH 4/4] fix linting issues --- mkdocs_static_i18n/plugin.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/mkdocs_static_i18n/plugin.py b/mkdocs_static_i18n/plugin.py index 261a151..3e93591 100644 --- a/mkdocs_static_i18n/plugin.py +++ b/mkdocs_static_i18n/plugin.py @@ -159,12 +159,9 @@ def on_page_markdown(self, markdown, page, config, files): if "pymdownx.details" in config["markdown_extensions"]: marker = r"(?:\?{3}\+?|!{3})" # Admonition or Details marker - RE = re.compile( - r'^(' - + marker - + r' ?)([\w\-]+(?: +[\w\-]+)*)(?: +"(.*?)")? *$' - ) # Copied from https://github.com/Python-Markdown/markdown/blob/master/markdown/extensions/admonition.py and modified for a single-line processing - # Adapted to match the details extension as well + # Copied from https://github.com/Python-Markdown/markdown/blob/master/markdown/extensions/admonition.py and modified for a single-line processing + # Adapted to match the details extension as well + RE = re.compile('^(' + marker + r' ?)([\w\-]+(?: +[\w\-]+)*)(?: +"(.*?)")? *$') def handle_admonition_translations(line): m = RE.match(line)