mdninja + markdown + jinja2 = beautiful HTML

Published: 2017-11-15 by Pradeep Gowda.

I love pandoc for writing documents. However, sometime you want a lighter solution if (for whatever reason) you cannot use Pandoc.

Python has a fairly decent markdown -> HTML converter in python-markdown. But the output of markdown converter tends to be barebones with no CSS styling and lacks ways to customise the final HTML layout. Jinja2 is a very powerful and friendly templating engine for Python.

mdninja is a nice Command Line Utility that allows you to produce neat looking HTML documents by combining these two nice libraries.

Look at the github page for mdninja for details. The package is also available on Python Package Index.

This requires you to have Python3.4+. I think it’s high time for folks to move off Python2.7 for good. I did not take the time to test with/target python2. I’ll welcome PRs to that effect if you want to use it with Python 2.


pip install mdninja


Simple use:

mdninja -o doc.html

If you want to use a different template:

mdninja -o doc.html --template=stylish.html

The default template is:

<!DOCTYPE html>
                <title>{% for title in meta.title %}{{title}} {% endfor %}</title>
                <h1>{% for title in meta.title %}{{title}} {% endfor %}</h1>
                {{ body }}

Metadata (like title above) is added to the document by adding metadata headers like this at the top of the file

Title: A simple document

Alternatively, you can specify the meatadata using a YAML style header too:

title: A simple document

See also: my pandoc page, markdown.