#!/usr/bin/python
import sys
import os
import argparse
import subprocess
import markdown
# Input is quoted UNIX type file path.
#
# Output is HTML file with support for tables and footnotes.
#
# File is written to the same directory as the source unless specified with the -o flag
#
# -o <file_path> Writes file to specified path. Must include file name
#
# -b Opens the output HTML file in the defualt browser
#
# -css <file_path> Uses a css file from specified path instead of built-in. Must include file name
headbegin = '''<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<title>FastMail Keyboard Shortcuts</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<style>'''
bodybegin = '''<body class="normal">
<div id="wrapper">'''
bodyend = '''</div></body>
</html>'''
style = '''
/* Style from http://johnsardine.com/freebies/dl-html-css/simple-little-tab/ */
table a:link {
color: #666;
font-weight: bold;
text-decoration:none;
}
table a:visited {
color: #999999;
font-weight:bold;
text-decoration:none;
}
table a:active,
table a:hover {
color: #bd5a35;
text-decoration:underline;
}
table {
font-family:Arial, Helvetica, sans-serif;
color:#666;
font-size:12px;
text-shadow: 1px 1px 0px #fff;
background:#eaebec;
margin:20px;
border:#ccc 1px solid;
-moz-border-radius:3px;
-webkit-border-radius:3px;
border-radius:3px;
-moz-box-shadow: 0 1px 2px #d1d1d1;
-webkit-box-shadow: 0 1px 2px #d1d1d1;
box-shadow: 0 1px 2px #d1d1d1;
}
table th {
padding:21px 25px 22px 25px;
border-top:1px solid #fafafa;
border-bottom:1px solid #e0e0e0;
background: #ededed;
background: -webkit-gradient(linear, left top, left bottom, from(#ededed), to(#ebebeb));
background: -moz-linear-gradient(top, #ededed, #ebebeb);
}
table th:first-child {
text-align: left;
padding-left:20px;
}
table tr:first-child th:first-child {
-moz-border-radius-topleft:3px;
-webkit-border-top-left-radius:3px;
border-top-left-radius:3px;
}
table tr:first-child th:last-child {
-moz-border-radius-topright:3px;
-webkit-border-top-right-radius:3px;
border-top-right-radius:3px;
}
table tr {
text-align: center;
padding-left:20px;
}
table td:first-child {
text-align: left;
padding-left:20px;
border-left: 0;
}
table td {
padding:18px;
border-top: 1px solid #ffffff;
border-bottom:1px solid #e0e0e0;
border-left: 1px solid #e0e0e0;
background: #fafafa;
background: -webkit-gradient(linear, left top, left bottom, from(#fbfbfb), to(#fafafa));
background: -moz-linear-gradient(top, #fbfbfb, #fafafa);
}
table tr.even td {
background: #f6f6f6;
background: -webkit-gradient(linear, left top, left bottom, from(#f8f8f8), to(#f6f6f6));
background: -moz-linear-gradient(top, #f8f8f8, #f6f6f6);
}
table tr:last-child td {
border-bottom:0;
}
table tr:last-child td:first-child {
-moz-border-radius-bottomleft:3px;
-webkit-border-bottom-left-radius:3px;
border-bottom-left-radius:3px;
}
table tr:last-child td:last-child {
-moz-border-radius-bottomright:3px;
-webkit-border-bottom-right-radius:3px;
border-bottom-right-radius:3px;
}
table tr:hover td {
background: #f2f2f2;
background: -webkit-gradient(linear, left top, left bottom, from(#f2f2f2), to(#f0f0f0));
background: -moz-linear-gradient(top, #f2f2f2, #f0f0f0);
}
'''
styleEnd = '''</style>
<style>#wrapper { max-width:600px; margin:0 auto }</style></head>'''
parser = argparse.ArgumentParser(description='Generate HTML Tables from MultiMarkdown')
parser.add_argument('source', help='The source file path, including file name')
parser.add_argument('-o','--output', help='Path to store the output file', action='store', required=False)
parser.add_argument('-css','--css', help='Path to a custom CSS file, including file name', metavar='in-file', type=argparse.FileType('rt'),required=False)
parser.add_argument('-b', '--browser', help='View the output file in the default browser after saving.', action='store_true')
args = parser.parse_args()
try:
if args.source:
path, filename = os.path.split(args.source)
inputFile = open(args.source, "r")
inputText = inputFile.read()
inputFile.close()
else:
log("No source file specified")
print "No source file specified"
sys.exit(1)
h = inputText
marktable = markdown.markdown(h, ['footnotes', 'fenced_code', 'tables'])
if (args.css):
css_file = args.css
cssText = css_file.read()
css_file.close()
h = headbegin + cssText + styleEnd + bodybegin + marktable + bodyend
else:
h = headbegin + style + styleEnd + bodybegin + marktable + bodyend
# If an output file is specified, write to it
if args.output:
output_dir = args.output
output_file_path = output_dir+filename.split(os.extsep, 1)[0]+'.html'
else:
output_file_path = path+'/'+filename.split(os.extsep, 1)[0]+'.html'
file = open(output_file_path, 'w')
file.write(h.encode('ascii'))
file.close()
print "\nOutput file created: "+ output_file_path
if (args.browser):
try:
retcode = subprocess.call("open " + output_file, shell=True)
if retcode < 0:
print >>sys.stderr, "Child was terminated by signal", -retcode
else:
print >>sys.stderr, "Child returned", retcode
except OSError, e:
print >>sys.stderr, "Execution failed:", e
except:
print "Unexpected Error: ", sys.exc_info()[0]
raise