diff options
Diffstat (limited to 'site_builder/__init__.py')
-rw-r--r-- | site_builder/__init__.py | 39 |
1 files changed, 20 insertions, 19 deletions
diff --git a/site_builder/__init__.py b/site_builder/__init__.py index 1ebef36..9478dce 100644 --- a/site_builder/__init__.py +++ b/site_builder/__init__.py | |||
@@ -7,15 +7,13 @@ Site Builder | |||
7 | """ | 7 | """ |
8 | 8 | ||
9 | import os | 9 | import os |
10 | from os import path | ||
11 | import jinja2 | 10 | import jinja2 |
12 | import pagebuilder | ||
13 | from datetime import datetime | 11 | from datetime import datetime |
14 | from docutils.core import publish_parts | 12 | from docutils.core import publish_parts |
15 | from docutils.io import FileInput | ||
16 | 13 | ||
17 | 14 | ||
18 | TEMPLATES = path.join(path.dirname(path.dirname(__file__)), "templates") | 15 | TEMPLATES = os.path.join( |
16 | os.path.dirname(os.path.dirname(__file__)), "templates") | ||
19 | 17 | ||
20 | 18 | ||
21 | def get_template(name): | 19 | def get_template(name): |
@@ -25,27 +23,27 @@ def get_template(name): | |||
25 | 23 | ||
26 | 24 | ||
27 | def build_standard_page(filename, output_name): | 25 | def build_standard_page(filename, output_name): |
28 | parts = publish_parts(open(filename, 'r').read(), writer_name='html') | 26 | parts = publish_parts(open(filename, "r").read(), writer_name="html") |
29 | template = get_template('page.html') | 27 | template = get_template("page.html") |
30 | 28 | ||
31 | try: | 29 | try: |
32 | os.makedirs(path.dirname(output_name)) | 30 | os.makedirs(os.path.dirname(output_name)) |
33 | except OSError: | 31 | except OSError: |
34 | pass # directory exists | 32 | pass # directory exists |
35 | 33 | ||
36 | open(output_name, 'w').write(template.render( | 34 | open(output_name, "w").write(template.render( |
37 | contents=parts['html_body'], | 35 | contents=parts["html_body"], |
38 | build_date=datetime.now().strftime('%B %d, %Y'), | 36 | build_date=datetime.now().strftime("%B %d, %Y"), |
39 | source_link=filename)) | 37 | source_link=filename)) |
40 | 38 | ||
41 | 39 | ||
42 | def get_output_name(base_dir, output_dir, filename): | 40 | def get_output_name(base_dir, output_dir, filename): |
43 | base_depth = len(base_dir.split(path.sep)) | 41 | base_depth = len(base_dir.split(os.path.sep)) - 1 |
44 | out_name = filename.split(path.sep)[base_depth:] | 42 | out_name = filename.split(os.path.sep)[base_depth:] |
45 | new_path = path.join(output_dir, *out_name) | 43 | new_path = os.path.join(output_dir, *out_name) |
46 | 44 | ||
47 | if new_path.endswith('.rst'): | 45 | if new_path.endswith(".rst"): |
48 | new_path = new_path[:-len('.rst')] + '.html' | 46 | new_path = new_path[:-len(".rst")] + ".html" |
49 | 47 | ||
50 | return new_path | 48 | return new_path |
51 | 49 | ||
@@ -53,13 +51,16 @@ def get_output_name(base_dir, output_dir, filename): | |||
53 | def build_all(base_dir, output_dir): | 51 | def build_all(base_dir, output_dir): |
54 | for root, dirs, files in os.walk(base_dir): | 52 | for root, dirs, files in os.walk(base_dir): |
55 | for filename in files: | 53 | for filename in files: |
56 | if ('blog.cfg' in files or | 54 | if ("blog.cfg" in files or |
57 | not filename.endswith('.rst')): | 55 | not filename.endswith(".rst")): |
58 | continue | 56 | continue |
59 | 57 | ||
60 | old_path = path.join(root, filename) | 58 | old_path = os.path.join(root, filename) |
61 | new_path = get_output_name(base_dir, output_dir, old_path) | 59 | new_path = get_output_name(base_dir, output_dir, old_path) |
62 | 60 | ||
63 | print "BUILDING: ", old_path | 61 | if not os.path.exists(os.path.dirname(new_path)): |
62 | os.makedirs(os.path.dirname(new_path)) | ||
63 | |||
64 | print("BUILDING: ", old_path, " to ", new_path) | ||
64 | 65 | ||
65 | build_standard_page(old_path, new_path) | 66 | build_standard_page(old_path, new_path) |