Skip to content
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

Added header support for wiki tables #335

Merged
merged 9 commits into from Nov 25, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -12,3 +12,4 @@ sandbox/*.html
__pycache__
.tox
*.egg-info
*.idea
43 changes: 32 additions & 11 deletions lib/markdown2.py
Expand Up @@ -217,6 +217,7 @@ def __init__(self, html4tags=False, tab_width=4, safe_mode=None,
else:
self.empty_element_suffix = " />"
self.tab_width = tab_width
self.tab = tab_width * " "

# For compatibility with earlier markdown2.py and with
# markdown.py's safe_mode being a boolean,
Expand Down Expand Up @@ -1070,23 +1071,43 @@ def _do_tables(self, text):

def _wiki_table_sub(self, match):
ttext = match.group(0).strip()
# print 'wiki table: %r' % match.group(0)
# print('wiki table: %r' % match.group(0))
rows = []
for line in ttext.splitlines(0):
line = line.strip()[2:-2].strip()
row = [c.strip() for c in re.split(r'(?<!\\)\|\|', line)]
rows.append(row)
# from pprint import pprint
# pprint(rows)
hlines = ['<table%s>' % self._html_class_str_from_tag('table'), '<tbody>']
for row in rows:
hrow = ['<tr>']
for cell in row:
hrow.append('<td>')
hrow.append(self._run_span_gamut(cell))
hrow.append('</td>')
hrow.append('</tr>')
hlines.append(''.join(hrow))
hlines += ['</tbody>', '</table>']
hlines = []

def add_hline(line, indents=0):
hlines.append((self.tab * indents) + line)

def format_cell(text):
return self._run_span_gamut(re.sub(r"^\s*~", "", cell).strip(" "))

add_hline('<table%s>' % self._html_class_str_from_tag('table'))
# Check if first cell of first row is a header cell. If so, assume the whole row is a header row.
if rows and rows[0] and re.match(r"^\s*~", rows[0][0]):
add_hline('<thead>', 1)
add_hline('<tr>', 2)
for cell in rows[0]:
add_hline("<th>{}</th>".format(format_cell(cell)), 3)
add_hline('</tr>', 2)
add_hline('</thead>', 1)
# Only one header row allowed.
rows = rows[1:]
# If no more rows, don't create a tbody.
if rows:
add_hline('<tbody>', 1)
for row in rows:
add_hline('<tr>', 2)
for cell in row:
add_hline('<td>{}</td>'.format(format_cell(cell)), 3)
add_hline('</tr>', 2)
add_hline('</tbody>', 1)
add_hline('</table>')
return '\n'.join(hlines) + '\n'

def _do_wiki_tables(self, text):
Expand Down
2 changes: 1 addition & 1 deletion test/php-markdown-extra-cases/Tables.html
Expand Up @@ -307,4 +307,4 @@ <h1>Missing tailing pipe</h1>
<td>Cell</td>
</tr>
</tbody>
</table>
</table>
1 change: 1 addition & 0 deletions test/php-markdown-extra-cases/Tables.opts
@@ -0,0 +1 @@
{"extras": ["tables"]}
9 changes: 4 additions & 5 deletions test/php-markdown-extra-cases/Tables.text
Expand Up @@ -73,10 +73,10 @@ Table alignement (alternate spacing):
| A | B |
| C | |

Header 1 | Header 2
--------- | ---------
A | B
| D
| Header 1 | Header 2
| --------- | ---------
| A | B
| | D

* * *

Expand All @@ -101,4 +101,3 @@ Header 1 | Header 2 |
--------- | --------- |
Cell | Cell |
Cell | Cell

30 changes: 24 additions & 6 deletions test/tm-cases/html_classes.html
Expand Up @@ -18,12 +18,30 @@
</table>

<table class="table table-striped">
<tbody>
<tr><td><em>Year</em></td><td><em>Temperature (low)</em></td><td><em>Temperature (high)</em></td></tr>
<tr><td>1900</td><td>-10</td><td>25</td></tr>
<tr><td>1910</td><td>-15</td><td>30</td></tr>
<tr><td>1920</td><td>-10</td><td>32</td></tr>
</tbody>
<thead>
<tr>
<th>Year</th>
<th>Temperature (low)</th>
<th>Temperature (high)</th>
</tr>
</thead>
<tbody>
<tr>
<td>1900</td>
<td>-10</td>
<td>25</td>
</tr>
<tr>
<td>1910</td>
<td>-15</td>
<td>30</td>
</tr>
<tr>
<td>1920</td>
<td>-10</td>
<td>32</td>
</tr>
</tbody>
</table>

<p class="col-xs-3 custom-paragraph-class">For example:</p>
Expand Down
2 changes: 1 addition & 1 deletion test/tm-cases/html_classes.text
Expand Up @@ -3,7 +3,7 @@
| `Cell 1` | [Cell 2](http://example.com) link |
| Cell 3 | **Cell 4** |

|| *Year* || *Temperature (low)* || *Temperature (high)* ||
||~ Year ||~ Temperature (low) ||~ Temperature (high) ||
|| 1900 || -10 || 25 ||
|| 1910 || -15 || 30 ||
|| 1920 || -10 || 32 ||
Expand Down
130 changes: 114 additions & 16 deletions test/tm-cases/wiki_tables.html
@@ -1,28 +1,123 @@
<table>
<tbody>
<tr><td><em>Year</em></td><td><em>Temperature (low)</em></td><td><em>Temperature (high)</em></td></tr>
<tr><td>1900</td><td>-10</td><td>25</td></tr>
<tr><td>1910</td><td>-15</td><td>30</td></tr>
<tr><td>1920</td><td>-10</td><td>32</td></tr>
</tbody>
<tbody>
<tr>
<td><em>Year</em></td>
<td><em>Temperature (low)</em></td>
<td><em>Temperature (high)</em></td>
</tr>
<tr>
<td>1900</td>
<td>-10</td>
<td>25</td>
</tr>
<tr>
<td>1910</td>
<td>-15</td>
<td>30</td>
</tr>
<tr>
<td>1920</td>
<td>-10</td>
<td>32</td>
</tr>
</tbody>
</table>

<h1>With header row</h1>

<table>
<thead>
<tr>
<th>Name</th>
<th>Class</th>
<th>Race</th>
<th>Level</th>
</tr>
</thead>
<tbody>
<tr>
<td>Vlad</td>
<td>Barbarian</td>
<td>Dragonborn</td>
<td>12</td>
</tr>
<tr>
<td>Jimbo</td>
<td>Rogue</td>
<td>Halfling</td>
<td>13</td>
</tr>
</tbody>
</table>

<h1>With only header row</h1>

<table>
<thead>
<tr>
<th>Name</th>
<th>Class</th>
<th>Race</th>
<th>Level</th>
</tr>
</thead>
</table>

<h1>With header row, alternate spacing</h1>

<table>
<thead>
<tr>
<th>Name</th>
<th>Class</th>
<th>Race</th>
<th>Level</th>
</tr>
</thead>
<tbody>
<tr>
<td>Vlad</td>
<td>Barbarian</td>
<td>Dragonborn</td>
<td>12</td>
</tr>
<tr>
<td>Jimbo</td>
<td>Rogue</td>
<td>Halfling</td>
<td>13</td>
</tr>
</tbody>
</table>

<h1>just one line</h1>

<table>
<tbody>
<tr><td>foo</td><td>bar</td><td>baz</td></tr>
</tbody>
<tbody>
<tr>
<td>foo</td>
<td>bar</td>
<td>baz</td>
</tr>
</tbody>
</table>

<h1>blockquote</h1>

<blockquote>
<table>
<tbody>
<tr><td>grinch</td><td>stole</td><td>xmas</td></tr>
<tr><td>green</td><td><strong>eggs</strong></td><td>ham</td></tr>
</tbody>
<tbody>
<tr>
<td>grinch</td>
<td>stole</td>
<td>xmas</td>
</tr>
<tr>
<td>green</td>
<td><strong>eggs</strong></td>
<td>ham</td>
</tr>
</tbody>
</table>

<p>-- Dr. Seuss</p>
Expand All @@ -31,7 +126,10 @@ <h1>blockquote</h1>
<h1>end of file</h1>

<table>
<tbody>
<tr><td>ResourceNotFound</td><td>If :login does not exist</td></tr>
</tbody>
<tbody>
<tr>
<td>ResourceNotFound</td>
<td>If :login does not exist</td>
</tr>
</tbody>
</table>
2 changes: 1 addition & 1 deletion test/tm-cases/wiki_tables.tags
@@ -1 +1 @@
extra wiki-tables issue66
extra wiki-tables
15 changes: 15 additions & 0 deletions test/tm-cases/wiki_tables.text
Expand Up @@ -3,6 +3,21 @@
|| 1910 || -15 || 30 ||
|| 1920 || -10 || 32 ||

# With header row

||~ Name ||~ Class ||~ Race ||~ Level ||
|| Vlad || Barbarian || Dragonborn || 12 ||
|| Jimbo || Rogue || Halfling || 13 ||

# With only header row

||~ Name ||~ Class ||~ Race ||~ Level ||

# With header row, alternate spacing

|| ~Name || ~Class || ~Race || ~Level ||
|| Vlad || Barbarian || Dragonborn || 12 ||
|| Jimbo || Rogue || Halfling || 13 ||

# just one line

Expand Down