md_test.py
import markdown
md = markdown.Markdown()
sample_makedown = '''
An h1 header
============
Paragraphs are separated by a blank line.
2nd paragraph. *Italic*, **bold**, `monospace`. Itemized lists
look like:
* this one
* that one
* the other one
'''
# makedown -> html
print md.convert(sample_makedown)
The print result is as follows.
Output result
<h1>An h1 header</h1>
<p>Paragraphs are separated by a blank line.</p>
<p>2nd paragraph. <em>Italic</em>, <strong>bold</strong>, <code>monospace</code>. Itemized lists
look like:</p>
<ul>
<li>this one</li>
<li>that one</li>
<li>the other one</li>
</ul>
Just convert it like this It converts Markdown text to HTML. It's very easy to use. It also supports writing to files.
When using it with Django, I will make the following filter by myself
myfilter.py
import markdown
#Declare and initialize here for caching
md = markdown.Markdown()
@register.filter
@stringfilter
def mark2html(value):
return md.convert(value)
template.html
{% load myfilter %}
{{ markdown_text|mark2html }}
Recommended Posts