From da34930854d0759ada6fe2727f2e1a159f6b4fdb Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Tue, 17 Aug 2021 01:57:27 +0200 Subject: add {{lang-XX}}, {{IPA-XX}} and {{XXX}} templates --- setup.py | 1 + wikimini/templates/language.py | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/setup.py b/setup.py index 5af6a59..645d023 100644 --- a/setup.py +++ b/setup.py @@ -11,5 +11,6 @@ setuptools.setup( "requests", "mwparserfromhell", "tabulate", + "pycountry", ], ) diff --git a/wikimini/templates/language.py b/wikimini/templates/language.py index 052b7f0..e8ab738 100644 --- a/wikimini/templates/language.py +++ b/wikimini/templates/language.py @@ -1,6 +1,8 @@ """Language related templates.""" from . import registry +import pycountry + def tmpl_ipa(wikimini, obj): """Renders the ``{{IPA|...}}`` template.""" @@ -17,3 +19,42 @@ def tmpl_lang(wikimini, obj): registry.insert("lang", tmpl_lang) registry.insert("script", tmpl_lang) + + +def tmpl_lang_code(language_name): + """Creates a template renderer for a ``{{lang-xx|...}}`` template.""" + def inner(wikimini, obj): + return "{}: {}".format( + language_name, wikimini._convert(obj.params[0].value) + ) + return inner + + +def tmpl_ipa_code(language_name): + """Creates a template renderer for a ``{{IPA-xx|...}}`` template.""" + def inner(wikimini, obj): + return "{} pronunciation: [{}]".format( + language_name, wikimini._convert(obj.params[0].value) + ) + return inner + + +for language in pycountry.languages: + if hasattr(language, "alpha_2"): + registry.insert( + f"lang-{language.alpha_2}", tmpl_lang_code(language.name) + ) + registry.insert( + f"IPA-{language.alpha_2}", tmpl_ipa_code(language.name) + ) + + +def tmpl_country_flag(country): + """Creates a template renderer for ``{{BRA}}`` country flags.""" + def inner(wikimini, obj): + return country + return inner + + +for country in pycountry.countries: + registry.insert(country.alpha_3, tmpl_country_flag(country.name)) -- cgit v1.2.3