Skip to content

Commit 3b4a014

Browse files
xydxydxyd1AlexVonB
andauthored
Table merge cell horizontally (#110)
* Fix #109 Table merge cell horizontally * Add test case for colspan --------- Co-authored-by: AlexVonB <AlexVonB@users.noreply.github.com>
1 parent 57d4f37 commit 3b4a014

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

markdownify/__init__.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -376,10 +376,16 @@ def convert_figcaption(self, el, text, convert_as_inline):
376376
return '\n\n' + text + '\n\n'
377377

378378
def convert_td(self, el, text, convert_as_inline):
379-
return ' ' + text.strip().replace("\n", " ") + ' |'
379+
colspan = 1
380+
if 'colspan' in el.attrs:
381+
colspan = int(el['colspan'])
382+
return ' ' + text.strip().replace("\n", " ") + ' |' * colspan
380383

381384
def convert_th(self, el, text, convert_as_inline):
382-
return ' ' + text + ' |'
385+
colspan = 1
386+
if 'colspan' in el.attrs:
387+
colspan = int(el['colspan'])
388+
return ' ' + text.strip().replace("\n", " ") + ' |' * colspan
383389

384390
def convert_tr(self, el, text, convert_as_inline):
385391
cells = el.find_all(['td', 'th'])
@@ -392,7 +398,13 @@ def convert_tr(self, el, text, convert_as_inline):
392398
underline = ''
393399
if is_headrow and not el.previous_sibling:
394400
# first row and is headline: print headline underline
395-
underline += '| ' + ' | '.join(['---'] * len(cells)) + ' |' + '\n'
401+
full_colspan = 0
402+
for cell in cells:
403+
if "colspan" in cell.attrs:
404+
full_colspan += int(cell["colspan"])
405+
else:
406+
full_colspan += 1
407+
underline += '| ' + ' | '.join(['---'] * full_colspan) + ' |' + '\n'
396408
elif (not el.previous_sibling
397409
and (el.parent.name == 'table'
398410
or (el.parent.name == 'tbody'

tests/test_tables.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,23 @@
209209
</tbody>
210210
</table>"""
211211

212+
table_with_colspan = """<table>
213+
<tr>
214+
<th colspan="2">Name</th>
215+
<th>Age</th>
216+
</tr>
217+
<tr>
218+
<td>Jill</td>
219+
<td>Smith</td>
220+
<td>50</td>
221+
</tr>
222+
<tr>
223+
<td>Eve</td>
224+
<td>Jackson</td>
225+
<td>94</td>
226+
</tr>
227+
</table>"""
228+
212229

213230
def test_table():
214231
assert md(table) == '\n\n| Firstname | Lastname | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n'
@@ -222,3 +239,4 @@ def test_table():
222239
assert md(table_missing_head) == '\n\n| Firstname | Lastname | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n'
223240
assert md(table_body) == '\n\n| Firstname | Lastname | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n'
224241
assert md(table_with_caption) == 'TEXT\n\nCaption\n| Firstname | Lastname | Age |\n| --- | --- | --- |\n\n'
242+
assert md(table_with_colspan) == '\n\n| Name | | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n'

0 commit comments

Comments
 (0)