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>
This commit is contained in:
Eric Xu
2024-03-26 13:50:54 -07:00
committed by GitHub
parent 57d4f37923
commit 3b4a014f25
2 changed files with 33 additions and 3 deletions

View File

@@ -376,10 +376,16 @@ class MarkdownConverter(object):
return '\n\n' + text + '\n\n'
def convert_td(self, el, text, convert_as_inline):
return ' ' + text.strip().replace("\n", " ") + ' |'
colspan = 1
if 'colspan' in el.attrs:
colspan = int(el['colspan'])
return ' ' + text.strip().replace("\n", " ") + ' |' * colspan
def convert_th(self, el, text, convert_as_inline):
return ' ' + text + ' |'
colspan = 1
if 'colspan' in el.attrs:
colspan = int(el['colspan'])
return ' ' + text.strip().replace("\n", " ") + ' |' * colspan
def convert_tr(self, el, text, convert_as_inline):
cells = el.find_all(['td', 'th'])
@@ -392,7 +398,13 @@ class MarkdownConverter(object):
underline = ''
if is_headrow and not el.previous_sibling:
# first row and is headline: print headline underline
underline += '| ' + ' | '.join(['---'] * len(cells)) + ' |' + '\n'
full_colspan = 0
for cell in cells:
if "colspan" in cell.attrs:
full_colspan += int(cell["colspan"])
else:
full_colspan += 1
underline += '| ' + ' | '.join(['---'] * full_colspan) + ' |' + '\n'
elif (not el.previous_sibling
and (el.parent.name == 'table'
or (el.parent.name == 'tbody'