Files
wren/script/generate_docs.py

91 lines
2.1 KiB
Python
Raw Normal View History

#!/usr/bin/env python
2013-11-21 21:38:36 -08:00
import glob
import markdown
import os
import shutil
2013-12-04 07:46:41 -08:00
import sys
import time
2013-11-21 21:38:36 -08:00
from datetime import datetime
2013-12-04 07:46:41 -08:00
def format_file(path, skip_up_to_date):
2013-11-21 21:38:36 -08:00
basename = os.path.basename(path)
basename = basename.split('.')[0]
2014-01-30 06:51:52 -08:00
out_path = "build/docs/" + basename + ".html"
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)
if skip_up_to_date and source_mod < dest_mod:
return
2013-11-21 21:38:36 -08:00
title = ""
# 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
else:
print "UNKNOWN COMMAND:", command, args
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')
fields = {'title': title, 'html': html, 'mod': mod_str}
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
def format_files(skip_up_to_date):
for f in glob.iglob("doc/site/*.markdown"):
format_file(f, skip_up_to_date)
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
# Copy the CSS file.
2014-01-30 06:51:52 -08:00
shutil.copyfile("doc/site/style.css", "build/docs/style.css")
2013-12-04 07:46:41 -08:00
# TODO(bob): Check for CSS modification.
if len(sys.argv) == 2 and sys.argv[1] == '--watch':
while True:
format_files(True)
time.sleep(0.3)