diff options
author | Daniel Schadt <kingdread@gmx.de> | 2021-08-20 22:04:44 +0200 |
---|---|---|
committer | Daniel Schadt <kingdread@gmx.de> | 2021-08-20 22:04:44 +0200 |
commit | a9a7c6c77f3b6078e317d455b696ce76272b88cb (patch) | |
tree | 4d7649a3efe54378141e5fba6778281d2da4b8b4 /wikimini/templates | |
parent | 469353899ae6d7d0c0b7b105c24baaa4841c6328 (diff) | |
parent | a114ad49db792ec190a5cb6c96acc47669ac4b03 (diff) | |
download | wikimini-a9a7c6c77f3b6078e317d455b696ce76272b88cb.tar.gz wikimini-a9a7c6c77f3b6078e317d455b696ce76272b88cb.tar.bz2 wikimini-a9a7c6c77f3b6078e317d455b696ce76272b88cb.zip |
Merge branch 'document-repr'
Diffstat (limited to 'wikimini/templates')
-rw-r--r-- | wikimini/templates/__init__.py | 7 | ||||
-rw-r--r-- | wikimini/templates/cite.py | 3 | ||||
-rw-r--r-- | wikimini/templates/convert.py | 9 | ||||
-rw-r--r-- | wikimini/templates/language.py | 21 | ||||
-rw-r--r-- | wikimini/templates/mainlinks.py | 6 | ||||
-rw-r--r-- | wikimini/templates/quotes.py | 12 | ||||
-rw-r--r-- | wikimini/templates/various.py | 5 |
7 files changed, 36 insertions, 27 deletions
diff --git a/wikimini/templates/__init__.py b/wikimini/templates/__init__.py index 9e983e1..360b3fa 100644 --- a/wikimini/templates/__init__.py +++ b/wikimini/templates/__init__.py @@ -2,9 +2,9 @@ This module contains functions that mimic Wikipedia's templates. -A template is a function that takes the :class:`~wikimini.Wikimini` instance and the -:class:`~mwparserfromhell.nodes.template.Template` node to convert, and returns -a string with the template output (see :const:`Template`). +A template is a function that takes the :class:`~wikimini.Wikimini` instance +and the :class:`~mwparserfromhell.nodes.template.Template` node to convert, and +returns a string with the template output (see :const:`Template`). """ from typing import Callable, Optional @@ -31,6 +31,7 @@ class Registry: Returns: The template if found, or :any:`None`. """ + name = name.strip() # Are templates case-sensitive? # Yes, except usually the first letter. # (https://en.wikipedia.org/wiki/Help:A_quick_guide_to_templates#FAQ) diff --git a/wikimini/templates/cite.py b/wikimini/templates/cite.py index ac4f597..e342a5f 100644 --- a/wikimini/templates/cite.py +++ b/wikimini/templates/cite.py @@ -1,5 +1,6 @@ """Citation related templates.""" from . import registry +from ..document import Plain def tmpl_citation(wikimini, obj): @@ -23,7 +24,7 @@ def tmpl_citation(wikimini, obj): names.append(last) elif first: names.append(first) - return "{} ({})".format(title, "; ".join(names)) + return [Plain("{} ({})".format(title, "; ".join(names)))] for name in ["cite", "citation", "cite arXiv", "cite AV media", "cite book", diff --git a/wikimini/templates/convert.py b/wikimini/templates/convert.py index a7a3f44..8bab782 100644 --- a/wikimini/templates/convert.py +++ b/wikimini/templates/convert.py @@ -1,20 +1,21 @@ """Implementations for the unit conversion templates.""" from . import registry +from ..document import Plain def tmpl_convert(wikimini, obj): """Renders the ``{{convert|...}}`` template.""" if str(obj.params[1]) in {"-", "to"}: - return "{0}{3} {1} {2}{3}".format( + return [Plain("{0}{3} {1} {2}{3}".format( obj.params[0].value.strip_code(), obj.params[1].value.strip_code(), obj.params[2].value.strip_code(), obj.params[3].value.strip_code(), - ) - return "{}{}".format( + ))] + return [Plain("{}{}".format( obj.params[0].value.strip_code(), obj.params[1].value.strip_code(), - ) + ))] registry.insert("convert", tmpl_convert) diff --git a/wikimini/templates/language.py b/wikimini/templates/language.py index e8ab738..22320da 100644 --- a/wikimini/templates/language.py +++ b/wikimini/templates/language.py @@ -1,12 +1,15 @@ """Language related templates.""" from . import registry +from ..document import Plain import pycountry def tmpl_ipa(wikimini, obj): """Renders the ``{{IPA|...}}`` template.""" - return "pronounced [{}]".format(wikimini._convert(obj.params[0].value)) + return [Plain("pronounced [{}]".format( + wikimini.convert(obj.params[0].value).plain() + ))] registry.insert("IPA", tmpl_ipa) @@ -14,7 +17,7 @@ registry.insert("IPA", tmpl_ipa) def tmpl_lang(wikimini, obj): """Renders the ``{{Lang|...}}`` template.""" - return wikimini._convert(obj.params[1].value) + return wikimini.convert(obj.params[1].value).nodes() registry.insert("lang", tmpl_lang) @@ -24,18 +27,18 @@ 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 [Plain("{}: {}".format( + language_name, wikimini.convert(obj.params[0].value).plain() + ))] 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 [Plain("{} pronunciation: [{}]".format( + language_name, wikimini.convert(obj.params[0].value).plain() + ))] return inner @@ -52,7 +55,7 @@ for language in pycountry.languages: def tmpl_country_flag(country): """Creates a template renderer for ``{{BRA}}`` country flags.""" def inner(wikimini, obj): - return country + return [Plain(country)] return inner diff --git a/wikimini/templates/mainlinks.py b/wikimini/templates/mainlinks.py index ffcbc5e..3d945ed 100644 --- a/wikimini/templates/mainlinks.py +++ b/wikimini/templates/mainlinks.py @@ -1,14 +1,16 @@ """Renders templates that link to further articles.""" from . import registry +from ..document import Paragraph, Plain, BlockLink + def tmpl_main(wikimini, obj): """Renders the ``{{main|...}}`` template.""" links = [ - "=> {} {}".format(wikimini.page_url(str(t.value)), t.value) + BlockLink(wikimini.page_url(str(t.value)), t.value.strip_code()) for t in obj.params ] - return "Main articles:\n{}\n".format("\n".join(links)) + return [Paragraph([Plain("Main articles:")])] + links registry.insert("main", tmpl_main) diff --git a/wikimini/templates/quotes.py b/wikimini/templates/quotes.py index b51d92d..00b82fb 100644 --- a/wikimini/templates/quotes.py +++ b/wikimini/templates/quotes.py @@ -1,15 +1,16 @@ """Renders various quote related templates.""" from . import registry +from ..document import BlockQuote, Paragraph + def tmpl_quote(wikimini, obj): """Renders the ``{{blockquote|...}}`` template.""" text = obj.get("text", None) if not text: return "" - content = wikimini._convert(text.value) - lines = content.split("\n") - return "\n".join(f"> {line}" for line in lines) + content = wikimini.convert(text.value).nodes() + return [BlockQuote(Paragraph(content))] registry.insert("blockquote", tmpl_quote) @@ -19,9 +20,8 @@ registry.insert("quote", tmpl_quote) def tmpl_cquote(wikimini, obj): """Renders the ``{{cquote|...}}`` template.""" text = obj.params[0] - content = wikimini._convert(text.value) - lines = content.split("\n") - return "\n".join(f"> {line}" for line in lines) + content = wikimini.convert(text.value).nodes() + return [BlockQuote(Paragraph(content))] registry.insert("cquote", tmpl_cquote) diff --git a/wikimini/templates/various.py b/wikimini/templates/various.py index 8c6e0d5..6076ac4 100644 --- a/wikimini/templates/various.py +++ b/wikimini/templates/various.py @@ -1,16 +1,17 @@ """Various small templates.""" from . import registry +from ..document import Plain def tmpl_reign(wikimini, obj): """Renders the ``{{reign|...}}`` template.""" if not obj.params: - return "r. " + return [Plain("r. ")] first = obj.params[0].value.strip_code().strip() or "?" second = "" if len(obj.params) > 1: second = obj.params[1].value.strip_code().strip() - return f"r. {first} – {second}" + return [Plain(f"r. {first} – {second}")] registry.insert("reign", tmpl_reign) |