Skip to content

Commit

Permalink
handle un-parsable colspan values
Browse files Browse the repository at this point in the history
fixes #126
  • Loading branch information
AlexVonB committed Jun 23, 2024
1 parent c1672ae commit 2ec3338
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
6 changes: 3 additions & 3 deletions markdownify/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,13 +383,13 @@ def convert_figcaption(self, el, text, convert_as_inline):

def convert_td(self, el, text, convert_as_inline):
colspan = 1
if 'colspan' in el.attrs:
if 'colspan' in el.attrs and el['colspan'].isdigit():
colspan = int(el['colspan'])
return ' ' + text.strip().replace("\n", " ") + ' |' * colspan

def convert_th(self, el, text, convert_as_inline):
colspan = 1
if 'colspan' in el.attrs:
if 'colspan' in el.attrs and el['colspan'].isdigit():
colspan = int(el['colspan'])
return ' ' + text.strip().replace("\n", " ") + ' |' * colspan

Expand All @@ -406,7 +406,7 @@ def convert_tr(self, el, text, convert_as_inline):
# first row and is headline: print headline underline
full_colspan = 0
for cell in cells:
if "colspan" in cell.attrs:
if 'colspan' in cell.attrs and cell['colspan'].isdigit():
full_colspan += int(cell["colspan"])
else:
full_colspan += 1
Expand Down
14 changes: 13 additions & 1 deletion tests/test_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@
<th>Age</th>
</tr>
<tr>
<td>Jill</td>
<td colspan="1">Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
Expand All @@ -226,6 +226,17 @@
</tr>
</table>"""

table_with_undefined_colspan = """<table>
<tr>
<th colspan="undefined">Name</th>
<th>Age</th>
</tr>
<tr>
<td colspan="-1">Jill</td>
<td>Smith</td>
</tr>
</table>"""


def test_table():
assert md(table) == '\n\n| Firstname | Lastname | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n'
Expand All @@ -240,3 +251,4 @@ def test_table():
assert md(table_body) == '\n\n| Firstname | Lastname | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n'
assert md(table_with_caption) == 'TEXT\n\nCaption\n| Firstname | Lastname | Age |\n| --- | --- | --- |\n\n'
assert md(table_with_colspan) == '\n\n| Name | | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n'
assert md(table_with_undefined_colspan) == '\n\n| Name | Age |\n| --- | --- |\n| Jill | Smith |\n\n'

0 comments on commit 2ec3338

Please sign in to comment.