diff options
author | Daniel Schadt <kingdread@gmx.de> | 2021-08-21 22:53:00 +0200 |
---|---|---|
committer | Daniel Schadt <kingdread@gmx.de> | 2021-08-21 22:53:00 +0200 |
commit | f384001e0818968306917566c2007c21a7a13613 (patch) | |
tree | 27730929b86820a9c9161dd29b2cfc4ddbbceb4a | |
parent | 03d253975e9400943083770da6d16c127355fc2a (diff) | |
download | wikimini-f384001e0818968306917566c2007c21a7a13613.tar.gz wikimini-f384001e0818968306917566c2007c21a7a13613.tar.bz2 wikimini-f384001e0818968306917566c2007c21a7a13613.zip |
Move table parsing out to separate function
Just to clean up the code a bit :-)
-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. |