Repository: matthewwithanm/python-markdownify

Issue:
Support tables
Currently `table`s are not supported. Would be great to see basic support for

Comments:

Patch:
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)


Test patch:
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+', '', """
     </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():
     assert md(' <b></b> ') == '  '
     assert md(' <b> </b> ') == '  '
@@ -222,6 +292,12 @@ def test_div():
     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():
     assert md('<strong>Hello</strong>', strong_em_symbol=UNDERSCORE) == '__Hello__'
     assert md('<b>Hello</b>', strong_em_symbol=UNDERSCORE) == '__Hello__'