Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Markdown tables to html tables using Python

I have a markdown table syntax string, say:

table_md=\
"| Tables        | Are           | Cool  |\n\
| ------------- |-------------| -----|\n\
| col 3 is      | right-aligned | $1600 |\n\
| col 2 is      | centered      |   $12 |\n\
| zebra stripes | are neat      |    $1 |\n"

I would like to convert it to html syntax table string:

<table>
<thead>
<tr>
<th>Tables</th>
<th>Are</th>
<th>Cool</th>
</tr>
</thead>
<tbody>
<tr>
<td>col 3 is</td>
<td>right-aligned</td>
<td>$1600</td>
</tr>
<tr>
<td>col 2 is</td>
<td>centered</td>
<td>$12</td>
</tr>
<tr>
<td>zebra stripes</td>
<td>are neat</td>
<td>$1</td>
</tr>
</tbody>
</table>

First searching through stackoverflow, I have tried using this:

import markdown
table_html=markdown.markdown(table_md)

But the result is a html paragraph:

'<p>| Tables...    |</p>'

By gooling the issue, I have come to markdown extensions, and try add the extension to the command above:

table_html=markdown.markdown(table_md, extensions=[MyExtension(), \
'markdown.extensions.tables'])

Then it shows error saying that "NameError: name 'MyExtension' is not defined"

And there is no same situation in stackoverflow.

Please help me what to do with MyExtension above. Thank you!

like image 928
Tin Luu Avatar asked Nov 01 '17 05:11

Tin Luu


People also ask

How do I convert Markdown to HTML?

To convert Markdown to HTML using Typora, click File —> Export —> HTML. Then save the file in your preferred location. The image below shows that the HTML output looks exactly as how the Markdown is displayed inside Typora.

Can you use Markdown in Python?

Python-Markdown is a Python library that allows you to convert Markdown text to HTML in various ways. You can extend its functionality using its different extensions that provide additional features. Note however, that the Python-Markdown has a few minor differences with the standard Markdown syntax.

Can you make a table in Markdown?

To add a table, use three or more hyphens ( --- ) to create each column's header, and use pipes ( | ) to separate each column. For compatibility, you should also add a pipe on either end of the row. Cell widths can vary, as shown below.


2 Answers

firstly you can have your input like below:

table_md="| Tables        | Are           | Cool  |\n\
| ------------- |-------------| -----|\n\
| col 3 is      | right-aligned | $1600 |\n\
| col 2 is      | centered      |   $12 |\n\
| zebra stripes | are neat      |    $1 |\n"

use an extension markdown.extensions.tables

table_html=markdown.markdown(table_md, extensions=['markdown.extensions.tables'])

Output is:

>>> print table_html
<table>
<thead>
<tr>
<th>Tables</th>
<th>Are</th>
<th>Cool</th>
</tr>
</thead>
<tbody>
<tr>
<td>col 3 is</td>
<td>right-aligned</td>
<td>$1600</td>
</tr>
<tr>
<td>col 2 is</td>
<td>centered</td>
<td>$12</td>
</tr>
<tr>
<td>zebra stripes</td>
<td>are neat</td>
<td>$1</td>
</tr>
</tbody>
</table>
like image 59
be_good_do_good Avatar answered Sep 21 '22 23:09

be_good_do_good


I found the solution, the extension library state that "The list of extensions may contain instances of extensions and/or strings of extension names", so MyExtension() is optional, so I can delete it in this case, the solution is:

table_html=markdown.markdown(table_md, extensions=['markdown.extensions.tables'])

For those who want to adding their own additions or changes to the syntax of Markdown, you can use MyExtension as follow:

from markdown.extensions import Extension
class MyExtension(Extension):
    # define your extension here...

markdown.markdown(text, extensions=[MyExtension(option='value')])
like image 23
Tin Luu Avatar answered Sep 20 '22 23:09

Tin Luu