"""OKFText markdown module.
Provides basic markdown integrated with various extensions as well as basic
templating so that a document can be converted to a full html file.
This module requires the python markdown script provided at:
Currently designed to be compatible with markdown 1.6+ (1.7 especially).
Called mkdn so as to not cause import conflicts with markdown itself.
"""
import re
import markdown
def convert(infile):
"""Convert a markdown file to html.
"""
indata = infile.read()
if not isinstance(indata, unicode):
indata = indata.decode('utf8')
indata = indata.replace(r'\\', r'\\\\')
indata = re.sub(r'\\([^\\`*_#+-.!])', r'\\\\\1', indata)
md = markdown.Markdown(None,
extensions=['footnotes'],
)
html = md.convert(indata)
html = re.sub(r'([ \w])--([ \w])', r'\1–\2', html)
html = html.encode('utf8')
return html
html_template = \
'''
%(title)s
%(body)s
'''
def template(infile, **kwargs):
"""Convert a markdown file to html file (complete with header etc.
"""
out = convert(infile)
kwargs['body'] = out
out = html_template % kwargs
return out
def convert_for_1_dot_5(infile):
"""Convert a markdown file to html
"""
md = markdown.Markdown()
footnoteExtension.extendMarkdown(md)
indata = infile.read()
indata = indata.replace(r'\\', r'\\\\')
indata = re.sub(r'\\([^\\`*_#+-.!])', r'\\\\\1', indata)
md.source = indata
out = str(md)
out = re.sub(r'([ \w])--([ \w])', r'\1–\2', out)
return out