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

fix: make tables without body gfm compatible #616

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
26 changes: 14 additions & 12 deletions lib/markdown2.py
Original file line number Diff line number Diff line change
Expand Up @@ -3379,7 +3379,7 @@ def run(self, text):
(?:
^[ ]{0,%d}(?!\ ) # ensure line begins with 0 to less_than_tab spaces
.*\|.*[ ]*\n
)+
)*
)
''' % (less_than_tab, less_than_tab, less_than_tab), re.M | re.X)
return table_re.sub(self.sub, text)
Expand Down Expand Up @@ -3415,17 +3415,19 @@ def sub(self, match: re.Match) -> str:
hlines.append('</thead>')

# tbody
hlines.append('<tbody>')
for line in body.strip('\n').split('\n'):
hlines.append('<tr>')
cols = [re.sub(escape_bar_re, '|', cell.strip()) for cell in re.split(split_bar_re, re.sub(trim_bar_re, "", re.sub(trim_space_re, "", line)))]
for col_idx, col in enumerate(cols):
hlines.append(' <td{}>{}</td>'.format(
align_from_col_idx.get(col_idx, ''),
self.md._run_span_gamut(col)
))
hlines.append('</tr>')
hlines.append('</tbody>')
body = body.strip('\n')
if body:
hlines.append('<tbody>')
for line in body.split('\n'):
hlines.append('<tr>')
cols = [re.sub(escape_bar_re, '|', cell.strip()) for cell in re.split(split_bar_re, re.sub(trim_bar_re, "", re.sub(trim_space_re, "", line)))]
for col_idx, col in enumerate(cols):
hlines.append(' <td{}>{}</td>'.format(
align_from_col_idx.get(col_idx, ''),
self.md._run_span_gamut(col)
))
hlines.append('</tr>')
hlines.append('</tbody>')
hlines.append('</table>')

return '\n'.join(hlines) + '\n'
Expand Down
11 changes: 11 additions & 0 deletions test/tm-cases/tables.html
Original file line number Diff line number Diff line change
Expand Up @@ -394,3 +394,14 @@ <h1>escaping of pipes</h1>
</tr>
</tbody>
</table>

<h1>table without rows</h1>

<table>
<thead>
<tr>
<th>abc</th>
<th>def</th>
</tr>
</thead>
</table>
5 changes: 5 additions & 0 deletions test/tm-cases/tables.text
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,8 @@ the rule is it must have at least a single dash in there.
| A | \| | C \| C |
|--- |--- | ------ |
|\|\|| BB | C |

# table without rows

| abc | def |
| --- | --- |
Loading