Since working on this new project where we're using
<http://jottit.com/> for our to-do list, I've become enamored of
Markdown. So I wrote this script to allow me to write documents
originally in Markdown and then generate HTML versions.
This works particularly well with Emacs `longlines-mode`. I wrote the
first draft of my new company's "Acta Constitutiva" (i.e. charter and
bylaws) in it. With CSS and with `M-x recompile` bound to the F5 key,
and `compile-command` set to `(cd ~/distributed-expertise;
~/devel/mkhtml.py bylaws; iceweasel bylaws.html)`, it was a pretty
reasonable word-processing experience, preferable to OpenOffice Write
for the following reasons:
- On my 384MB 700MHz laptop, OpenOffice is painfully slow; Emacs
screams.
- My Emacs has an input method that handles Spanish reasonably;
OpenOffice might, but I haven't been able to find it.
- I could edit in HTML and CSS in the cases where I wanted it, and
pretend I was just editing a normal text file the rest of the time,
with all of the normal Emacs amenities. Except with
word-processor-style word wrap instead of this M-q crap.
- Stylesheeting comes naturally. I just put a `<style>` element at
the top with a few lines inside of it to format nicely.
- I can see more of the document at a time in Emacs.
Like everything else posted to kragen-hacks without any notice to the
contrary, this program is in the public domain; I abandon any
copyright in it.
#!/usr/bin/python
"""Turn Markdown documents into HTML documents.
Depends on python-markdown and Beautiful Soup.
Markdown normally generates HTML document content; this generates HTML
documents instead.
"""
import markdown, BeautifulSoup, sys, os, os.path
def render(text):
"Given Markdown input as a string, produce an HTML document as a string."
body = str(markdown.Markdown(text))
soup = BeautifulSoup.BeautifulSoup(body)
headers = soup('h1')
if len(headers) > 0:
title = headers[0].renderContents()
else:
title = 'Lame document with no top-level header'
return '''<html><head><title>%s</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
</head>
<body>%s</body></html>''' % (title, body)
def process(infile):
"Given a filename of Markdown input, create an HTML file as output."
outfile = infile + '.html'
if os.path.exists(outfile) and \
os.stat(outfile).st_mtime > os.stat(infile).st_mtime:
print "`%s` is newer than `%s`, skipping " % (outfile, infile)
return
outfiletmp = outfile + '.tmp'
fo = file(outfiletmp, 'w')
fo.write(render(file(infile).read()))
fo.close()
os.rename(outfiletmp, outfile) # atomic replace; won't work on Win32
print "rendered `%s` to `%s` " % (infile, outfile)
def main(args):
filenames = args[1:]
if filenames:
for filename in filenames: process(filename)
return 0
else:
print ("usage: `%s foo bar baz`; implicitly writes to `foo.html`, etc."
% args[0])
return 1
if __name__ == '__main__':
sys.exit(main(sys.argv))