From 3e2bc272f45fb22271a5738e408fa50edb1fae04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Schoentgen?= Date: Thu, 19 Jan 2023 10:38:23 +0100 Subject: [PATCH] fix #1591: [PT] Handle implicit "trad" in "etimo" template --- wikidict/lang/pt/__init__.py | 47 +++++++++++++++++++++++++++--------- wikidict/user_functions.py | 4 ++- 2 files changed, 39 insertions(+), 12 deletions(-) diff --git a/wikidict/lang/pt/__init__.py b/wikidict/lang/pt/__init__.py index 22f6f33f7..1fe282764 100644 --- a/wikidict/lang/pt/__init__.py +++ b/wikidict/lang/pt/__init__.py @@ -189,18 +189,33 @@ def last_template_handler( >>> last_template_handler(["escopo", "pt", "Catolicismo", "cristianismo", "cristianismo"], "pt") '(Catolicismo, cristianismo e cristianismo)' - >>> last_template_handler(["etimo", "la", "canem"], "pt") - 'canem' + >>> last_template_handler(["etimo", "la", "myrmecophaga"], "pt") + 'myrmecophaga' + >>> last_template_handler(["etimo", "grc", "στεγάζω", "(stegházo)"], "pt") + 'στεγάζω (stegházo)' + >>> last_template_handler(["etimo", "orv", "ꙗзꙑкъ", "(jazykŭ)", "língua"], "pt") + 'ꙗзꙑкъ (jazykŭ) “língua”' + >>> last_template_handler(["etimo", "orv", "ꙗзꙑкъ", "transcr=(jazykŭ)", "trad=língua"], "pt") + 'ꙗзꙑкъ (jazykŭ) “língua”' >>> last_template_handler(["etimo", "la", "canis", "trad=cão"], "pt") 'canis “cão”' >>> last_template_handler(["etimo", "la", "duos", "(duōs)"], "pt") 'duos (duōs)' - >>> last_template_handler(["étimo", "grc", "ἄντρον", "transcr=ánton", "trad=caverna"], "pt") - 'ἄντρον (ánton) “caverna”' - >>> last_template_handler(["étimo", "grc", "ἄντρον", "transl=ánton", "sign=caverna"], "pt") + >>> last_template_handler(["etimo", "grc", "ἄντρον", "transcr=ánton", "trad=caverna"], "pt") + 'ἄντρον (ánton) “caverna”' + >>> last_template_handler(["etimo", "grc", "ἄντρον", "transl=ánton", "sign=caverna"], "pt") 'ἄντρον (ánton) (“caverna”)' - >>> last_template_handler(["étimo", "la", "abūsus", "sign=abuso"], "pt") + >>> last_template_handler(["etimo", "la", "abūsus", "sign=abuso"], "pt") 'abūsus (“abuso”)' + >>> last_template_handler(["etimo", "la", "nomen substantivum", "", "nome autônomo"], "pt") + 'nomen substantivum “nome autônomo”' + + >>> last_template_handler(["étimo", "la", "canem"], "pt") + 'canem' + >>> last_template_handler(["étimo", "la", "canem", "canum"], "pt") + 'canum' + >>> last_template_handler(["étimo", "la", "canem", "canum", "canos"], "pt") + 'canum' >>> last_template_handler(["etm", "la", "pt"], "pt") 'latim' @@ -270,6 +285,7 @@ def last_template_handler( extract_keywords_from, italic, lookup_italic, + parenthesis, small, term, ) @@ -292,21 +308,30 @@ def last_template_handler( words = [lookup_italic(p, "pt") for p in parts if p not in langs] return term(concat(words, sep=", ", last_sep=" e ")) - if tpl in ("etimo", "étimo"): + if tpl == "etimo": src = parts.pop(0) # Remove the lang - phrase = italic(parts.pop(0)) - if parts: - phrase += f" {parts[0]}" + phrase = italic(parts.pop(0)) # The etimo + if parts: # Implicit transcr + transcr = parts.pop(0) + phrase += f" {parenthesis(transcr)}" if transcr else "" + if parts: # Implicit trad + phrase += f" “{parts.pop(0)}”" + if data["transcr"]: - phrase += " " + italic(f"({data['transcr']})") + phrase += f" {parenthesis(data['transcr'])}" if data["transl"]: phrase += " " + italic(f"({data['transl']})") if data["trad"]: phrase += f" “{data['trad']}”" if data["sign"]: phrase += f" (“{data['sign']}”)" + return phrase + if tpl == "étimo": + src = parts.pop(0) # Remove the lang + return italic(parts[1 if len(parts) >= 2 else 0]) + if tpl == "etm": return langs[parts[0]] diff --git a/wikidict/user_functions.py b/wikidict/user_functions.py index ce8e84532..22ad523ae 100644 --- a/wikidict/user_functions.py +++ b/wikidict/user_functions.py @@ -402,8 +402,10 @@ def parenthesis(text: str) -> str: >>> parenthesis("foo") '(foo)' + >>> parenthesis("(foo)") + '(foo)' """ - return f"({text})" + return text if text.startswith("(") else f"({text})" def person(word: str, parts: List[str]) -> str: