diff options
-rw-r--r-- | wikimini/__init__.py | 51 |
1 files changed, 26 insertions, 25 deletions
diff --git a/wikimini/__init__.py b/wikimini/__init__.py index 2ce96c8..6c2f2a6 100644 --- a/wikimini/__init__.py +++ b/wikimini/__init__.py @@ -163,31 +163,7 @@ class Wikimini: elif str(obj.tag) == "ref": return [] elif str(obj.tag) == "table": - rows = [] - header = () - for row in obj.contents.nodes: - if str(getattr(row, "tag", "")) != "tr": - continue - nodes = row.contents.nodes - parsed = [] - row_is_header = False - for node in nodes: - if str(getattr(node, "tag", "")) not in {"td", "th"}: - continue - if str(node.tag) == "th": - row_is_header = True - parsed.append( - self.convert_to_document(node.contents) - .plain() - .strip() - ) - if not row_is_header: - rows.append(parsed) - else: - header = parsed - return [Verbatim( - tabulate(rows, header, tablefmt=self.table_format) - )] + return self._convert_table(obj) else: return default(obj) elif isinstance(obj, mwp.nodes.template.Template): @@ -214,6 +190,31 @@ class Wikimini: else: return default(obj) + def _convert_table(self, obj: mwp.nodes.tag.Tag) -> Sequence[Block]: + rows = [] + header = [] + for row in obj.contents.nodes: + if str(getattr(row, "tag", "")) != "tr": + continue + nodes = row.contents.nodes + parsed = [] + row_is_header = False + for node in nodes: + if str(getattr(node, "tag", "")) not in {"td", "th"}: + continue + if str(node.tag) == "th": + row_is_header = True + parsed.append( + self.convert_to_document(node.contents).plain().strip() + ) + if not row_is_header: + rows.append(parsed) + else: + header = parsed + return [Verbatim( + tabulate(rows, header, tablefmt=self.table_format) + )] + def convert_to_document(self, obj: mwp.wikicode.Wikicode) -> Document: """Try to turn the given object into a sensible :class:`~document.Document` representation. |