summaryrefslogtreecommitdiffhomepage
path: root/md2pdf.py
diff options
context:
space:
mode:
authoreliseo papa <elipapa@alum.mit.edu> 2012-10-11 16:34:47 +0100
committereliseo papa <elipapa@alum.mit.edu> 2012-10-11 16:34:47 +0100
commit4db49669ef1a3656a8434c8d74db6e3bcc75852e (patch)
tree312981f859151eb7fb90a4bd6c01b772fc5cba40 /md2pdf.py
parent13d9220c214132fc95449de6a92ec5990f8a55b3 (diff)
created a conversion script and a first draft of a style
Diffstat (limited to 'md2pdf.py')
-rwxr-xr-xmd2pdf.py49
1 files changed, 49 insertions, 0 deletions
diff --git a/md2pdf.py b/md2pdf.py
new file mode 100755
index 0000000..08f5698
--- /dev/null
+++ b/md2pdf.py
@@ -0,0 +1,49 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+"""
+md2pdf
+converts markdown file to PDF using a given CSS style sheet
+
+usage:
+Outputs a PDF file, using the given CSS style sheet
+> md2pdf input.md --css input.css
+"""
+
+import markdown
+import codecs
+from subprocess import call
+
+import argparse
+parser = argparse.ArgumentParser(description='converts markdown file to PDF using a given CSS style sheet')
+
+parser.add_argument('mdsource')
+parser.add_argument('--css', dest="css")
+
+args = parser.parse_args()
+filename = args.mdsource.split('.')[0]
+
+
+if args.css:
+ head = """<!doctype html>
+<head>
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
+ <link href="%s" type="text/css" rel="stylesheet">
+</head>
+""" % args.css
+else:
+ head = """<!doctype html>
+<head>
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
+</head>
+"""
+
+# convert the markdown file to html
+mdsourcef = codecs.open(args.mdsource, mode="r", encoding="utf8")
+body = markdown.markdown(mdsourcef.read())
+html = ''.join([head, body])
+
+output_file = codecs.open("%s.html" % filename, "w", encoding="utf8")
+output_file.write(html)
+print ">>> first writing out %s.html" % filename
+print ">>> calling wkthmltopdf" % filename
+call(["wkhtmltopdf", "%s.html" % filename, "%s.pdf" % filename])