Skip to content

Commit ea5b228

Browse files
committed
Merge branch 'develop'
2 parents ec5858e + 9f3c4c9 commit ea5b228

File tree

4 files changed

+112
-45
lines changed

4 files changed

+112
-45
lines changed

markdownify/__init__.py

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -84,18 +84,26 @@ def process_tag(self, node, convert_as_inline, children_only=False):
8484
if not children_only and isHeading:
8585
convert_children_as_inline = True
8686

87-
# Remove whitespace-only textnodes in lists
88-
def is_list_node(el):
89-
return el and el.name in ['ol', 'ul', 'li']
87+
# Remove whitespace-only textnodes in purely nested nodes
88+
def is_nested_node(el):
89+
return el and el.name in ['ol', 'ul', 'li',
90+
'table', 'thead', 'tbody', 'tfoot',
91+
'tr', 'td', 'th']
9092

91-
if is_list_node(node):
93+
if is_nested_node(node):
9294
for el in node.children:
93-
# Only extract (remove) whitespace-only text node if any of the conditions is true:
95+
# Only extract (remove) whitespace-only text node if any of the
96+
# conditions is true:
9497
# - el is the first element in its parent
9598
# - el is the last element in its parent
96-
# - el is adjacent to an list node
97-
can_extract = not el.previous_sibling or not el.next_sibling or is_list_node(el.previous_sibling) or is_list_node(el.next_sibling)
98-
if isinstance(el, NavigableString) and six.text_type(el).strip() == '' and can_extract:
99+
# - el is adjacent to an nested node
100+
can_extract = (not el.previous_sibling
101+
or not el.next_sibling
102+
or is_nested_node(el.previous_sibling)
103+
or is_nested_node(el.next_sibling))
104+
if (isinstance(el, NavigableString)
105+
and six.text_type(el).strip() == ''
106+
and can_extract):
99107
el.extract()
100108

101109
# Convert the children first
@@ -277,21 +285,28 @@ def convert_img(self, el, text, convert_as_inline):
277285
return '![%s](%s%s)' % (alt, src, title_part)
278286

279287
def convert_table(self, el, text, convert_as_inline):
280-
rows = el.find_all('tr')
281-
text_data = []
282-
for row in rows:
283-
headers = row.find_all('th')
284-
columns = row.find_all('td')
285-
if len(headers) > 0:
286-
headers = [head.text.strip() for head in headers]
287-
text_data.append('| ' + ' | '.join(headers) + ' |')
288-
text_data.append('| ' + ' | '.join(['---'] * len(headers)) + ' |')
289-
elif len(columns) > 0:
290-
columns = [colm.text.strip() for colm in columns]
291-
text_data.append('| ' + ' | '.join(columns) + ' |')
292-
else:
293-
continue
294-
return '\n'.join(text_data)
288+
return '\n\n' + text + '\n'
289+
290+
def convert_tr(self, el, text, convert_as_inline):
291+
cells = el.find_all(['td', 'th'])
292+
is_headrow = all([cell.name == 'th' for cell in cells])
293+
overline = ''
294+
underline = ''
295+
if is_headrow and not el.previous_sibling:
296+
# first row and is headline: print headline underline
297+
underline += '| ' + ' | '.join(['---'] * len(cells)) + ' |' + '\n'
298+
elif not el.previous_sibling and not el.parent.name != 'table':
299+
# first row, not headline, and the parent is sth. like tbody:
300+
# print empty headline above this row
301+
overline += '| ' + ' | '.join([''] * len(cells)) + ' |' + '\n'
302+
overline += '| ' + ' | '.join(['---'] * len(cells)) + ' |' + '\n'
303+
return overline + '|' + text + '\n' + underline
304+
305+
def convert_th(self, el, text, convert_as_inline):
306+
return ' ' + text + ' |'
307+
308+
def convert_td(self, el, text, convert_as_inline):
309+
return ' ' + text + ' |'
295310

296311
def convert_hr(self, el, text, convert_as_inline):
297312
return '\n\n---\n\n'

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
[flake8]
2-
ignore = E501
2+
ignore = E501 W503

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
pkgmeta = {
1111
'__title__': 'markdownify',
1212
'__author__': 'Matthew Tretter',
13-
'__version__': '0.7.3',
13+
'__version__': '0.7.4',
1414
}
1515

1616

tests/test_conversions.py

Lines changed: 72 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from markdownify import markdownify as md, ATX, ATX_CLOSED, BACKSLASH, UNDERSCORE
2-
import re
32

43

54
nested_uls = """
@@ -41,8 +40,7 @@
4140
</ul>"""
4241

4342

44-
table = re.sub(r'\s+', '', """
45-
<table>
43+
table = """<table>
4644
<tr>
4745
<th>Firstname</th>
4846
<th>Lastname</th>
@@ -58,18 +56,54 @@
5856
<td>Jackson</td>
5957
<td>94</td>
6058
</tr>
61-
</table>
62-
""")
59+
</table>"""
6360

6461

65-
table_head_body = re.sub(r'\s+', '', """
66-
<table>
62+
table_with_html_content = """<table>
63+
<tr>
64+
<th>Firstname</th>
65+
<th>Lastname</th>
66+
<th>Age</th>
67+
</tr>
68+
<tr>
69+
<td><b>Jill</b></td>
70+
<td><i>Smith</i></td>
71+
<td><a href="#">50</a></td>
72+
</tr>
73+
<tr>
74+
<td>Eve</td>
75+
<td>Jackson</td>
76+
<td>94</td>
77+
</tr>
78+
</table>"""
79+
80+
81+
table_with_header_column = """<table>
82+
<tr>
83+
<th>Firstname</th>
84+
<th>Lastname</th>
85+
<th>Age</th>
86+
</tr>
87+
<tr>
88+
<th>Jill</th>
89+
<td>Smith</td>
90+
<td>50</td>
91+
</tr>
92+
<tr>
93+
<th>Eve</th>
94+
<td>Jackson</td>
95+
<td>94</td>
96+
</tr>
97+
</table>"""
98+
99+
100+
table_head_body = """<table>
67101
<thead>
68-
<tr>
102+
<tr>
69103
<th>Firstname</th>
70104
<th>Lastname</th>
71105
<th>Age</th>
72-
</tr>
106+
</tr>
73107
</thead>
74108
<tbody>
75109
<tr>
@@ -83,17 +117,15 @@
83117
<td>94</td>
84118
</tr>
85119
</tbody>
86-
</table>
87-
""")
120+
</table>"""
88121

89-
table_missing_text = re.sub(r'\s+', '', """
90-
<table>
122+
table_missing_text = """<table>
91123
<thead>
92-
<tr>
124+
<tr>
93125
<th></th>
94126
<th>Lastname</th>
95127
<th>Age</th>
96-
</tr>
128+
</tr>
97129
</thead>
98130
<tbody>
99131
<tr>
@@ -107,8 +139,25 @@
107139
<td>94</td>
108140
</tr>
109141
</tbody>
110-
</table>
111-
""")
142+
</table>"""
143+
144+
table_missing_head = """<table>
145+
<tr>
146+
<td>Firstname</td>
147+
<td>Lastname</td>
148+
<td>Age</td>
149+
</tr>
150+
<tr>
151+
<td>Jill</td>
152+
<td>Smith</td>
153+
<td>50</td>
154+
</tr>
155+
<tr>
156+
<td>Eve</td>
157+
<td>Jackson</td>
158+
<td>94</td>
159+
</tr>
160+
</table>"""
112161

113162

114163
def test_chomp():
@@ -322,9 +371,12 @@ def test_div():
322371

323372

324373
def test_table():
325-
assert md(table) == '| Firstname | Lastname | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |'
326-
assert md(table_head_body) == '| Firstname | Lastname | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |'
327-
assert md(table_missing_text) == '| | Lastname | Age |\n| --- | --- | --- |\n| Jill | | 50 |\n| Eve | Jackson | 94 |'
374+
assert md(table) == '\n\n| Firstname | Lastname | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n'
375+
assert md(table_with_html_content) == '\n\n| Firstname | Lastname | Age |\n| --- | --- | --- |\n| **Jill** | *Smith* | [50](#) |\n| Eve | Jackson | 94 |\n\n'
376+
assert md(table_with_header_column) == '\n\n| Firstname | Lastname | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n'
377+
assert md(table_head_body) == '\n\n| Firstname | Lastname | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n'
378+
assert md(table_missing_text) == '\n\n| | Lastname | Age |\n| --- | --- | --- |\n| Jill | | 50 |\n| Eve | Jackson | 94 |\n\n'
379+
assert md(table_missing_head) == '\n\n| | | |\n| --- | --- | --- |\n| Firstname | Lastname | Age |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n'
328380

329381

330382
def test_strong_em_symbol():

0 commit comments

Comments
 (0)