diff options
Diffstat (limited to 'site_builder/__init__.py')
-rw-r--r-- | site_builder/__init__.py | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/site_builder/__init__.py b/site_builder/__init__.py new file mode 100644 index 0000000..f60820a --- /dev/null +++ b/site_builder/__init__.py | |||
@@ -0,0 +1,63 @@ | |||
1 | # vim: set filencoding=utf8 | ||
2 | """ | ||
3 | Site Builder | ||
4 | |||
5 | @author: Mike Crute (mcrute@ag.com) | ||
6 | @organization: American Greetings Interactive | ||
7 | @date: June 04, 2010 | ||
8 | """ | ||
9 | |||
10 | import os | ||
11 | import jinja2 | ||
12 | import pagebuilder | ||
13 | from datetime import datetime | ||
14 | from docutils.core import publish_parts | ||
15 | from docutils.io import FileInput | ||
16 | |||
17 | |||
18 | def get_template(name): | ||
19 | loader = jinja2.FileSystemLoader('templates') | ||
20 | renderer = jinja2.Environment(loader=loader) | ||
21 | return renderer.get_template(name) | ||
22 | |||
23 | |||
24 | def build_standard_page(filename, output_name): | ||
25 | parts = publish_parts(open(filename, 'r').read(), writer_name='html') | ||
26 | template = get_template('page.html') | ||
27 | |||
28 | try: | ||
29 | os.makedirs(os.path.dirname(output_name)) | ||
30 | except OSError: | ||
31 | pass # directory exists | ||
32 | |||
33 | open(output_name, 'w').write(template.render( | ||
34 | contents=parts['html_body'], | ||
35 | build_date=datetime.now().strftime('%B %d, %Y'), | ||
36 | source_link=filename)) | ||
37 | |||
38 | |||
39 | def get_output_name(base_dir, output_dir, filename): | ||
40 | base_depth = len(base_dir.split(os.path.sep)) | ||
41 | out_name = filename.split(os.path.sep)[base_depth:] | ||
42 | new_path = os.path.join(output_dir, *out_name) | ||
43 | |||
44 | if new_path.endswith('.rst'): | ||
45 | new_path = new_path[:-len('.rst')] + '.html' | ||
46 | |||
47 | return new_path | ||
48 | |||
49 | |||
50 | def build_all(base_dir, output_dir): | ||
51 | for root, dirs, files in os.walk(base_dir): | ||
52 | for filename in files: | ||
53 | if ('personal_blog' in root or | ||
54 | 'blog' in root or | ||
55 | not filename.endswith('.rst')): | ||
56 | continue | ||
57 | |||
58 | old_path = os.path.join(root, filename) | ||
59 | new_path = get_output_name(base_dir, output_dir, old_path) | ||
60 | |||
61 | print "BUILDING: ", old_path | ||
62 | |||
63 | build_standard_page(old_path, new_path) | ||