diff --git a/mkdocs_static_i18n/plugin.py b/mkdocs_static_i18n/plugin.py index 55c0649..3e93591 100644 --- a/mkdocs_static_i18n/plugin.py +++ b/mkdocs_static_i18n/plugin.py @@ -151,14 +151,19 @@ 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( - 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(): + + marker = r"!{3}" # Admonition marker + if "pymdownx.details" in config["markdown_extensions"]: + marker = r"(?:\?{3}\+?|!{3})" # Admonition or Details marker + + # 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) if m: type = m.group(2) @@ -167,7 +172,13 @@ 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) 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..eb1e4f1 --- /dev/null +++ b/tests/details/index.fr.md @@ -0,0 +1,19 @@ +# 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 + +???+ 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` + +??? 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..f74851f --- /dev/null +++ b/tests/details/index.md @@ -0,0 +1,19 @@ +# Home page + +??? tip + Implicit title derived from admonition type, can be overriden by setting `admonition_translations` + +???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` + +??? 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..2fba3b9 --- /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', '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', 'Conseil', 'Conseil', 'Avertissement', 'Heey'])