Python 3: Markdown to HTML

Convert a Markdown-formatted text file to HTML

nick3499
2 min readDec 27, 2017

markdown.txt contains:

# H1 HeadingFirst line with **bold**.## H2 HeadingSecond line with _italics_.### H3 HeadingThird line with `code` and [link](http://www.example.com).#### H4 HeadingFourth line with **_bold italics_**.##### H5 Heading

Create Markdown Object

md_obj = co.open('markdown.txt', mode='r', encoding='utf-8')

codecs.open() converts markdown.txt to a single string of markdown-formatted data, then returns a codecs.StreamReaderWriter object. mode='r' is default mode which means read data. encoding='utf-8'specifies UTF-8, which is also default. In other words, mode and encoding attributes can be left out, since they are automatic, e.g. file_open = co.open('markdown.md').

<codecs.StreamReaderWriter object at 0x7f91720d7978>. 0x7f91720d7978 represents the object's memory location.

Read Markdown Object

read() the markdown-formatted string stored in the codecs.StreamReaderWriter object which is then assigned to md_str.

md_str = md_obj.read()

The Markdown-formatted string assigned to md_str variable is shown below:

'# H1 Heading\n\nFirst line with **bold**.\n\n## H2 Heading\n\nSecond line with _italics_.\n\n### H3 Heading\n\nThird line with ``code`` and [link](http://www.example.com).\n\n#### H4 Heading\n\nFourth line with **_bold italics_**.\n\n##### H5 Heading\n'

Convert Markdown to HTML

markdown.markdown() converts the Markdown string (shown above) to the HTML string (shown below).

html = md.markdown(md_str)

The HTML conversion assigned to html variable is shown below:

'<h1>H1 Heading</h1>\n<p>First line with <strong>bold</strong>.</p>\n<h2>H2 Heading</h2>\n<p>Second line with <em>italics</em>.</p>\n<h3>H3 Heading</h3>\n<p>Third line with <code>code</code> and <a href="http://www.example.com">link</a>.</p>\n<h4>H4 Heading</h4>\n<p>Fourth line with <strong><em>bold italics</em></strong>.</p>\n<h5>H5 Heading</h5>'

Create HTML Object

codecs.open() returns a codecs.StreamReaderWriter object for the HTML output file, given "w" or write mode. out.html will become the new HTML file added to the working directory.

output = co.open("out.html", "w", encoding="utf-8", errors="xmlcharrefreplace")

<codecs.StreamReaderWriter object at 0x7f9171914ef0>

Output HTML File

output.write(html)

out.html contains:

<h1>H1 Heading</h1>
<p>First line with <strong>bold</strong>.</p>
<h2>H2 Heading</h2>
<p>Second line with <em>italics</em>.</p>
<h3>H3 Heading</h3>
<p>Third line with <code>code</code> and <a href="http://www.example.com">link</a>.</p>
<h4>H4 Heading</h4>
<p>Fourth line with <strong><em>bold italics</em></strong>.</p>
<h5>H5 Heading</h5>

--

--