2014-01-28 12:56:24 -06:00
|
|
|
#!/usr/bin/env python
|
2013-11-21 21:38:36 -08:00
|
|
|
|
|
|
|
|
import glob
|
|
|
|
|
import markdown
|
|
|
|
|
import os
|
|
|
|
|
import shutil
|
2014-04-05 15:39:02 -07:00
|
|
|
import subprocess
|
2013-12-04 07:46:41 -08:00
|
|
|
import sys
|
|
|
|
|
import time
|
2013-11-21 21:38:36 -08:00
|
|
|
from datetime import datetime
|
|
|
|
|
|
2014-01-31 20:55:37 -08:00
|
|
|
def is_up_to_date(path, out_path):
|
2013-12-04 07:46:41 -08:00
|
|
|
# See if it's up to date.
|
|
|
|
|
source_mod = os.path.getmtime(path)
|
|
|
|
|
source_mod = max(source_mod, os.path.getmtime('doc/site/template.html'))
|
|
|
|
|
|
|
|
|
|
dest_mod = 0
|
|
|
|
|
if os.path.exists(out_path):
|
|
|
|
|
dest_mod = os.path.getmtime(out_path)
|
2014-01-28 13:56:49 -06:00
|
|
|
return source_mod < dest_mod
|
2013-12-04 07:46:41 -08:00
|
|
|
|
2014-01-28 13:56:49 -06:00
|
|
|
def format_file(path, skip_up_to_date):
|
|
|
|
|
basename = os.path.basename(path)
|
|
|
|
|
basename = basename.split('.')[0]
|
|
|
|
|
|
2014-01-31 20:55:37 -08:00
|
|
|
out_path = "build/docs/" + basename + ".html"
|
2014-01-28 13:56:49 -06:00
|
|
|
|
2014-01-31 20:55:37 -08:00
|
|
|
if skip_up_to_date and is_up_to_date(path, out_path):
|
2014-01-28 13:56:49 -06:00
|
|
|
# It's up to date.
|
2013-12-04 07:46:41 -08:00
|
|
|
return
|
|
|
|
|
|
2013-11-21 21:38:36 -08:00
|
|
|
title = ""
|
2014-04-14 21:23:46 -07:00
|
|
|
category = ""
|
2013-11-21 21:38:36 -08:00
|
|
|
|
|
|
|
|
# Read the markdown file and preprocess it.
|
|
|
|
|
contents = ""
|
|
|
|
|
with open(path, "r") as input:
|
|
|
|
|
# Read each line, preprocessing the special codes.
|
|
|
|
|
for line in input:
|
|
|
|
|
stripped = line.lstrip()
|
|
|
|
|
indentation = line[:len(line) - len(stripped)]
|
|
|
|
|
|
|
|
|
|
if stripped.startswith("^"):
|
|
|
|
|
command,_,args = stripped.rstrip("\n").lstrip("^").partition(" ")
|
|
|
|
|
args = args.strip()
|
|
|
|
|
|
|
|
|
|
if command == "title":
|
|
|
|
|
title = args
|
2014-04-14 21:23:46 -07:00
|
|
|
elif command == "category":
|
|
|
|
|
category = args
|
2013-11-21 21:38:36 -08:00
|
|
|
else:
|
|
|
|
|
print "UNKNOWN COMMAND:", command, args
|
|
|
|
|
|
2014-01-31 17:51:09 -08:00
|
|
|
elif stripped.startswith('#'):
|
|
|
|
|
# Add anchors to the headers.
|
|
|
|
|
index = stripped.find(" ")
|
|
|
|
|
headertype = stripped[:index]
|
|
|
|
|
header = stripped[index:].strip()
|
|
|
|
|
anchor = header.lower().replace(' ', '-')
|
|
|
|
|
anchor = anchor.translate(None, '.?!:/')
|
|
|
|
|
|
|
|
|
|
contents += indentation + headertype
|
|
|
|
|
contents += '{1} <a href="#{0}" name="{0}" class="header-anchor">#</a>\n'.format(anchor, header)
|
|
|
|
|
|
2013-11-21 21:38:36 -08:00
|
|
|
else:
|
|
|
|
|
contents = contents + line
|
|
|
|
|
|
|
|
|
|
html = markdown.markdown(contents, ['def_list', 'codehilite'])
|
|
|
|
|
|
|
|
|
|
modified = datetime.fromtimestamp(os.path.getmtime(path))
|
|
|
|
|
mod_str = modified.strftime('%B %d, %Y')
|
|
|
|
|
|
2014-04-14 21:23:46 -07:00
|
|
|
fields = {
|
|
|
|
|
'title': title,
|
|
|
|
|
'html': html,
|
|
|
|
|
'mod': mod_str,
|
2014-04-20 21:42:17 -07:00
|
|
|
'category': category
|
2014-04-14 21:23:46 -07:00
|
|
|
}
|
2013-11-21 21:38:36 -08:00
|
|
|
|
2013-12-04 07:46:41 -08:00
|
|
|
with open("doc/site/template.html") as f:
|
|
|
|
|
template = f.read()
|
|
|
|
|
|
2013-11-21 21:38:36 -08:00
|
|
|
# Write the html output.
|
2013-12-04 07:46:41 -08:00
|
|
|
with open(out_path, 'w') as out:
|
|
|
|
|
out.write(template.format(**fields))
|
2013-11-21 21:38:36 -08:00
|
|
|
|
|
|
|
|
print "converted", basename
|
|
|
|
|
|
2013-12-04 07:46:41 -08:00
|
|
|
|
2014-04-05 15:39:02 -07:00
|
|
|
def check_sass():
|
|
|
|
|
source_mod = os.path.getmtime('doc/site/style.scss')
|
|
|
|
|
|
|
|
|
|
dest_mod = 0
|
|
|
|
|
if os.path.exists('build/docs/style.css'):
|
|
|
|
|
dest_mod = os.path.getmtime('build/docs/style.css')
|
|
|
|
|
|
|
|
|
|
if source_mod < dest_mod:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
subprocess.call(['sass', 'doc/site/style.scss', 'build/docs/style.css'])
|
|
|
|
|
print "built css"
|
|
|
|
|
|
|
|
|
|
|
2013-12-04 07:46:41 -08:00
|
|
|
def format_files(skip_up_to_date):
|
2014-04-05 15:39:02 -07:00
|
|
|
check_sass()
|
|
|
|
|
|
2013-12-04 07:46:41 -08:00
|
|
|
for f in glob.iglob("doc/site/*.markdown"):
|
|
|
|
|
format_file(f, skip_up_to_date)
|
2014-01-31 20:55:37 -08:00
|
|
|
|
2013-12-04 07:46:41 -08:00
|
|
|
|
2013-11-21 21:38:36 -08:00
|
|
|
# Clean the output directory.
|
2014-01-30 06:51:52 -08:00
|
|
|
if not os.path.exists("build"):
|
|
|
|
|
os.mkdir("build")
|
|
|
|
|
|
|
|
|
|
if os.path.exists("build/docs"):
|
|
|
|
|
shutil.rmtree("build/docs")
|
|
|
|
|
os.mkdir("build/docs")
|
2013-11-21 21:38:36 -08:00
|
|
|
|
|
|
|
|
# Process each markdown file.
|
2013-12-04 07:46:41 -08:00
|
|
|
format_files(False)
|
2013-11-21 21:38:36 -08:00
|
|
|
|
2014-01-31 20:55:37 -08:00
|
|
|
# Watch files.
|
2013-12-04 07:46:41 -08:00
|
|
|
if len(sys.argv) == 2 and sys.argv[1] == '--watch':
|
|
|
|
|
while True:
|
|
|
|
|
format_files(True)
|
|
|
|
|
time.sleep(0.3)
|