aboutsummaryrefslogtreecommitdiff
path: root/scripts/build.py
blob: 8188f5117ebd88336ffb23eac4205957fddb4394 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env python

import os
import glob
import shutil
import string
import subprocess


# call markdown as a subprocess, and capture the output
def markdown(raw_file):
  extensions = '-x tables' + ' ' + '-x "toc(title=In This Document)"'
  command = 'markdown' + ' ' + extensions + ' ' + raw_file
  p = subprocess.Popen(command, stdout = subprocess.PIPE, shell = True)
  return p.communicate()[0]


# read just the title (first heading) from a source page
def get_title(raw_file):
  for line in open(raw_file, 'r'):
    if '#' in line:
      return line.strip(' #\n')
  return ""


# directory to compile the site to (will be clobbered during build!)
HTML_DIR = 'out'
# directory to look in for markdown source files
SRC_DIR = 'src'
# directory to look in for html templates
TEMPLATE_DIR = 'templates'

# filenames of templates to load, in order
TEMPLATE_LIST = ['includes', 'header', 'sidebar', 'main', 'footer'] 

t = ""
for f in TEMPLATE_LIST:
  t += open(os.path.join(TEMPLATE_DIR, f), 'r').read()
template = string.Template(t)

if os.path.exists(HTML_DIR):
  shutil.rmtree(HTML_DIR)

os.mkdir(HTML_DIR)

category = 'home'
parents = {}
for curdir, subdirs, files in os.walk(SRC_DIR):
  print 'Processing %s...'  % (curdir,),
  outdir = [x for x in curdir.split(os.path.sep) if x]
  outdir[0] = HTML_DIR
  if len(outdir) == 2:
    category = outdir[-1]
  outdir = os.path.join(*outdir)

  for subdir in subdirs:
    os.mkdir(os.path.join(outdir, subdir))

  parentdir = os.path.dirname(curdir)
  if parentdir in parents:
    parent = parents[parentdir]
  else:
    parent = ('', '')

  if 'sidebar.md' in files:
    sidebar = markdown(os.path.join(curdir, 'sidebar.md'))
    del files[files.index('sidebar.md')]
  else:
    sidebar = parent[0]

  if 'sidebar2.md' in files:
    sidebar2 = markdown(os.path.join(curdir, 'sidebar2.md'))
    del files[files.index('sidebar2.md')]
  else:
    sidebar2 = parent[1]

  parents[curdir] = (sidebar, sidebar2)

  for f in files:
    print ' .',
    if f.endswith('.md'):
      main = markdown(os.path.join(curdir, f))
      final = template.safe_substitute(main=main, sidebar=sidebar, sidebar2=sidebar2, \
          category=category, title=get_title(os.path.join(curdir, f)))
    
      html = file(os.path.join(outdir, f.replace('.md', '.html')), 'w')
      html.write(final)
    else:
      shutil.copy(os.path.join(curdir, f), os.path.join(outdir, f))
  print

print 'Done.'