Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added admonitions translations to pymdownx.details #317

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions mkdocs_static_i18n/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

Expand Down
19 changes: 19 additions & 0 deletions tests/details/index.fr.md
Original file line number Diff line number Diff line change
@@ -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`
19 changes: 19 additions & 0 deletions tests/details/index.md
Original file line number Diff line number Diff line change
@@ -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`
46 changes: 46 additions & 0 deletions tests/test_details.py
Original file line number Diff line number Diff line change
@@ -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"<summary>([^<]*)", 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"<summary>([^<]*)", f.read())
assert(admonition_titles == ['Conseil', 'Conseil', 'Conseil', 'Conseil', 'Avertissement', 'Heey'])
Loading