Skip to content
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
9 changes: 5 additions & 4 deletions markdownify/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,8 @@ def convert_li(self, el, text, parent_tags):
# determine list item bullet character to use
parent = el.parent
if parent is not None and parent.name == 'ol':
if parent.get("start") and str(parent.get("start")).isnumeric():
# isdecimal matches int(); isnumeric/isdigit accept chars int() rejects
if parent.get("start") and str(parent.get("start")).isdecimal():
start = int(parent.get("start"))
else:
start = 1
Expand Down Expand Up @@ -734,13 +735,13 @@ def convert_figcaption(self, el, text, parent_tags):

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

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

Expand All @@ -761,7 +762,7 @@ def convert_tr(self, el, text, parent_tags):
underline = ''
full_colspan = 0
for cell in cells:
if 'colspan' in cell.attrs and cell['colspan'].isdigit():
if 'colspan' in cell.attrs and cell['colspan'].isdecimal():
full_colspan += max(1, min(1000, int(cell['colspan'])))
else:
full_colspan += 1
Expand Down
5 changes: 5 additions & 0 deletions tests/test_lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ def test_ol():
assert md('<ol start="-1"><li>a</li><li>b</li></ol>') == '\n\n1. a\n2. b\n'
assert md('<ol start="foo"><li>a</li><li>b</li></ol>') == '\n\n1. a\n2. b\n'
assert md('<ol start="1.5"><li>a</li><li>b</li></ol>') == '\n\n1. a\n2. b\n'
# Unicode numerics (superscript/fraction/CJK/roman/circled) pass isnumeric() but int() rejects them; start falls back to 1
assert md('<ol start="²"><li>a</li><li>b</li></ol>') == '\n\n1. a\n2. b\n'
assert md('<ol start="½"><li>a</li><li>b</li></ol>') == '\n\n1. a\n2. b\n'
assert md('<ol start="一"><li>a</li><li>b</li></ol>') == '\n\n1. a\n2. b\n'
assert md('<ol start="⑤"><li>a</li><li>b</li></ol>') == '\n\n1. a\n2. b\n'
assert md('<ol start="1234"><li><p>first para</p><p>second para</p></li><li><p>third para</p><p>fourth para</p></li></ol>') == '\n\n1234. first para\n\n second para\n1235. third para\n\n fourth para\n'


Expand Down
13 changes: 13 additions & 0 deletions tests/test_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,18 @@
</tr>
</table>"""

# superscript ² and circled ② pass str.isdigit() but int() rejects them
table_with_unicode_colspan = """<table>
<tr>
<th colspan="²">Name</th>
<th>Age</th>
</tr>
<tr>
<td colspan="②">Jill</td>
<td>Smith</td>
</tr>
</table>"""

table_with_colspan_missing_head = """<table>
<tr>
<td colspan="2">Name</td>
Expand Down Expand Up @@ -300,6 +312,7 @@ def test_table():
assert md(table_with_caption) == 'TEXT\n\nCaption\n\n| | | |\n| --- | --- | --- |\n| Firstname | Lastname | Age |\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'
assert md(table_with_unicode_colspan) == '\n\n| Name | Age |\n| --- | --- |\n| Jill | Smith |\n\n'
assert md(table_with_colspan_missing_head) == '\n\n| | | |\n| --- | --- | --- |\n| Name | | Age |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n'


Expand Down