guard table lines with pipes, resolves the empty header problem

This commit is contained in:
AlexVonB
2021-04-22 12:36:11 +02:00
parent de6f91af0e
commit e1dbbfad42
2 changed files with 7 additions and 8 deletions

View File

@@ -245,12 +245,11 @@ class MarkdownConverter(object):
columns = row.find_all('td')
if len(headers) > 0:
headers = [head.text.strip() for head in headers]
headers = [head for head in headers if head]
text_data.append(' | '.join(headers))
text_data.append(' | '.join(['---'] * len(headers)))
text_data.append('| ' + ' | '.join(headers) + ' |')
text_data.append('| ' + ' | '.join(['---'] * len(headers)) + ' |')
elif len(columns) > 0:
columns = [colm.text.strip() for colm in columns]
text_data.append(' | '.join(columns))
text_data.append('| ' + ' | '.join(columns) + ' |')
else:
continue
return '\n'.join(text_data)

View File

@@ -71,7 +71,7 @@ table_missing_text = re.sub(r'\s+', '', """
<table>
<thead>
<tr>
<th>Firstname</th>
<th></th>
<th>Lastname</th>
<th>Age</th>
</tr>
@@ -289,6 +289,6 @@ def test_div():
def test_table():
assert md(table) == 'Firstname | Lastname | Age\n--- | --- | ---\nJill | Smith | 50\nEve | Jackson | 94'
assert md(table_head_body) == 'Firstname | Lastname | Age\n--- | --- | ---\nJill | Smith | 50\nEve | Jackson | 94'
assert md(table_missing_text) == 'Firstname | Lastname | Age\n--- | --- | ---\nJill | | 50\nEve | Jackson | 94'
assert md(table) == '| Firstname | Lastname | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |'
assert md(table_head_body) == '| Firstname | Lastname | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |'
assert md(table_missing_text) == '| | Lastname | Age |\n| --- | --- | --- |\n| Jill | | 50 |\n| Eve | Jackson | 94 |'