Skyler Kaufman | 991ae4d | 2011-04-07 12:30:41 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import os |
| 4 | import glob |
| 5 | import shutil |
| 6 | import string |
| 7 | import subprocess |
| 8 | |
| 9 | |
| 10 | # call markdown as a subprocess, and capture the output |
| 11 | def 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 |
| 19 | def 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!) |
| 27 | HTML_DIR = 'out' |
| 28 | # directory to look in for markdown source files |
| 29 | SRC_DIR = 'src' |
| 30 | # directory to look in for html templates |
| 31 | TEMPLATE_DIR = 'templates' |
| 32 | |
| 33 | # filenames of templates to load, in order |
| 34 | TEMPLATE_LIST = ['includes', 'header', 'sidebar', 'main', 'footer'] |
| 35 | |
| 36 | t = "" |
| 37 | for f in TEMPLATE_LIST: |
| 38 | t += open(os.path.join(TEMPLATE_DIR, f), 'r').read() |
| 39 | template = string.Template(t) |
| 40 | |
| 41 | if os.path.exists(HTML_DIR): |
| 42 | shutil.rmtree(HTML_DIR) |
| 43 | |
| 44 | os.mkdir(HTML_DIR) |
| 45 | |
| 46 | category = 'home' |
Jeff Brown | a049dde | 2011-06-29 16:51:54 -0700 | [diff] [blame] | 47 | parents = {} |
Skyler Kaufman | 991ae4d | 2011-04-07 12:30:41 -0700 | [diff] [blame] | 48 | for 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 Brown | a049dde | 2011-06-29 16:51:54 -0700 | [diff] [blame] | 55 | |
Skyler Kaufman | 991ae4d | 2011-04-07 12:30:41 -0700 | [diff] [blame] | 56 | for subdir in subdirs: |
| 57 | os.mkdir(os.path.join(outdir, subdir)) |
| 58 | |
Jeff Brown | a049dde | 2011-06-29 16:51:54 -0700 | [diff] [blame] | 59 | parentdir = os.path.dirname(curdir) |
| 60 | if parentdir in parents: |
| 61 | parent = parents[parentdir] |
| 62 | else: |
| 63 | parent = ('', '') |
| 64 | |
Skyler Kaufman | 991ae4d | 2011-04-07 12:30:41 -0700 | [diff] [blame] | 65 | if 'sidebar.md' in files: |
| 66 | sidebar = markdown(os.path.join(curdir, 'sidebar.md')) |
| 67 | del files[files.index('sidebar.md')] |
| 68 | else: |
Jeff Brown | a049dde | 2011-06-29 16:51:54 -0700 | [diff] [blame] | 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 | |
Skyler Kaufman | 991ae4d | 2011-04-07 12:30:41 -0700 | [diff] [blame] | 79 | for f in files: |
| 80 | print ' .', |
| 81 | if f.endswith('.md'): |
| 82 | main = markdown(os.path.join(curdir, f)) |
Jeff Brown | a049dde | 2011-06-29 16:51:54 -0700 | [diff] [blame] | 83 | final = template.safe_substitute(main=main, sidebar=sidebar, sidebar2=sidebar2, \ |
| 84 | category=category, title=get_title(os.path.join(curdir, f))) |
Skyler Kaufman | 991ae4d | 2011-04-07 12:30:41 -0700 | [diff] [blame] | 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 | |
| 92 | print 'Done.' |