Skip to content

Fix regression of single column tables #540

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

Merged
merged 1 commit into from
Jan 26, 2017
Merged
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
49 changes: 40 additions & 9 deletions markdown/extensions/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
from ..blockprocessors import BlockProcessor
from ..util import etree
import re
PIPE_NONE = 0
PIPE_LEFT = 1
PIPE_RIGHT = 2


class TableProcessor(BlockProcessor):
Expand All @@ -41,17 +44,33 @@ def test(self, parent, block):
Keep border check and separator row do avoid repeating the work.
"""
is_table = False
header = [row.strip() for row in block.split('\n')[0:2]]
if len(header) == 2:
self.border = header[0].startswith('|')
row = self._split_row(header[0])
is_table = len(row) > 1
rows = [row.strip() for row in block.split('\n')]
if len(rows) > 1:
header0 = rows[0]
self.border = PIPE_NONE
if header0.startswith('|'):
self.border |= PIPE_LEFT
if self.RE_END_BORDER.search(header0) is not None:
self.border |= PIPE_RIGHT
row = self._split_row(header0)
row0_len = len(row)
is_table = row0_len > 1

# Each row in a single column table needs at least one pipe.
if not is_table and row0_len == 1 and self.border:
for index in range(1, len(rows)):
is_table = rows[index].startswith('|')
if not is_table:
is_table = self.RE_END_BORDER.search(rows[index]) is not None
if not is_table:
break

if is_table:
row = self._split_row(header[1])
is_table = len(row) > 1 and set(''.join(row)) <= set('|:- ')
row = self._split_row(rows[1])
is_table = (len(row) == row0_len) and set(''.join(row)) <= set('|:- ')
if is_table:
self.separator = row

return is_table

def run(self, parent, blocks):
Expand All @@ -78,8 +97,20 @@ def run(self, parent, blocks):
thead = etree.SubElement(table, 'thead')
self._build_row(header, thead, align)
tbody = etree.SubElement(table, 'tbody')
for row in rows:
self._build_row(row.strip(), tbody, align)
if len(rows) == 0:
# Handle empty table
self._build_empty_row(tbody, align)
else:
for row in rows:
self._build_row(row.strip(), tbody, align)

def _build_empty_row(self, parent, align):
"""Build an empty row."""
tr = etree.SubElement(parent, 'tr')
count = len(align)
while count:
etree.SubElement(tr, 'td')
count -= 1

def _build_row(self, row, parent, align):
""" Given a row of text, build table cells. """
Expand Down
92 changes: 90 additions & 2 deletions tests/extensions/extra/tables.html
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,12 @@ <h2>Table Tests</h2>
<th>Second Header</th>
</tr>
</thead>
<tbody></tbody>
<tbody>
<tr>
<td></td>
<td></td>
</tr>
</tbody>
</table>
<p>More inline code block tests</p>
<table>
Expand Down Expand Up @@ -375,4 +380,87 @@ <h2>Table Tests</h2>
<td>\\<code>code</code></td>
</tr>
</tbody>
</table>
</table>
<p>Single column tables</p>
<table>
<thead>
<tr>
<th>Is a Table</th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
<table>
<thead>
<tr>
<th>Is a Table</th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
<table>
<thead>
<tr>
<th>Is a Table</th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
<table>
<thead>
<tr>
<th>Is a Table</th>
</tr>
</thead>
<tbody>
<tr>
<td>row</td>
</tr>
</tbody>
</table>
<table>
<thead>
<tr>
<th>Is a Table</th>
</tr>
</thead>
<tbody>
<tr>
<td>row</td>
</tr>
</tbody>
</table>
<table>
<thead>
<tr>
<th>Is a Table</th>
</tr>
</thead>
<tbody>
<tr>
<td>row</td>
</tr>
</tbody>
</table>
<h2>| Is not a Table</h2>
<p>| row</p>
<h2>Is not a Table |</h2>
<p>row |</p>
<p>| Is not a Table
| --------------
row</p>
<p>Is not a Table |
-------------- |
row</p>
39 changes: 39 additions & 0 deletions tests/extensions/extra/tables.txt
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,42 @@ Should not be code | Should be code
------------------ | --------------
\`Not code\` | \\`code`
\\\`Not code\\\` | \\\\`code`

Single column tables

| Is a Table |
| ---------- |

| Is a Table
| ----------

Is a Table |
---------- |

| Is a Table |
| ---------- |
| row |

| Is a Table
| ----------
| row

Is a Table |
---------- |
row |

| Is not a Table
--------------
| row

Is not a Table |
--------------
row |

| Is not a Table
| --------------
row

Is not a Table |
-------------- |
row