The Basics
To use markdown as a module:
import markdown
html = markdown.markdown(your_text_string)
Encoded Text
Note that markdown() expects either a simple ascii string or unicode as input and returns output as unicode. Do not pass encoded strings to it. If your input is encoded, e.g. as UTF8, it is your responsibility to decode it. E.g.:
input_file = codecs.open("test.txt", mode="r", encoding="utf16")
text = input_file.read()
html = markdown.markdown(text, extensions)
If you later want to write it to disk, you should encode it:
output_file = codecs.open("test.html", "w", encoding="utf8")
output_file.write(html_unicode)
More Options
If you want to pass more options, you can create an instance of Markdown yourself and then use convert() to generate HTML:
import markdown
md = Markdown(text,
extensions=['footnotes'],
extension_configs= {'footnotes' :
('PLACE_MARKER','~~~~~~~~')}
encoding='utf8',
safe_mode = True)
return md.convert()
You should also use this method if you want to process multiple strings:
md = Markdown(text=None)
html1 = md.convert(text1)
html2 = md.convert(text2)
Using Extensions
One of the parameters that you can pass is a list of extensions. Extensions must be available as python modules with names starting with mdx_, followed by the name of the extension. extensions=['footnotes'] will thus look for a module named mdx_footnotes. See the documentation specific to the extension you are using for help in specifying configuration settings for that extension.
Note that some extensions may need their state reset between each call to convert:
html1 = md.convert(text1)
md.reset()
html2 = md.convert(text2)
Safe Mode
If you are using Markdown on a web system which will transform text provided by untrusted users, you may want to use the "safe_mode" option which ensures that user's HTML tags are either replaced, removed or escaped. (They can still create links using Markdown syntax.)
To replace HTML, set safe_mode='replace' (safe_mode=True still works for backward compatibility with older versions). The HTML will be replaced with the text defined in markdown.HTML_REMOVED_TEXT which defaults to "[HTML_REMOVED]". To replace the HTML with something else:
markdown.HTML_REMOVED_TEXT = "--RAW HTML IS NOT ALLOWED--"
md = markdown.Markdown(safe_mode='replace')
Note: You may edit the value of HTML_REMOVED_TEXT directly in markdown.py but you will need to remember to do so every time you upgrade to a newer version of Markdown.
To remove HTML, set safe_mode='remove'. Any raw HTML will be completely stripped from the text with no warning to the author.
To escape HTML, set safe_mode='escape'. The HTML will be escaped and included in the document.