fix: translate indented admonitions
This PR allows detecting indented admonitions and translating them.
Closes #328
It also modifies
pyproject.tomlto be able to pass args topytest.
[tool.hatch.envs.test.scripts]
test = [
- "pytest -xs",
+ "pytest -xs {args:tests}",
]
Thanks a lot @joapuiib , that's super clear
@ShimYama also states:
Also, while admonitions are not case-sensitive, their translations are case-sensitive.
What would you have to say about that please?
Also, while admonitions are not case-sensitive, their translations are case-sensitive.
I did miss that completely. I came across the indenting problem, and I found the issue after I wrote the fix.
Let me try to fix that as well.
@ultrabug I think I've fixed it by transforming the type to lowercase before checking if a translation is available at admonitions_translation.
if (
not title or title.strip() == ""
) and admonition_type.lower() in admonition_translations:
new_title = admonition_translations[admonition_type.lower()]
line = f'{indent}{marker}{admonition_type} "{new_title}"'
A 'limitation' would be that admonitions_translation keys should be lowercase.
"admonition_translations": {
"tip": "Conseil",
"warning": "Avertissement",
}
@joapuiib out of my head I think we can overcome this if:
- we update the config parsing part of the admonition_translations and force
.title()onadmonition_translationskeys - make
admonition_type = admonition_type = m.group("type").title()
?
we update the config parsing part of the admonition_translations and force .title() on admonition_translations keys
I thought about this option and I think it's a good approach.
make admonition_type = admonition_type = m.group("type").title()
This would also work, but the translated markdown would be always in lowercase, even if the user writes it in uppercase. I don't know about admonitions extension implementation, but in my opinion is better to leave it as the user has written it.
@ultrabug Should we force .title() or .lower()?
I'd rather store admonition_types keys as lowercase and leave the admonition_type = m.group("type") as written by the user in the document.
diff --git a/mkdocs_static_i18n/plugin.py b/mkdocs_static_i18n/plugin.py
index b8ccd92..5dda0e9 100644
--- a/mkdocs_static_i18n/plugin.py
+++ b/mkdocs_static_i18n/plugin.py
@@ -47,6 +47,11 @@ class I18n(ExtendedPlugin):
)
admonition_translations = self.current_language_config.admonition_translations or {}
+
+ admonition_translations = {
+ k.lower(): v for k, v in admonition_translations.items()
+ }
+
if len(admonition_translations) > 0 and (
"markdown_extensions" not in config or "admonition" not in config["markdown_extensions"]
):
sorry @joapuiib
Ok for lower() ofc
@ultrabug I've applied the previous patch in order to force lower() in all admonition_translations keys.
Both problems should be fixed: indentation and case-insensitive types.
Thanks a lot @joapuiib