Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for colspan #11

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion commonmark_extensions/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down Expand Up @@ -171,7 +173,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 +213,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 +229,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
155 changes: 155 additions & 0 deletions commonmark_extensions/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,69 @@ def test_no_tbody_if_empty(self):
</thead>
</table>""")

def test_colspan_header(self):
self.assertRender(
"""| foo | - |
| --- | --- |
| baz | bim |""",

"""<table>
<thead>
<tr>
<th colspan="2">foo</th>
</tr>
</thead>
<tbody>
<tr>
<td>baz</td>
<td>bim</td>
</tr>
</tbody>
</table>"""
)

def test_colspan_body(self):
self.assertRender(
"""| foo | bar |
| --- | --- |
| baz | - |""",

"""<table>
<thead>
<tr>
<th>foo</th>
<th>bar</th>
</tr>
</thead>
<tbody>
<tr>
<td colspan="2">baz</td>
</tr>
</tbody>
</table>"""
)

def test_colspan_multiple(self):
self.assertRender(
"""| foo | bar | - |- | -|
| --- | --- | --- | --- | --- |
| baz | - | bim | - |-|""",

"""<table>
<thead>
<tr>
<th>foo</th>
<th colspan="4">bar</th>
</tr>
</thead>
<tbody>
<tr>
<td colspan="2">baz</td>
<td colspan="3">bim</td>
</tr>
</tbody>
</table>"""
)

class MultilineTablesTests(unittest.TestCase):
"""Test the multiline cell mode."""
Expand Down Expand Up @@ -236,6 +299,98 @@ def test_simple(self):
</table>"""
)

def test_colspan_header(self):
self.assertRender(
"""| foo | - |
| === | === |
| baz | bim |
| baz | bim |""",

"""<table>
<thead>
<tr>
<th colspan="2">foo</th>
</tr>
</thead>
<tbody>
<tr>
<td><p>baz
baz</p>
</td>
<td><p>bim
bim</p>
</td>
</tr>
</tbody>
</table>"""
)

def test_colspan_body(self):
self.assertRender(
"""| foo | bar |
| === | === |
| baz | - |""",

"""<table>
<thead>
<tr>
<th>foo</th>
<th>bar</th>
</tr>
</thead>
<tbody>
<tr>
<td colspan="2">baz</td>
</tr>
</tbody>
</table>"""
)

def test_colspan_multiple(self):
self.assertRender(
"""| foo | bar | - |- | -|
| === | === | === | === | === |
| baz | - | bim | - |-|""",

"""<table>
<thead>
<tr>
<th>foo</th>
<th colspan="4">bar</th>
</tr>
</thead>
<tbody>
<tr>
<td colspan="2">baz</td>
<td colspan="3">bim</td>
</tr>
</tbody>
</table>"""
)

def test_colspan_multiline_body(self):
self.assertRender(
"""| foo | bar |
| === | === |
| baz | - |
| baz | |""",

"""<table>
<thead>
<tr>
<th>foo</th>
<th>bar</th>
</tr>
</thead>
<tbody>
<tr>
<td colspan="2"><p>baz
baz</p>
</td>
</tr>
</tbody>
</table>"""
)

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