2014-09-15 22:05:36 -05:00
|
|
|
"""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
|
|
|
|
|
2016-07-03 01:30:20 -05:00
|
|
|
# https://github.com/FND/markdown-checklist
|
|
|
|
from markdown_checklist.extension import ChecklistExtension
|
|
|
|
|
2014-09-15 22:05:36 -05:00
|
|
|
# Markdown extensions
|
2016-07-03 01:30:20 -05:00
|
|
|
MD_EXTENSIONS = ['toc', 'meta', 'extra', 'footnotes', 'admonition',
|
|
|
|
'tables', 'attr_list', ChecklistExtension()]
|
2014-09-15 22:05:36 -05:00
|
|
|
|
|
|
|
# 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
|
|
|
|
|
2014-09-15 22:05:36 -05:00
|
|
|
# Get article title
|
2016-07-03 01:30:20 -05:00
|
|
|
if 'title' not in cmd_opts:
|
2014-09-17 08:14:03 -05:00
|
|
|
cmd_opts['title'] = commands.getoutput("""grep 'Title:' %s |awk -F'Title: ' '{print $2}'""" % filename)
|
2014-09-20 23:35:18 -05:00
|
|
|
cmd_opts['title'] = cmd_opts['title'].strip()
|
2014-09-15 22:05:36 -05:00
|
|
|
|
2014-09-26 10:56:15 -05:00
|
|
|
# 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']
|
|
|
|
|
2014-09-15 22:05:36 -05:00
|
|
|
# Set HTML head
|
2016-05-19 06:51:20 -05:00
|
|
|
html = """<!DOCTYPE html>
|
2014-09-15 22:05:36 -05:00
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
|
|
|
<title>%(title)s</title>
|
2016-04-21 23:33:25 -05:00
|
|
|
<link rel="stylesheet" type="text/css" href="./css/markdown.css" />
|
2014-09-15 22:05:36 -05:00
|
|
|
</head>
|
|
|
|
<body>
|
2019-07-13 06:21:55 -05:00
|
|
|
""" % cmd_opts
|
2014-09-15 22:05:36 -05:00
|
|
|
|
2014-09-20 09:32:14 -05:00
|
|
|
# Add navigation items.
|
|
|
|
# Link to iRedMail.org
|
|
|
|
html += """
|
|
|
|
<div id="navigation">
|
2017-11-16 21:48:44 -06:00
|
|
|
<a href="https://www.iredmail.org" target="_blank">
|
2016-04-19 12:48:51 -05:00
|
|
|
<img alt="iRedMail web site"
|
2016-04-21 23:33:25 -05:00
|
|
|
src="./images/logo-iredmail.png"
|
2016-04-19 12:48:51 -05:00
|
|
|
style="vertical-align: middle; height: 30px;"
|
|
|
|
/>
|
|
|
|
<span>iRedMail</span>
|
|
|
|
</a>
|
|
|
|
""" % cmd_opts
|
2014-09-20 09:32:14 -05:00
|
|
|
|
|
|
|
# Add link to index page in article pages.
|
|
|
|
if 'add_index_link' in cmd_opts:
|
2016-02-29 02:15:19 -06:00
|
|
|
html += """ // <a href="./index.html">Document Index</a>"""
|
2014-09-20 09:32:14 -05:00
|
|
|
|
|
|
|
html += """</div>"""
|
|
|
|
|
2015-08-13 10:08:13 -05:00
|
|
|
# Convert to unicode first.
|
|
|
|
html = web.safeunicode(html)
|
|
|
|
|
2014-09-15 22:05:36 -05:00
|
|
|
# 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)
|
|
|
|
|
2016-05-19 06:51:20 -05:00
|
|
|
html += """<div class="footer">
|
2019-12-31 00:07:48 -06:00
|
|
|
<p style="text-align: center; color: grey;">All documents are available in <a href="https://github.com/iredmail/docs/">GitHub repository</a>, and published under <a href="http://creativecommons.org/licenses/by-nd/3.0/us/" target="_blank">Creative Commons</a> license. You can <a href="https://github.com/iredmail/docs/archive/master.zip">download the latest version</a> for offline reading. If you found something wrong, please do <a href="https://www.iredmail.org/contact.html">contact us</a> to fix it.</p>
|
2016-05-19 06:51:20 -05:00
|
|
|
</div>"""
|
2014-10-18 22:49:55 -05:00
|
|
|
|
2014-10-13 19:28:43 -05:00
|
|
|
|
2016-05-19 06:51:20 -05:00
|
|
|
html += """
|
2017-11-05 02:33:58 -06:00
|
|
|
<!-- Global site tag (gtag.js) - Google Analytics -->
|
|
|
|
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-3293801-21"></script>
|
|
|
|
<script>
|
|
|
|
window.dataLayer = window.dataLayer || [];
|
|
|
|
function gtag(){dataLayer.push(arguments);}
|
|
|
|
gtag('js', new Date());
|
|
|
|
|
|
|
|
gtag('config', 'UA-3293801-21');
|
2014-09-15 22:05:36 -05:00
|
|
|
</script>
|
|
|
|
"""
|
|
|
|
|
|
|
|
html += '</body></html>'
|
|
|
|
|
|
|
|
# Write to file
|
|
|
|
f = open(output_html_file, 'w')
|
2015-07-12 01:13:08 -05:00
|
|
|
f.write(html.encode('utf-8'))
|
2014-09-15 22:05:36 -05:00
|
|
|
f.close()
|