aboutsummaryrefslogtreecommitdiffstats
path: root/markdown
diff options
context:
space:
mode:
authorIsaac Muse <faceless.shop@gmail.com>2017-01-26 11:25:06 -0700
committerWaylan Limberg <waylan.limberg@icloud.com>2017-01-26 13:25:06 -0500
commitab9f4c2dfb678c773387fe478f24bb59a0908e27 (patch)
treef549bc3b95388ef69b75d1a58d18bce7013ac1b6 /markdown
parent13c88972de9976137e9f523a80bfd71cdfb97224 (diff)
downloadmarkdown-ab9f4c2dfb678c773387fe478f24bb59a0908e27.tar.gz
markdown-ab9f4c2dfb678c773387fe478f24bb59a0908e27.tar.bz2
markdown-ab9f4c2dfb678c773387fe478f24bb59a0908e27.zip
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 `<tbody>` is invalid HTML. Fixes #539.
Diffstat (limited to 'markdown')
-rw-r--r--markdown/extensions/tables.py49
1 files changed, 40 insertions, 9 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. """