Add basic support for HTML tables
This commit is contained in:
@@ -237,6 +237,24 @@ class MarkdownConverter(object):
|
||||
|
||||
return '' % (alt, src, title_part)
|
||||
|
||||
def convert_table(self, el, text, convert_as_inline):
|
||||
rows = el.find_all('tr')
|
||||
text_data = []
|
||||
for row in rows:
|
||||
headers = row.find_all('th')
|
||||
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)))
|
||||
elif len(columns) > 0:
|
||||
columns = [colm.text.strip() for colm in columns]
|
||||
text_data.append(' | '.join([colm for colm in columns if colm]))
|
||||
else:
|
||||
continue
|
||||
return '\n'.join(text_data)
|
||||
|
||||
|
||||
def markdownify(html, **options):
|
||||
return MarkdownConverter(**options).convert(html)
|
||||
|
||||
Reference in New Issue
Block a user