From 9b5b2bda1e43e659f142bb88a6b8138962e754e9 Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Fri, 20 Aug 2021 11:10:14 +0200 Subject: Rework ItemList/BlockQuote to hold Paragraph A List[Node] is basically a Paragraph, and we already delegated some of the methods to Paragraph (see ItemList.cleanup). Therefore, it only made sense to rework ItemList and BlockQuote to hold a Paragraph instead of a List[Node]. --- wikimini/document.py | 28 ++++++++++++---------------- wikimini/templates/quotes.py | 6 +++--- 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/wikimini/document.py b/wikimini/document.py index fe979fc..7c459b6 100644 --- a/wikimini/document.py +++ b/wikimini/document.py @@ -283,12 +283,12 @@ class ItemList(Block): """A list of elements. Attributes: - items: The list of items. Each item is a list of inline :class:`Node`. + items: The list of items. Each item is a :class:`Paragraph`. ordered: A flag indicating whether the list should be an ordered (numbered) list. """ __slots__ = ("items", "ordered") - items: List[List[Node]] + items: List[Paragraph] ordered: bool def __bool__(self): @@ -296,7 +296,7 @@ class ItemList(Block): def new_item(self): """Start a new item.""" - self.items.append([]) + self.items.append(Paragraph([])) def append(self, node): if not self.items: @@ -304,20 +304,17 @@ class ItemList(Block): self.items[-1].append(node) def plain(self): - return "\n".join( - "".join(i.plain() for i in item) for item in self.items - ) + return "\n".join(paragraph.plain() for paragraph in self.items) def to_nodes(self): - return [node for item in self.items for node in item] + return [node for item in self.items for node in item.nodes] def cleanup(self): i = 0 while i < len(self.items): - p = Paragraph(self.items[i]) - p.cleanup() - if p: - self.items[i] = p.to_nodes() + paragraph = self.items[i] + paragraph.cleanup() + if paragraph: i += 1 else: del self.items[i] @@ -328,11 +325,10 @@ class BlockQuote(Block): """A quote. Attributes: - nodes: The content of the blockquote, similar to - :attr:`Paragraph.nodes`. + nodes: The content of the blockquote. """ __slots__ = ("nodes",) - nodes: List[Node] + nodes: Paragraph def __bool__(self): return bool(self.nodes) @@ -341,10 +337,10 @@ class BlockQuote(Block): self.nodes.append(node) def plain(self): - return "".join(node.plain() for node in self.nodes) + return self.nodes.plain() def to_nodes(self): - return self.nodes + return self.nodes.to_nodes() @dataclass diff --git a/wikimini/templates/quotes.py b/wikimini/templates/quotes.py index ef7f297..00b82fb 100644 --- a/wikimini/templates/quotes.py +++ b/wikimini/templates/quotes.py @@ -1,7 +1,7 @@ """Renders various quote related templates.""" from . import registry -from ..document import BlockQuote +from ..document import BlockQuote, Paragraph def tmpl_quote(wikimini, obj): @@ -10,7 +10,7 @@ def tmpl_quote(wikimini, obj): if not text: return "" content = wikimini.convert(text.value).nodes() - return [BlockQuote(content)] + return [BlockQuote(Paragraph(content))] registry.insert("blockquote", tmpl_quote) @@ -21,7 +21,7 @@ def tmpl_cquote(wikimini, obj): """Renders the ``{{cquote|...}}`` template.""" text = obj.params[0] content = wikimini.convert(text.value).nodes() - return [BlockQuote(content)] + return [BlockQuote(Paragraph(content))] registry.insert("cquote", tmpl_cquote) -- cgit v1.2.3