#!/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 Writes file to specified path. Must include file name # # -b Opens the output HTML file in the defualt browser # # -css Uses a css file from specified path instead of built-in. Must include file name headbegin = ''' ''' headTitleClose =''' ''' 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) baseFileName = filename.split(os.extsep, 1)[0] 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 + baseFileName + headTitleClose +cssText + styleEnd + bodybegin + marktable + bodyend else: h = headbegin + baseFileName + headTitleClose + 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+baseFileName+'.html' else: output_file_path = path+'/'+baseFileName+'.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