Skip to content

Enable tables without headers #508

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

Closed
wants to merge 6 commits into from
Closed
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
61 changes: 44 additions & 17 deletions markdown/extensions/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,56 @@ class TableProcessor(BlockProcessor):

def test(self, parent, block):
rows = block.split('\n')
return (len(rows) > 1 and '|' in rows[0] and
'|' in rows[1] and '-' in rows[1] and
rows[1].strip()[0] in ['|', ':', '-'] and
set(rows[1]) <= set('|:- '))

if len(rows) <= 1:
return False

is_table_with_header = (
'|' in rows[0] and '|' in rows[1] and '-' in rows[1] and
rows[1].strip()[0] in ['|', ':', '-'] and
set(rows[1]) <= set('|:- ')
)

is_table_without_header = (
'|' in rows[0] and '-' in rows[0] and
rows[0].strip()[0] in ['|', ':', '-'] and
set(rows[0]) <= set('|:- ')
)

return is_table_with_header or is_table_without_header

def run(self, parent, blocks):
""" Parse a table block and build table. """
block = blocks.pop(0).split('\n')
header = block[0].strip()
seperator = block[1].strip()
rows = [] if len(block) < 3 else block[2:]

if '-' in block[0] and set(block[0]) <= set('|:- '): # no header
header = None
separator = block[0].strip()
rows = [] if len(block) < 2 else block[1:]
else:
header = block[0].strip()
separator = block[1].strip()
rows = [] if len(block) < 3 else block[2:]

# Get format type (bordered by pipes or not)
border = False
if header.startswith('|'):
if (header and header.startswith('|')) or separator.startswith('|'):
border = True
# Get alignment of columns

align = self._get_column_align(separator, border)

# Build table
table = etree.SubElement(parent, 'table')
if header:
thead = etree.SubElement(table, 'thead')
self._build_row(header, thead, align, border)
tbody = etree.SubElement(table, 'tbody')
for row in rows:
self._build_row(row.strip(), tbody, align, border)

def _get_column_align(self, separator, border):
align = []
for c in self._split_row(seperator, border):
for c in self._split_row(separator, border):
c = c.strip()
if c.startswith(':') and c.endswith(':'):
align.append('center')
Expand All @@ -55,13 +87,8 @@ def run(self, parent, blocks):
align.append('right')
else:
align.append(None)
# Build table
table = etree.SubElement(parent, 'table')
thead = etree.SubElement(table, 'thead')
self._build_row(header, thead, align, border)
tbody = etree.SubElement(table, 'tbody')
for row in rows:
self._build_row(row.strip(), tbody, align, border)

return align

def _build_row(self, row, parent, align, border):
""" Given a row of text, build table cells. """
Expand Down
9 changes: 9 additions & 0 deletions tests/extensions/extra/tables.html
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,15 @@ <h2>Table Tests</h2>
</tr>
</tbody>
</table>
<p>A table without a header:</p>
<table>
<tbody>
<tr>
<td>foo</td>
<td>bar</td>
</tr>
</tbody>
</table>
<p>Lists are not tables</p>
<ul>
<li>this | should | not</li>
Expand Down
5 changes: 5 additions & 0 deletions tests/extensions/extra/tables.txt
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ foo | bar
--- | ---
foo | (`bar`) and `baz`.

A table without a header:

---|---
foo|bar

Lists are not tables

- this | should | not
Expand Down