iredmail-doc/tools/markdown2html.py

98 lines
2.8 KiB
Python
Raw Normal View History

"""Convert Markdown to HTML file.
Required Markdown module: http://pypi.python.org/pypi/Markdown/2.1.1
"""
# Usage:
# shell> python markdown2html.py path/to/file.md path/to/output/dir
import sys
import commands
import web
import markdown
# Markdown extensions
2014-10-09 22:30:58 -05:00
MD_EXTENSIONS = ['toc', 'meta', 'extra', 'footnotes']
# Get file name
filename = sys.argv[1]
# Get file name without file extension
filename_without_ext = filename.split('/')[-1].replace('.md', '')
# Get output directory
output_dir = sys.argv[2]
2014-09-16 09:54:05 -05:00
# Get other options and convert them to a dict.
args = sys.argv[3:]
cmd_opts = {}
for arg in args:
if '=' in arg:
(var, value) = arg.split('=')
cmd_opts[var] = value
if not 'css' in cmd_opts:
2014-09-22 22:17:52 -05:00
cmd_opts['css'] = './css/markdown.css'
2014-09-16 09:54:05 -05:00
# Get article title
if not 'title' in cmd_opts:
cmd_opts['title'] = commands.getoutput("""grep 'Title:' %s |awk -F'Title: ' '{print $2}'""" % filename)
cmd_opts['title'] = cmd_opts['title'].strip()
# Set output file name
output_html_file = output_dir + '/' + filename_without_ext + '.html'
if 'output_filename' in cmd_opts:
output_html_file = output_dir + '/' + cmd_opts['output_filename']
# Set HTML head
html = """\
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>%(title)s</title>
<link rel="stylesheet" type="text/css" href="%(css)s" />
</head>
<body>
2014-09-16 09:54:05 -05:00
""" % cmd_opts
2014-09-20 09:32:14 -05:00
# Add navigation items.
# Link to iRedMail.org
html += """
<div id="navigation">
<a href="http://www.iredmail.org" target="_blank">iRedMail web site</a>
"""
# Add link to index page in article pages.
if 'add_index_link' in cmd_opts:
html += """
2014-09-22 22:20:46 -05:00
// <a href="./index.html">Document Index</a>
2014-09-20 09:32:14 -05:00
"""
html += """</div>"""
# Read markdown file and render as HTML body
# Handle unicode characters with web.safeunicode
orig_content = web.safeunicode(open(filename).read())
html += markdown.markdown(orig_content, extensions=MD_EXTENSIONS)
2014-12-04 07:30:08 -06:00
html += """<p style="text-align: center; color: grey;">Document published under a <a href="http://creativecommons.org/licenses/by-nd/3.0/us/" target="_blank">CC BY-ND 3.0</a> license. If you found something wrong, please do <a href="http://www.iredmail.org/contact.html">contact us</a> to fix it."""
2014-10-18 22:49:55 -05:00
2014-10-13 19:28:43 -05:00
html += """\
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-3293801-21', 'auto');
ga('send', 'pageview');
</script>
"""
html += '</body></html>'
# Write to file
f = open(output_html_file, 'w')
f.write(html.encode('utf-8'))
f.close()