blob: 1325a38f964c50c8ab97418274bdc9f42a671cdc [file] [log] [blame]
Skyler Kaufman991ae4d2011-04-07 12:30:41 -07001#!/usr/bin/env python
2
3import os
4import glob
5import shutil
6import string
7import subprocess
8
9
10# call markdown as a subprocess, and capture the output
11def markdown(raw_file):
Jeff Brown590a9d62011-06-30 12:55:34 -070012 extensions = '-x tables -x "toc(title=In This Document)" -x def_list'
Skyler Kaufman991ae4d2011-04-07 12:30:41 -070013 command = 'markdown' + ' ' + extensions + ' ' + raw_file
14 p = subprocess.Popen(command, stdout = subprocess.PIPE, shell = True)
15 return p.communicate()[0]
16
17
18# read just the title (first heading) from a source page
19def get_title(raw_file):
20 for line in open(raw_file, 'r'):
21 if '#' in line:
22 return line.strip(' #\n')
23 return ""
24
25
26# directory to compile the site to (will be clobbered during build!)
27HTML_DIR = 'out'
28# directory to look in for markdown source files
29SRC_DIR = 'src'
30# directory to look in for html templates
31TEMPLATE_DIR = 'templates'
32
33# filenames of templates to load, in order
34TEMPLATE_LIST = ['includes', 'header', 'sidebar', 'main', 'footer']
35
36t = ""
37for f in TEMPLATE_LIST:
38 t += open(os.path.join(TEMPLATE_DIR, f), 'r').read()
39template = string.Template(t)
40
41if os.path.exists(HTML_DIR):
42 shutil.rmtree(HTML_DIR)
43
44os.mkdir(HTML_DIR)
45
46category = 'home'
Jeff Browna049dde2011-06-29 16:51:54 -070047parents = {}
Skyler Kaufman991ae4d2011-04-07 12:30:41 -070048for curdir, subdirs, files in os.walk(SRC_DIR):
49 print 'Processing %s...' % (curdir,),
50 outdir = [x for x in curdir.split(os.path.sep) if x]
51 outdir[0] = HTML_DIR
52 if len(outdir) == 2:
53 category = outdir[-1]
54 outdir = os.path.join(*outdir)
Jeff Browna049dde2011-06-29 16:51:54 -070055
Skyler Kaufman991ae4d2011-04-07 12:30:41 -070056 for subdir in subdirs:
57 os.mkdir(os.path.join(outdir, subdir))
58
Jeff Browna049dde2011-06-29 16:51:54 -070059 parentdir = os.path.dirname(curdir)
60 if parentdir in parents:
61 parent = parents[parentdir]
62 else:
63 parent = ('', '')
64
Skyler Kaufman991ae4d2011-04-07 12:30:41 -070065 if 'sidebar.md' in files:
66 sidebar = markdown(os.path.join(curdir, 'sidebar.md'))
67 del files[files.index('sidebar.md')]
68 else:
Jeff Browna049dde2011-06-29 16:51:54 -070069 sidebar = parent[0]
70
71 if 'sidebar2.md' in files:
72 sidebar2 = markdown(os.path.join(curdir, 'sidebar2.md'))
73 del files[files.index('sidebar2.md')]
74 else:
75 sidebar2 = parent[1]
76
77 parents[curdir] = (sidebar, sidebar2)
78
Skyler Kaufman991ae4d2011-04-07 12:30:41 -070079 for f in files:
80 print ' .',
81 if f.endswith('.md'):
82 main = markdown(os.path.join(curdir, f))
Jeff Browna049dde2011-06-29 16:51:54 -070083 final = template.safe_substitute(main=main, sidebar=sidebar, sidebar2=sidebar2, \
84 category=category, title=get_title(os.path.join(curdir, f)))
Skyler Kaufman991ae4d2011-04-07 12:30:41 -070085
86 html = file(os.path.join(outdir, f.replace('.md', '.html')), 'w')
87 html.write(final)
88 else:
89 shutil.copy(os.path.join(curdir, f), os.path.join(outdir, f))
90 print
91
92print 'Done.'