"""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 MD_EXTENSIONS = ['toc', 'meta', 'extra', 'footnotes', 'admonition', 'tables', 'attr_list'] # 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] # 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 # 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 = """\ %(title)s """ % cmd_opts # Add navigation items. # Link to iRedMail.org html += """ """ # Convert to unicode first. html = web.safeunicode(html) # 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) html += """

All documents are available in BitBucket repository, and published under Creative Commons license. You can download the latest version for offline reading. If you found something wrong, please do contact us to fix it.""" html += """\ """ html += '' # Write to file f = open(output_html_file, 'w') f.write(html.encode('utf-8')) f.close()