Markdown转为HTML格式。

http://districtdatalabs.silvrback.com/markup-for-fast-data-science-publication

安装:

pip install bleach
pip install markdown


In [3]:
import bleach
from markdown import markdown

def htmlize(text):
    """
    This helper method renders Markdown then uses Bleach to sanitize it as
    well as converting all links in text to actual anchor tags.
    """
    text = bleach.clean(text, strip=True)    # Clean the text by stripping bad HTML tags
    text = markdown(text)                    # Convert the markdown to HTML
    text = bleach.linkify(text)              # Add links from the text and add nofollow to existing links

    return text

In [4]:
md = """
# My Markdown Document

For more information, search on [Google](http://www.google.com).

_Grocery List:_

1. Apples
2. Bananas
3. Oranges

"""

In [5]:
print(htmlize(md))


<h1>My Markdown Document</h1>
<p>For more information, search on <a href="http://www.google.com" rel="nofollow">Google</a>.</p>
<p><em>Grocery List:</em></p>
<ol>
<li>Apples</li>
<li>Bananas</li>
<li>Oranges</li>
</ol>

In [ ]: