aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--markdown/extensions/tables.py49
-rw-r--r--tests/extensions/extra/tables.html92
-rw-r--r--tests/extensions/extra/tables.txt39
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
<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>
@@ -375,4 +380,87 @@ Content Cell | Content Cell
<td>\\<code>code</code></td>
</tr>
</tbody>
-</table> \ No newline at end of file
+</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> \ 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