Skip to content

Commit

Permalink
Add colspan support
Browse files Browse the repository at this point in the history
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 GovReady#10
  • Loading branch information
hughdavenport committed Aug 3, 2021
1 parent c59969a commit 9cf784e
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion commonmark_extensions/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -211,6 +211,9 @@ def table(self, node, entering):
for row in part:
self.lit("<tr>\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"):
Expand All @@ -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
Expand Down

0 comments on commit 9cf784e

Please sign in to comment.