allow tables with headers in first (or any) column

This commit is contained in:
AlexVonB
2021-05-17 12:36:48 +02:00
parent 7dac92e85e
commit e6da15c173
2 changed files with 30 additions and 11 deletions

View File

@@ -281,20 +281,19 @@ class MarkdownConverter(object):
text_data = []
rendered_header = False
for row in rows:
headers = row.find_all('th')
columns = row.find_all('td')
if not rendered_header and len(headers) > 0:
headers = [head.text.strip() for head in headers]
text_data.append('| ' + ' | '.join(headers) + ' |')
text_data.append('| ' + ' | '.join(['---'] * len(headers)) + ' |')
cells = row.find_all(['td', 'th'])
is_headrow = all([cell.name == 'th' for cell in cells])
texts = [cell.text.strip() for cell in cells]
if not rendered_header and is_headrow:
text_data.append('| ' + ' | '.join(texts) + ' |')
text_data.append('| ' + ' | '.join(['---'] * len(cells)) + ' |')
rendered_header = True
elif len(columns) > 0:
elif len(cells) > 0:
if not rendered_header:
text_data.append('| ' + ' | '.join([''] * len(columns)) + ' |')
text_data.append('| ' + ' | '.join(['---'] * len(columns)) + ' |')
text_data.append('| ' + ' | '.join([''] * len(cells)) + ' |')
text_data.append('| ' + ' | '.join(['---'] * len(cells)) + ' |')
rendered_header = True
columns = [colm.text.strip() for colm in columns]
text_data.append('| ' + ' | '.join(columns) + ' |')
text_data.append('| ' + ' | '.join(texts) + ' |')
else:
continue
return '\n'.join(text_data)