Created
September 21, 2012 11:26
-
-
Save epoz/3760964 to your computer and use it in GitHub Desktop.
Markdown Watcher and auto regenerater
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
''' | |
Markdown Watcher and auto regenerater | |
While sitting in an aeroplane, I found myself editing a bunch of Markdown | |
files and needing to regenerate the HTML and preview in a browser. | |
It was tedious re-typing the 'markdown' command every time, so I made | |
this little script to watch the *.markdown files and create the corresponding | |
.html flavour if the modification date of the markdown file is newer or the | |
html does not exist yet. | |
No idea if this already existed, to grab it in stead of making it. | |
I could not search, having no connectivity. (of course it does exist, this is a common usecase) | |
And besides, sometimes just writing the code is quicker than trying to find exactly what you need by Googling. | |
NOTE: This assumes you have the 'markdown' executable installed on your path. | |
''' | |
import os, time, sys | |
while True: | |
f = [x for x in os.listdir('.') if x.lower().endswith('.markdown')] | |
for x in f: | |
base = x.split('.')[0] | |
hbase = '%s.html'%base | |
gen = False | |
if not os.path.exists(hbase): | |
gen = True | |
else: | |
if os.stat(x).st_mtime > os.stat(hbase).st_mtime: | |
gen = True | |
if gen: | |
sys.stderr.write('\rDetected change in %s regenerating %s' % (x, hbase)) | |
os.system('markdown %s > %s' % (x, hbase)) | |
for x in range(3): | |
time.sleep(1) | |
sys.stderr.write('.') | |
sys.stderr.write('\r! \r') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment