blob: 12bcb98607e3619d0257ffac17a120ab7c64caf5 [file] [log] [blame]
Skyler Kaufman991ae4d2011-04-07 12:30:41 -07001#!/usr/bin/env python
2
Omari Stephensbdac7662011-12-13 16:51:27 -08003# Copyright (C) 2011 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
Skyler Kaufman991ae4d2011-04-07 12:30:41 -070017import os
18import glob
19import shutil
20import string
21import subprocess
22
23
24# call markdown as a subprocess, and capture the output
25def markdown(raw_file):
Jeff Brown590a9d62011-06-30 12:55:34 -070026 extensions = '-x tables -x "toc(title=In This Document)" -x def_list'
Skyler Kaufman991ae4d2011-04-07 12:30:41 -070027 command = 'markdown' + ' ' + extensions + ' ' + raw_file
28 p = subprocess.Popen(command, stdout = subprocess.PIPE, shell = True)
29 return p.communicate()[0]
30
31
32# read just the title (first heading) from a source page
33def get_title(raw_file):
34 for line in open(raw_file, 'r'):
35 if '#' in line:
36 return line.strip(' #\n')
Omari Stephensbdac7662011-12-13 16:51:27 -080037 return ''
Skyler Kaufman991ae4d2011-04-07 12:30:41 -070038
39
40# directory to compile the site to (will be clobbered during build!)
41HTML_DIR = 'out'
42# directory to look in for markdown source files
43SRC_DIR = 'src'
44# directory to look in for html templates
45TEMPLATE_DIR = 'templates'
46
47# filenames of templates to load, in order
Omari Stephensbdac7662011-12-13 16:51:27 -080048TEMPLATE_LIST = ['includes', 'header', 'sidebar', 'main', 'footer']
Skyler Kaufman991ae4d2011-04-07 12:30:41 -070049
Omari Stephensbdac7662011-12-13 16:51:27 -080050# Step 1, concatenate the template pieces into a single template string
51t = ''
Skyler Kaufman991ae4d2011-04-07 12:30:41 -070052for f in TEMPLATE_LIST:
53 t += open(os.path.join(TEMPLATE_DIR, f), 'r').read()
54template = string.Template(t)
55
Omari Stephensbdac7662011-12-13 16:51:27 -080056# Step 2, rm -rf HTML_DIR if it exists, and then re-create it
Skyler Kaufman991ae4d2011-04-07 12:30:41 -070057if os.path.exists(HTML_DIR):
58 shutil.rmtree(HTML_DIR)
59
60os.mkdir(HTML_DIR)
61
Omari Stephensbdac7662011-12-13 16:51:27 -080062# Step 3, recursively mirror SRC_DIR to HTML_DIR, directory by directory, translating *.md
Skyler Kaufman991ae4d2011-04-07 12:30:41 -070063category = 'home'
Jeff Browna049dde2011-06-29 16:51:54 -070064parents = {}
Skyler Kaufman991ae4d2011-04-07 12:30:41 -070065for curdir, subdirs, files in os.walk(SRC_DIR):
66 print 'Processing %s...' % (curdir,),
Omari Stephensbdac7662011-12-13 16:51:27 -080067 # Step A: split path, and update cached category name if needed
68 curdir = os.path.normpath(curdir)
69 outdir = curdir.split(os.path.sep)
Skyler Kaufman991ae4d2011-04-07 12:30:41 -070070 outdir[0] = HTML_DIR
71 if len(outdir) == 2:
72 category = outdir[-1]
73 outdir = os.path.join(*outdir)
Jeff Browna049dde2011-06-29 16:51:54 -070074
Omari Stephensbdac7662011-12-13 16:51:27 -080075 # Step B: mirror the hierarchy of immediate subdirectories
Skyler Kaufman991ae4d2011-04-07 12:30:41 -070076 for subdir in subdirs:
77 os.mkdir(os.path.join(outdir, subdir))
78
Omari Stephensbdac7662011-12-13 16:51:27 -080079 # Step C: cache the translated sidebars, keyed by parent dir, so we can do sidebar inheritance
80 # FIXME: make this depth-agnostic, perhaps by caching all sidebars and moving the resolution
81 # FIXME: complexity out of the datastructure and into the resolution algorithm.
Jeff Browna049dde2011-06-29 16:51:54 -070082 parentdir = os.path.dirname(curdir)
83 if parentdir in parents:
84 parent = parents[parentdir]
85 else:
Omari Stephensbdac7662011-12-13 16:51:27 -080086 parent = ('', '', '')
Jeff Browna049dde2011-06-29 16:51:54 -070087
Skyler Kaufman991ae4d2011-04-07 12:30:41 -070088 if 'sidebar.md' in files:
89 sidebar = markdown(os.path.join(curdir, 'sidebar.md'))
90 del files[files.index('sidebar.md')]
91 else:
Jeff Browna049dde2011-06-29 16:51:54 -070092 sidebar = parent[0]
93
94 if 'sidebar2.md' in files:
95 sidebar2 = markdown(os.path.join(curdir, 'sidebar2.md'))
96 del files[files.index('sidebar2.md')]
97 else:
98 sidebar2 = parent[1]
99
Omari Stephensbdac7662011-12-13 16:51:27 -0800100 if 'sidebar3.md' in files:
101 sidebar3 = markdown(os.path.join(curdir, 'sidebar3.md'))
102 del files[files.index('sidebar3.md')]
103 else:
104 sidebar3 = parent[2]
Jeff Browna049dde2011-06-29 16:51:54 -0700105
Omari Stephensbdac7662011-12-13 16:51:27 -0800106 parents[curdir] = (sidebar, sidebar2, sidebar3)
107
108 # Step D: mirror all non-*.md files, and translate (file).md files into (file).html
Skyler Kaufman991ae4d2011-04-07 12:30:41 -0700109 for f in files:
110 print ' .',
Omari Stephensbdac7662011-12-13 16:51:27 -0800111 # Note that this "absolute" filename has a root at SRC_DIR, not "/"
112 absfilename = os.path.join(curdir, f)
113
Skyler Kaufman991ae4d2011-04-07 12:30:41 -0700114 if f.endswith('.md'):
Omari Stephensbdac7662011-12-13 16:51:27 -0800115 main = markdown(absfilename)
Jeff Browna049dde2011-06-29 16:51:54 -0700116 final = template.safe_substitute(main=main, sidebar=sidebar, sidebar2=sidebar2, \
Omari Stephensbdac7662011-12-13 16:51:27 -0800117 sidebar3=sidebar3, category=category, title=get_title(absfilename))
118
Skyler Kaufman991ae4d2011-04-07 12:30:41 -0700119 html = file(os.path.join(outdir, f.replace('.md', '.html')), 'w')
120 html.write(final)
121 else:
Omari Stephensbdac7662011-12-13 16:51:27 -0800122 shutil.copy(absfilename, os.path.join(outdir, f))
Skyler Kaufman991ae4d2011-04-07 12:30:41 -0700123 print
124
125print 'Done.'
Omari Stephensbdac7662011-12-13 16:51:27 -0800126