From ab9f4c2dfb678c773387fe478f24bb59a0908e27 Mon Sep 17 00:00:00 2001 From: Isaac Muse Date: Thu, 26 Jan 2017 11:25:06 -0700 Subject: Fix regression of single column tables (#540) Single column tables are valid tables, so add back in the accidentally removed functionality of allowing single column tables, but with one exception -- table bodies should not render empty (an empty `` is invalid HTML. Fixes #539. --- markdown/extensions/tables.py | 49 ++++++++++++++++---- tests/extensions/extra/tables.html | 92 +++++++++++++++++++++++++++++++++++++- tests/extensions/extra/tables.txt | 39 ++++++++++++++++ 3 files changed, 169 insertions(+), 11 deletions(-) diff --git a/markdown/extensions/tables.py b/markdown/extensions/tables.py index ebe6ffa..ec3b6ac 100644 --- a/markdown/extensions/tables.py +++ b/markdown/extensions/tables.py @@ -21,6 +21,9 @@ from . import Extension from ..blockprocessors import BlockProcessor from ..util import etree import re +PIPE_NONE = 0 +PIPE_LEFT = 1 +PIPE_RIGHT = 2 class TableProcessor(BlockProcessor): @@ -41,17 +44,33 @@ class TableProcessor(BlockProcessor): 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): @@ -78,8 +97,20 @@ class TableProcessor(BlockProcessor): 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. """ diff --git a/tests/extensions/extra/tables.html b/tests/extensions/extra/tables.html index 2418c98..25dee48 100644 --- a/tests/extensions/extra/tables.html +++ b/tests/extensions/extra/tables.html @@ -167,7 +167,12 @@ Content Cell | Content Cell Second Header - + + + + + +

More inline code block tests

@@ -375,4 +380,87 @@ Content Cell | Content Cell -
\\code
\ No newline at end of file + +

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

\ No newline at end of file diff --git a/tests/extensions/extra/tables.txt b/tests/extensions/extra/tables.txt index d766224..2dc4967 100644 --- a/tests/extensions/extra/tables.txt +++ b/tests/extensions/extra/tables.txt @@ -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 -- cgit v1.2.3