diff --git a/markdownify/__init__.py b/markdownify/__init__.py index 88e158e..0b2a620 100644 --- a/markdownify/__init__.py +++ b/markdownify/__init__.py @@ -252,6 +252,23 @@ class MarkdownConverter(object): return '![%s](%s%s)' % (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): return MarkdownConverter(**options).convert(html) diff --git a/setup.py b/setup.py index db71182..61d07ec 100644 --- a/setup.py +++ b/setup.py @@ -10,7 +10,7 @@ read = lambda filepath: codecs.open(filepath, 'r', 'utf-8').read() pkgmeta = { '__title__': 'markdownify', '__author__': 'Matthew Tretter', - '__version__': '0.6.6', + '__version__': '0.7.0', } diff --git a/tests/test_conversions.py b/tests/test_conversions.py index 68b44c6..6dcf9a6 100644 --- a/tests/test_conversions.py +++ b/tests/test_conversions.py @@ -22,6 +22,76 @@ nested_uls = re.sub(r'\s+', '', """ """) +table = re.sub(r'\s+', '', """ + + + + + + + + + + + + + + + + +
FirstnameLastnameAge
JillSmith50
EveJackson94
+""") + + +table_head_body = re.sub(r'\s+', '', """ + + + + + + + + + + + + + + + + + + + + +
FirstnameLastnameAge
JillSmith50
EveJackson94
+""") + +table_missing_text = re.sub(r'\s+', '', """ + + + + + + + + + + + + + + + + + + + + +
LastnameAge
Jill50
EveJackson94
+""") + + def test_chomp(): assert md(' ') == ' ' assert md(' ') == ' ' @@ -222,6 +292,12 @@ def test_div(): assert md('Hello 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(): assert md('Hello', strong_em_symbol=UNDERSCORE) == '__Hello__' assert md('Hello', strong_em_symbol=UNDERSCORE) == '__Hello__'