From 9cf784e80bf055abacb3ca06c2476bc934264fe6 Mon Sep 17 00:00:00 2001 From: Hugh Davenport Date: Wed, 4 Aug 2021 11:09:19 +1200 Subject: [PATCH 1/3] Add colspan support This adds support for having colspan generated in tables. The spec is yet to be updated with GFM. A cell with the sole contents of `-` will be ignored, and previous cells in the row will have a colspan attribute. Side effect would be that users can't have a cell with just `-` in it. Fixes #10 --- commonmark_extensions/tables.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/commonmark_extensions/tables.py b/commonmark_extensions/tables.py index d30ae89..567a901 100644 --- a/commonmark_extensions/tables.py +++ b/commonmark_extensions/tables.py @@ -171,7 +171,7 @@ def inner_parser(cell): for part in table_parts: for row in part: for i, cell in enumerate(row): - row[i] = inner_parser(cell) + row[i] = inner_parser(cell) if cell != "-" else None # Store the parsed table on the node. block.column_properties = column_properties @@ -211,6 +211,9 @@ def table(self, node, entering): for row in part: self.lit("\n") for colidx, cell in enumerate(row): + if cell is None: + continue + if part_tag == "thead": col_tag = "th" if self.options.get("table_th_scope"): @@ -224,6 +227,15 @@ def table(self, node, entering): if colidx in node.column_properties and "align" in node.column_properties[colidx]: col_attrs += ' align=\"' + node.column_properties[colidx]["align"] + "\"" + span = 1 + for nextid in range(colidx+1, len(row)): + if row[nextid] is None: + span += 1 + else: + break + if span > 1: + col_attrs += ' colspan=\"' + str(span) + '\"' + self.lit("<" + col_tag + col_attrs + ">") import copy From 13815d27fb1ba63f9b83605ef32a1c05a79a8cca Mon Sep 17 00:00:00 2001 From: Hugh Davenport Date: Wed, 4 Aug 2021 11:37:02 +1200 Subject: [PATCH 2/3] Colspan on multiline --- commonmark_extensions/tables.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/commonmark_extensions/tables.py b/commonmark_extensions/tables.py index 567a901..27747b1 100644 --- a/commonmark_extensions/tables.py +++ b/commonmark_extensions/tables.py @@ -143,6 +143,8 @@ def finalize(parser=None, block=None): # Multline mode. Merge this row with the previous one. for i in range(len(row)): if i < len(table_parts[-1][-1]): + if table_parts[-1][-1][i] == "-": + continue table_parts[-1][-1][i] += "\n" + row[i] else: table_parts[-1][-1].append(row[i]) From 91e905efbca5ccf140e3d010c20f2182508fc545 Mon Sep 17 00:00:00 2001 From: Hugh Davenport Date: Wed, 4 Aug 2021 11:38:57 +1200 Subject: [PATCH 3/3] Colspan add tests --- commonmark_extensions/tests.py | 155 +++++++++++++++++++++++++++++++++ 1 file changed, 155 insertions(+) diff --git a/commonmark_extensions/tests.py b/commonmark_extensions/tests.py index f9df165..2667506 100644 --- a/commonmark_extensions/tests.py +++ b/commonmark_extensions/tests.py @@ -196,6 +196,69 @@ def test_no_tbody_if_empty(self): """) + def test_colspan_header(self): + self.assertRender( +"""| foo | - | +| --- | --- | +| baz | bim |""", + +""" + + + + + + + + + + + +
foo
bazbim
""" + ) + + def test_colspan_body(self): + self.assertRender( +"""| foo | bar | +| --- | --- | +| baz | - |""", + +""" + + + + + + + + + + + +
foobar
baz
""" + ) + + def test_colspan_multiple(self): + self.assertRender( +"""| foo | bar | - |- | -| +| --- | --- | --- | --- | --- | +| baz | - | bim | - |-|""", + +""" + + + + + + + + + + + + +
foobar
bazbim
""" + ) class MultilineTablesTests(unittest.TestCase): """Test the multiline cell mode.""" @@ -236,6 +299,98 @@ def test_simple(self): """ ) + def test_colspan_header(self): + self.assertRender( +"""| foo | - | +| === | === | +| baz | bim | +| baz | bim |""", + +""" + + + + + + + + + + + +
foo

baz +baz

+

bim +bim

+
""" + ) + + def test_colspan_body(self): + self.assertRender( +"""| foo | bar | +| === | === | +| baz | - |""", + +""" + + + + + + + + + + + +
foobar
baz
""" + ) + + def test_colspan_multiple(self): + self.assertRender( +"""| foo | bar | - |- | -| +| === | === | === | === | === | +| baz | - | bim | - |-|""", + +""" + + + + + + + + + + + + +
foobar
bazbim
""" + ) + + def test_colspan_multiline_body(self): + self.assertRender( +"""| foo | bar | +| === | === | +| baz | - | +| baz | |""", + +""" + + + + + + + + + + + +
foobar

baz +baz

+
""" + ) class PlainTextRendererTests(unittest.TestCase): """Test the PlainTextRenderer cell mode."""