Skyler Kaufman | 991ae4d | 2011-04-07 12:30:41 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
Omari Stephens | bdac766 | 2011-12-13 16:51:27 -0800 | [diff] [blame] | 3 | # 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 Owens | b63bdee | 2012-06-04 10:45:42 -0700 | [diff] [blame] | 17 | import codecs |
Skyler Kaufman | 991ae4d | 2011-04-07 12:30:41 -0700 | [diff] [blame] | 18 | import glob |
Conley Owens | b63bdee | 2012-06-04 10:45:42 -0700 | [diff] [blame] | 19 | import markdown |
| 20 | import os |
Skyler Kaufman | 991ae4d | 2011-04-07 12:30:41 -0700 | [diff] [blame] | 21 | import shutil |
| 22 | import string |
| 23 | import subprocess |
| 24 | |
| 25 | |
Skyler Kaufman | 991ae4d | 2011-04-07 12:30:41 -0700 | [diff] [blame] | 26 | # read just the title (first heading) from a source page |
| 27 | def get_title(raw_file): |
| 28 | for line in open(raw_file, 'r'): |
| 29 | if '#' in line: |
| 30 | return line.strip(' #\n') |
Omari Stephens | bdac766 | 2011-12-13 16:51:27 -0800 | [diff] [blame] | 31 | return '' |
Skyler Kaufman | 991ae4d | 2011-04-07 12:30:41 -0700 | [diff] [blame] | 32 | |
| 33 | |
| 34 | # directory to compile the site to (will be clobbered during build!) |
| 35 | HTML_DIR = 'out' |
| 36 | # directory to look in for markdown source files |
| 37 | SRC_DIR = 'src' |
| 38 | # directory to look in for html templates |
| 39 | TEMPLATE_DIR = 'templates' |
| 40 | |
| 41 | # filenames of templates to load, in order |
Omari Stephens | bdac766 | 2011-12-13 16:51:27 -0800 | [diff] [blame] | 42 | TEMPLATE_LIST = ['includes', 'header', 'sidebar', 'main', 'footer'] |
Skyler Kaufman | 991ae4d | 2011-04-07 12:30:41 -0700 | [diff] [blame] | 43 | |
Omari Stephens | bdac766 | 2011-12-13 16:51:27 -0800 | [diff] [blame] | 44 | # Step 1, concatenate the template pieces into a single template string |
| 45 | t = '' |
Skyler Kaufman | 991ae4d | 2011-04-07 12:30:41 -0700 | [diff] [blame] | 46 | for f in TEMPLATE_LIST: |
| 47 | t += open(os.path.join(TEMPLATE_DIR, f), 'r').read() |
| 48 | template = string.Template(t) |
| 49 | |
Omari Stephens | bdac766 | 2011-12-13 16:51:27 -0800 | [diff] [blame] | 50 | # Step 2, rm -rf HTML_DIR if it exists, and then re-create it |
Skyler Kaufman | 991ae4d | 2011-04-07 12:30:41 -0700 | [diff] [blame] | 51 | if os.path.exists(HTML_DIR): |
| 52 | shutil.rmtree(HTML_DIR) |
| 53 | |
| 54 | os.mkdir(HTML_DIR) |
| 55 | |
Omari Stephens | bdac766 | 2011-12-13 16:51:27 -0800 | [diff] [blame] | 56 | # Step 3, recursively mirror SRC_DIR to HTML_DIR, directory by directory, translating *.md |
Skyler Kaufman | 991ae4d | 2011-04-07 12:30:41 -0700 | [diff] [blame] | 57 | category = 'home' |
Jeff Brown | a049dde | 2011-06-29 16:51:54 -0700 | [diff] [blame] | 58 | parents = {} |
Skyler Kaufman | 991ae4d | 2011-04-07 12:30:41 -0700 | [diff] [blame] | 59 | for curdir, subdirs, files in os.walk(SRC_DIR): |
Conley Owens | b63bdee | 2012-06-04 10:45:42 -0700 | [diff] [blame] | 60 | 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 Kaufman | 991ae4d | 2011-04-07 12:30:41 -0700 | [diff] [blame] | 65 | print 'Processing %s...' % (curdir,), |
Omari Stephens | bdac766 | 2011-12-13 16:51:27 -0800 | [diff] [blame] | 66 | # 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 Kaufman | 991ae4d | 2011-04-07 12:30:41 -0700 | [diff] [blame] | 69 | outdir[0] = HTML_DIR |
| 70 | if len(outdir) == 2: |
| 71 | category = outdir[-1] |
| 72 | outdir = os.path.join(*outdir) |
Jeff Brown | a049dde | 2011-06-29 16:51:54 -0700 | [diff] [blame] | 73 | |
Omari Stephens | bdac766 | 2011-12-13 16:51:27 -0800 | [diff] [blame] | 74 | # Step B: mirror the hierarchy of immediate subdirectories |
Skyler Kaufman | 991ae4d | 2011-04-07 12:30:41 -0700 | [diff] [blame] | 75 | for subdir in subdirs: |
| 76 | os.mkdir(os.path.join(outdir, subdir)) |
| 77 | |
Omari Stephens | bdac766 | 2011-12-13 16:51:27 -0800 | [diff] [blame] | 78 | # 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 Brown | a049dde | 2011-06-29 16:51:54 -0700 | [diff] [blame] | 81 | parentdir = os.path.dirname(curdir) |
| 82 | if parentdir in parents: |
| 83 | parent = parents[parentdir] |
| 84 | else: |
Omari Stephens | bdac766 | 2011-12-13 16:51:27 -0800 | [diff] [blame] | 85 | parent = ('', '', '') |
Jeff Brown | a049dde | 2011-06-29 16:51:54 -0700 | [diff] [blame] | 86 | |
Skyler Kaufman | 991ae4d | 2011-04-07 12:30:41 -0700 | [diff] [blame] | 87 | if 'sidebar.md' in files: |
Conley Owens | b63bdee | 2012-06-04 10:45:42 -0700 | [diff] [blame] | 88 | sidebar = md(os.path.join(curdir, 'sidebar.md')) |
Skyler Kaufman | 991ae4d | 2011-04-07 12:30:41 -0700 | [diff] [blame] | 89 | del files[files.index('sidebar.md')] |
| 90 | else: |
Jeff Brown | a049dde | 2011-06-29 16:51:54 -0700 | [diff] [blame] | 91 | sidebar = parent[0] |
| 92 | |
| 93 | if 'sidebar2.md' in files: |
Conley Owens | b63bdee | 2012-06-04 10:45:42 -0700 | [diff] [blame] | 94 | sidebar2 = md(os.path.join(curdir, 'sidebar2.md')) |
Jeff Brown | a049dde | 2011-06-29 16:51:54 -0700 | [diff] [blame] | 95 | del files[files.index('sidebar2.md')] |
| 96 | else: |
| 97 | sidebar2 = parent[1] |
| 98 | |
Omari Stephens | bdac766 | 2011-12-13 16:51:27 -0800 | [diff] [blame] | 99 | if 'sidebar3.md' in files: |
Conley Owens | b63bdee | 2012-06-04 10:45:42 -0700 | [diff] [blame] | 100 | sidebar3 = md(os.path.join(curdir, 'sidebar3.md')) |
Omari Stephens | bdac766 | 2011-12-13 16:51:27 -0800 | [diff] [blame] | 101 | del files[files.index('sidebar3.md')] |
| 102 | else: |
| 103 | sidebar3 = parent[2] |
Jeff Brown | a049dde | 2011-06-29 16:51:54 -0700 | [diff] [blame] | 104 | |
Omari Stephens | bdac766 | 2011-12-13 16:51:27 -0800 | [diff] [blame] | 105 | parents[curdir] = (sidebar, sidebar2, sidebar3) |
| 106 | |
| 107 | # Step D: mirror all non-*.md files, and translate (file).md files into (file).html |
Skyler Kaufman | 991ae4d | 2011-04-07 12:30:41 -0700 | [diff] [blame] | 108 | for f in files: |
| 109 | print ' .', |
Omari Stephens | bdac766 | 2011-12-13 16:51:27 -0800 | [diff] [blame] | 110 | # Note that this "absolute" filename has a root at SRC_DIR, not "/" |
| 111 | absfilename = os.path.join(curdir, f) |
| 112 | |
Skyler Kaufman | 991ae4d | 2011-04-07 12:30:41 -0700 | [diff] [blame] | 113 | if f.endswith('.md'): |
Conley Owens | b63bdee | 2012-06-04 10:45:42 -0700 | [diff] [blame] | 114 | main = md(absfilename) |
Jeff Brown | a049dde | 2011-06-29 16:51:54 -0700 | [diff] [blame] | 115 | final = template.safe_substitute(main=main, sidebar=sidebar, sidebar2=sidebar2, \ |
Omari Stephens | bdac766 | 2011-12-13 16:51:27 -0800 | [diff] [blame] | 116 | sidebar3=sidebar3, category=category, title=get_title(absfilename)) |
| 117 | |
Conley Owens | b63bdee | 2012-06-04 10:45:42 -0700 | [diff] [blame] | 118 | html = codecs.open(os.path.join(outdir, f.replace('.md', '.html')), 'w', encoding="utf8") |
Skyler Kaufman | 991ae4d | 2011-04-07 12:30:41 -0700 | [diff] [blame] | 119 | html.write(final) |
| 120 | else: |
Omari Stephens | bdac766 | 2011-12-13 16:51:27 -0800 | [diff] [blame] | 121 | shutil.copy(absfilename, os.path.join(outdir, f)) |
Skyler Kaufman | 991ae4d | 2011-04-07 12:30:41 -0700 | [diff] [blame] | 122 | print |
| 123 | |
| 124 | print 'Done.' |