diff options
-rw-r--r-- | wikimini/__init__.py | 18 | ||||
-rw-r--r-- | wikimini/templates/language.py | 10 | ||||
-rw-r--r-- | wikimini/templates/quotes.py | 4 |
3 files changed, 18 insertions, 14 deletions
diff --git a/wikimini/__init__.py b/wikimini/__init__.py index 3e344fd..2ce96c8 100644 --- a/wikimini/__init__.py +++ b/wikimini/__init__.py @@ -98,7 +98,7 @@ class Wikimini: def convert( self, obj: Union[mwp.wikicode.Wikicode, mwp.nodes.Node], - ) -> Union[Document, Sequence[Node], Sequence[Block]]: + ) -> Union[Sequence[Node], Sequence[Block]]: """Function that converts and renders a node. This function is exposed for template implementors, for normal usage, @@ -107,14 +107,14 @@ class Wikimini: The input and output of this function is as follows: * If ``obj`` is a :class:`~mwparserfromhell.wikicode.Wikicode`, then - :meth:`convert` will return a :class:`document.Document`. + :meth:`convert` will return a list of :class:`document.Block`. * If ``obj`` is a :class:`~mwparserfromhell.nodes.Node`, then :meth:`convert` will return either a list of :class:`document.Node` or a list of :class:`document.Block`, depending on whether the converted object is inline (like a link), or a block object (like a quote). - Note that in the last case, the empty list ``[]`` might be returned, + Note that in either case, the empty list ``[]`` might be returned, indicating that the object should not be included in the output. Args: @@ -149,7 +149,7 @@ class Wikimini: elif isinstance(current[0], Node): for c in current: insert_into(document, c) - return Document(document) + return document elif isinstance(obj, mwp.nodes.heading.Heading): return [Heading(obj.level, obj.title.strip_code())] elif isinstance(obj, mwp.nodes.tag.Tag): @@ -177,7 +177,9 @@ class Wikimini: if str(node.tag) == "th": row_is_header = True parsed.append( - self.convert(node.contents).plain().strip() + self.convert_to_document(node.contents) + .plain() + .strip() ) if not row_is_header: rows.append(parsed) @@ -231,10 +233,10 @@ class Wikimini: Returns: The converted Document. """ + document = Document() # Avoid calling str() on the whole Wikicode here - if (isinstance(obj, mwp.wikicode.Wikicode) + if (len(obj.nodes) >= 2 and str(mwp.wikicode.Wikicode(obj.nodes[:2])) == "#REDIRECT "): - document = Document() title = str(obj.nodes[2].title) if "#" in title: title, section = title.split("#") @@ -246,7 +248,7 @@ class Wikimini: document.append(Paragraph([Plain(section)])) return document - document = self.convert(obj) + document.blocks = self.convert(obj) document.cleanup() return document diff --git a/wikimini/templates/language.py b/wikimini/templates/language.py index 22320da..7579c34 100644 --- a/wikimini/templates/language.py +++ b/wikimini/templates/language.py @@ -8,7 +8,7 @@ import pycountry def tmpl_ipa(wikimini, obj): """Renders the ``{{IPA|...}}`` template.""" return [Plain("pronounced [{}]".format( - wikimini.convert(obj.params[0].value).plain() + wikimini.convert_to_document(obj.params[0].value).plain() ))] @@ -17,7 +17,7 @@ registry.insert("IPA", tmpl_ipa) def tmpl_lang(wikimini, obj): """Renders the ``{{Lang|...}}`` template.""" - return wikimini.convert(obj.params[1].value).nodes() + return wikimini.convert_to_document(obj.params[1].value).nodes() registry.insert("lang", tmpl_lang) @@ -28,7 +28,8 @@ def tmpl_lang_code(language_name): """Creates a template renderer for a ``{{lang-xx|...}}`` template.""" def inner(wikimini, obj): return [Plain("{}: {}".format( - language_name, wikimini.convert(obj.params[0].value).plain() + language_name, + wikimini.convert_to_document(obj.params[0].value).plain(), ))] return inner @@ -37,7 +38,8 @@ def tmpl_ipa_code(language_name): """Creates a template renderer for a ``{{IPA-xx|...}}`` template.""" def inner(wikimini, obj): return [Plain("{} pronunciation: [{}]".format( - language_name, wikimini.convert(obj.params[0].value).plain() + language_name, + wikimini.convert_to_document(obj.params[0].value).plain(), ))] return inner diff --git a/wikimini/templates/quotes.py b/wikimini/templates/quotes.py index 00b82fb..5951543 100644 --- a/wikimini/templates/quotes.py +++ b/wikimini/templates/quotes.py @@ -9,7 +9,7 @@ def tmpl_quote(wikimini, obj): text = obj.get("text", None) if not text: return "" - content = wikimini.convert(text.value).nodes() + content = wikimini.convert_to_document(text.value).nodes() return [BlockQuote(Paragraph(content))] @@ -20,7 +20,7 @@ registry.insert("quote", tmpl_quote) def tmpl_cquote(wikimini, obj): """Renders the ``{{cquote|...}}`` template.""" text = obj.params[0] - content = wikimini.convert(text.value).nodes() + content = wikimini.convert_to_document(text.value).nodes() return [BlockQuote(Paragraph(content))] |