Merge pull request #36 from BrunoMiguens/add-basic-support-for-tables
Add basic support for tables
This commit is contained in:
@@ -252,6 +252,23 @@ class MarkdownConverter(object):
|
|||||||
|
|
||||||
return '' % (alt, src, title_part)
|
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]
|
||||||
|
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) + ' |')
|
||||||
|
else:
|
||||||
|
continue
|
||||||
|
return '\n'.join(text_data)
|
||||||
|
|
||||||
|
|
||||||
def markdownify(html, **options):
|
def markdownify(html, **options):
|
||||||
return MarkdownConverter(**options).convert(html)
|
return MarkdownConverter(**options).convert(html)
|
||||||
|
|||||||
@@ -22,6 +22,76 @@ nested_uls = re.sub(r'\s+', '', """
|
|||||||
</ul>""")
|
</ul>""")
|
||||||
|
|
||||||
|
|
||||||
|
table = re.sub(r'\s+', '', """
|
||||||
|
<table>
|
||||||
|
<tr>
|
||||||
|
<th>Firstname</th>
|
||||||
|
<th>Lastname</th>
|
||||||
|
<th>Age</th>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>Jill</td>
|
||||||
|
<td>Smith</td>
|
||||||
|
<td>50</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>Eve</td>
|
||||||
|
<td>Jackson</td>
|
||||||
|
<td>94</td>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
""")
|
||||||
|
|
||||||
|
|
||||||
|
table_head_body = re.sub(r'\s+', '', """
|
||||||
|
<table>
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>Firstname</th>
|
||||||
|
<th>Lastname</th>
|
||||||
|
<th>Age</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
<tr>
|
||||||
|
<td>Jill</td>
|
||||||
|
<td>Smith</td>
|
||||||
|
<td>50</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>Eve</td>
|
||||||
|
<td>Jackson</td>
|
||||||
|
<td>94</td>
|
||||||
|
</tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
""")
|
||||||
|
|
||||||
|
table_missing_text = re.sub(r'\s+', '', """
|
||||||
|
<table>
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th></th>
|
||||||
|
<th>Lastname</th>
|
||||||
|
<th>Age</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
<tr>
|
||||||
|
<td>Jill</td>
|
||||||
|
<td></td>
|
||||||
|
<td>50</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>Eve</td>
|
||||||
|
<td>Jackson</td>
|
||||||
|
<td>94</td>
|
||||||
|
</tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
""")
|
||||||
|
|
||||||
|
|
||||||
def test_chomp():
|
def test_chomp():
|
||||||
assert md(' <b></b> ') == ' '
|
assert md(' <b></b> ') == ' '
|
||||||
assert md(' <b> </b> ') == ' '
|
assert md(' <b> </b> ') == ' '
|
||||||
@@ -222,6 +292,12 @@ def test_div():
|
|||||||
assert md('Hello</div> World') == 'Hello World'
|
assert md('Hello</div> World') == 'Hello World'
|
||||||
|
|
||||||
|
|
||||||
|
def test_table():
|
||||||
|
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 |'
|
||||||
|
|
||||||
|
|
||||||
def test_strong_em_symbol():
|
def test_strong_em_symbol():
|
||||||
assert md('<strong>Hello</strong>', strong_em_symbol=UNDERSCORE) == '__Hello__'
|
assert md('<strong>Hello</strong>', strong_em_symbol=UNDERSCORE) == '__Hello__'
|
||||||
assert md('<b>Hello</b>', strong_em_symbol=UNDERSCORE) == '__Hello__'
|
assert md('<b>Hello</b>', strong_em_symbol=UNDERSCORE) == '__Hello__'
|
||||||
|
|||||||
Reference in New Issue
Block a user