blob: 33a1e3302da5f85a066bfa6a6d51970da1abf24d [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
Conley Owensb63bdee2012-06-04 10:45:42 -070017import codecs
Skyler Kaufman991ae4d2011-04-07 12:30:41 -070018import glob
Conley Owensb63bdee2012-06-04 10:45:42 -070019import markdown
20import os
Skyler Kaufman991ae4d2011-04-07 12:30:41 -070021import shutil
22import string
23import subprocess
24
25
Skyler Kaufman991ae4d2011-04-07 12:30:41 -070026# read just the title (first heading) from a source page
27def get_title(raw_file):
28 for line in open(raw_file, 'r'):
29 if '#' in line:
30 return line.strip(' #\n')
Omari Stephensbdac7662011-12-13 16:51:27 -080031 return ''
Skyler Kaufman991ae4d2011-04-07 12:30:41 -070032
33
34# directory to compile the site to (will be clobbered during build!)
35HTML_DIR = 'out'
36# directory to look in for markdown source files
37SRC_DIR = 'src'
38# directory to look in for html templates
39TEMPLATE_DIR = 'templates'
40
41# filenames of templates to load, in order
Omari Stephensbdac7662011-12-13 16:51:27 -080042TEMPLATE_LIST = ['includes', 'header', 'sidebar', 'main', 'footer']
Skyler Kaufman991ae4d2011-04-07 12:30:41 -070043
Omari Stephensbdac7662011-12-13 16:51:27 -080044# Step 1, concatenate the template pieces into a single template string
45t = ''
Skyler Kaufman991ae4d2011-04-07 12:30:41 -070046for f in TEMPLATE_LIST:
47 t += open(os.path.join(TEMPLATE_DIR, f), 'r').read()
48template = string.Template(t)
49
Omari Stephensbdac7662011-12-13 16:51:27 -080050# Step 2, rm -rf HTML_DIR if it exists, and then re-create it
Skyler Kaufman991ae4d2011-04-07 12:30:41 -070051if os.path.exists(HTML_DIR):
52 shutil.rmtree(HTML_DIR)
53
54os.mkdir(HTML_DIR)
55
Omari Stephensbdac7662011-12-13 16:51:27 -080056# Step 3, recursively mirror SRC_DIR to HTML_DIR, directory by directory, translating *.md
Skyler Kaufman991ae4d2011-04-07 12:30:41 -070057category = 'home'
Jeff Browna049dde2011-06-29 16:51:54 -070058parents = {}
Skyler Kaufman991ae4d2011-04-07 12:30:41 -070059for curdir, subdirs, files in os.walk(SRC_DIR):
Conley Owensb63bdee2012-06-04 10:45:42 -070060 def md(path):
61 text = codecs.open(path, encoding='utf8').read()
62 extensions = ['tables', 'def_list', 'toc(title=In This Document)']
63 return markdown.markdown(text, extensions)
64
Skyler Kaufman991ae4d2011-04-07 12:30:41 -070065 print 'Processing %s...' % (curdir,),
Omari Stephensbdac7662011-12-13 16:51:27 -080066 # Step A: split path, and update cached category name if needed
67 curdir = os.path.normpath(curdir)
68 outdir = curdir.split(os.path.sep)
Skyler Kaufman991ae4d2011-04-07 12:30:41 -070069 outdir[0] = HTML_DIR
70 if len(outdir) == 2:
71 category = outdir[-1]
72 outdir = os.path.join(*outdir)
Jeff Browna049dde2011-06-29 16:51:54 -070073
Omari Stephensbdac7662011-12-13 16:51:27 -080074 # Step B: mirror the hierarchy of immediate subdirectories
Skyler Kaufman991ae4d2011-04-07 12:30:41 -070075 for subdir in subdirs:
76 os.mkdir(os.path.join(outdir, subdir))
77
Omari Stephensbdac7662011-12-13 16:51:27 -080078 # Step C: cache the translated sidebars, keyed by parent dir, so we can do sidebar inheritance
79 # FIXME: make this depth-agnostic, perhaps by caching all sidebars and moving the resolution
80 # FIXME: complexity out of the datastructure and into the resolution algorithm.
Jeff Browna049dde2011-06-29 16:51:54 -070081 parentdir = os.path.dirname(curdir)
82 if parentdir in parents:
83 parent = parents[parentdir]
84 else:
Omari Stephensbdac7662011-12-13 16:51:27 -080085 parent = ('', '', '')
Jeff Browna049dde2011-06-29 16:51:54 -070086
Skyler Kaufman991ae4d2011-04-07 12:30:41 -070087 if 'sidebar.md' in files:
Conley Owensb63bdee2012-06-04 10:45:42 -070088 sidebar = md(os.path.join(curdir, 'sidebar.md'))
Skyler Kaufman991ae4d2011-04-07 12:30:41 -070089 del files[files.index('sidebar.md')]
90 else:
Jeff Browna049dde2011-06-29 16:51:54 -070091 sidebar = parent[0]
92
93 if 'sidebar2.md' in files:
Conley Owensb63bdee2012-06-04 10:45:42 -070094 sidebar2 = md(os.path.join(curdir, 'sidebar2.md'))
Jeff Browna049dde2011-06-29 16:51:54 -070095 del files[files.index('sidebar2.md')]
96 else:
97 sidebar2 = parent[1]
98
Omari Stephensbdac7662011-12-13 16:51:27 -080099 if 'sidebar3.md' in files:
Conley Owensb63bdee2012-06-04 10:45:42 -0700100 sidebar3 = md(os.path.join(curdir, 'sidebar3.md'))
Omari Stephensbdac7662011-12-13 16:51:27 -0800101 del files[files.index('sidebar3.md')]
102 else:
103 sidebar3 = parent[2]
Jeff Browna049dde2011-06-29 16:51:54 -0700104
Omari Stephensbdac7662011-12-13 16:51:27 -0800105 parents[curdir] = (sidebar, sidebar2, sidebar3)
106
107 # Step D: mirror all non-*.md files, and translate (file).md files into (file).html
Skyler Kaufman991ae4d2011-04-07 12:30:41 -0700108 for f in files:
109 print ' .',
Omari Stephensbdac7662011-12-13 16:51:27 -0800110 # Note that this "absolute" filename has a root at SRC_DIR, not "/"
111 absfilename = os.path.join(curdir, f)
112
Skyler Kaufman991ae4d2011-04-07 12:30:41 -0700113 if f.endswith('.md'):
Conley Owensb63bdee2012-06-04 10:45:42 -0700114 main = md(absfilename)
Jeff Browna049dde2011-06-29 16:51:54 -0700115 final = template.safe_substitute(main=main, sidebar=sidebar, sidebar2=sidebar2, \
Omari Stephensbdac7662011-12-13 16:51:27 -0800116 sidebar3=sidebar3, category=category, title=get_title(absfilename))
117
Conley Owensb63bdee2012-06-04 10:45:42 -0700118 html = codecs.open(os.path.join(outdir, f.replace('.md', '.html')), 'w', encoding="utf8")
Skyler Kaufman991ae4d2011-04-07 12:30:41 -0700119 html.write(final)
120 else:
Omari Stephensbdac7662011-12-13 16:51:27 -0800121 shutil.copy(absfilename, os.path.join(outdir, f))
Skyler Kaufman991ae4d2011-04-07 12:30:41 -0700122 print
123
124print 'Done.'