• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/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'
47parents = {}
48for 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)
55
56  for subdir in subdirs:
57    os.mkdir(os.path.join(outdir, subdir))
58
59  parentdir = os.path.dirname(curdir)
60  if parentdir in parents:
61    parent = parents[parentdir]
62  else:
63    parent = ('', '')
64
65  if 'sidebar.md' in files:
66    sidebar = markdown(os.path.join(curdir, 'sidebar.md'))
67    del files[files.index('sidebar.md')]
68  else:
69    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
79  for f in files:
80    print ' .',
81    if f.endswith('.md'):
82      main = markdown(os.path.join(curdir, f))
83      final = template.safe_substitute(main=main, sidebar=sidebar, sidebar2=sidebar2, \
84          category=category, title=get_title(os.path.join(curdir, f)))
85
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.'
93