Skip to content

Commit 7908f14

Browse files
authored
Generalize handling of colspan in case where colspan is in first row but header row is missing (#203)
1 parent 618747c commit 7908f14

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

Diff for: markdownify/__init__.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,12 @@ def convert_tr(self, el, text, parent_tags):
701701
)
702702
overline = ''
703703
underline = ''
704+
full_colspan = 0
705+
for cell in cells:
706+
if 'colspan' in cell.attrs and cell['colspan'].isdigit():
707+
full_colspan += int(cell["colspan"])
708+
else:
709+
full_colspan += 1
704710
if ((is_headrow
705711
or (is_head_row_missing
706712
and self.options['table_infer_header']))
@@ -709,12 +715,6 @@ def convert_tr(self, el, text, parent_tags):
709715
# - is headline or
710716
# - headline is missing and header inference is enabled
711717
# print headline underline
712-
full_colspan = 0
713-
for cell in cells:
714-
if 'colspan' in cell.attrs and cell['colspan'].isdigit():
715-
full_colspan += int(cell["colspan"])
716-
else:
717-
full_colspan += 1
718718
underline += '| ' + ' | '.join(['---'] * full_colspan) + ' |' + '\n'
719719
elif ((is_head_row_missing
720720
and not self.options['table_infer_header'])
@@ -727,8 +727,8 @@ def convert_tr(self, el, text, parent_tags):
727727
# - the parent is table or
728728
# - the parent is tbody at the beginning of a table.
729729
# print empty headline above this row
730-
overline += '| ' + ' | '.join([''] * len(cells)) + ' |' + '\n'
731-
overline += '| ' + ' | '.join(['---'] * len(cells)) + ' |' + '\n'
730+
overline += '| ' + ' | '.join([''] * full_colspan) + ' |' + '\n'
731+
overline += '| ' + ' | '.join(['---'] * full_colspan) + ' |' + '\n'
732732
return overline + '|' + text + '\n' + underline
733733

734734

Diff for: tests/test_tables.py

+19
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,23 @@
267267
</tr>
268268
</table>"""
269269

270+
table_with_colspan_missing_head = """<table>
271+
<tr>
272+
<td colspan="2">Name</td>
273+
<td>Age</td>
274+
</tr>
275+
<tr>
276+
<td>Jill</td>
277+
<td>Smith</td>
278+
<td>50</td>
279+
</tr>
280+
<tr>
281+
<td>Eve</td>
282+
<td>Jackson</td>
283+
<td>94</td>
284+
</tr>
285+
</table>"""
286+
270287

271288
def test_table():
272289
assert md(table) == '\n\n| Firstname | Lastname | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n'
@@ -283,6 +300,7 @@ def test_table():
283300
assert md(table_with_caption) == 'TEXT\n\nCaption\n\n| | | |\n| --- | --- | --- |\n| Firstname | Lastname | Age |\n\n'
284301
assert md(table_with_colspan) == '\n\n| Name | | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n'
285302
assert md(table_with_undefined_colspan) == '\n\n| Name | Age |\n| --- | --- |\n| Jill | Smith |\n\n'
303+
assert md(table_with_colspan_missing_head) == '\n\n| | | |\n| --- | --- | --- |\n| Name | | Age |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n'
286304

287305

288306
def test_table_infer_header():
@@ -300,3 +318,4 @@ def test_table_infer_header():
300318
assert md(table_with_caption, table_infer_header=True) == 'TEXT\n\nCaption\n\n| Firstname | Lastname | Age |\n| --- | --- | --- |\n\n'
301319
assert md(table_with_colspan, table_infer_header=True) == '\n\n| Name | | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n'
302320
assert md(table_with_undefined_colspan, table_infer_header=True) == '\n\n| Name | Age |\n| --- | --- |\n| Jill | Smith |\n\n'
321+
assert md(table_with_colspan_missing_head, table_infer_header=True) == '\n\n| Name | | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n'

0 commit comments

Comments
 (0)