blob: d57762d12e99803e22cf9fe74eece4d5f7f11a52 [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):
12 extensions = '-x tables' + ' ' + '-x "toc(title=In This Document)"'
13 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'
47for curdir, subdirs, files in os.walk(SRC_DIR):
48 print 'Processing %s...' % (curdir,),
49 outdir = [x for x in curdir.split(os.path.sep) if x]
50 outdir[0] = HTML_DIR
51 if len(outdir) == 2:
52 category = outdir[-1]
53 outdir = os.path.join(*outdir)
54
55 for subdir in subdirs:
56 os.mkdir(os.path.join(outdir, subdir))
57
58 if 'sidebar.md' in files:
59 sidebar = markdown(os.path.join(curdir, 'sidebar.md'))
60 del files[files.index('sidebar.md')]
61 else:
62 sidebar = ''
63 for f in files:
64 print ' .',
65 if f.endswith('.md'):
66 main = markdown(os.path.join(curdir, f))
67 final = template.safe_substitute(main=main, sidebar=sidebar, category=category, title=get_title(os.path.join(curdir, f)))
68
69 html = file(os.path.join(outdir, f.replace('.md', '.html')), 'w')
70 html.write(final)
71 else:
72 shutil.copy(os.path.join(curdir, f), os.path.join(outdir, f))
73 print
74
75print 'Done.'