diff options
Diffstat (limited to 'site_builder/blogbuilder.py')
-rw-r--r-- | site_builder/blogbuilder.py | 43 |
1 files changed, 22 insertions, 21 deletions
diff --git a/site_builder/blogbuilder.py b/site_builder/blogbuilder.py index f45fb65..060049c 100644 --- a/site_builder/blogbuilder.py +++ b/site_builder/blogbuilder.py | |||
@@ -11,61 +11,62 @@ import json | |||
11 | import operator | 11 | import operator |
12 | from datetime import datetime | 12 | from datetime import datetime |
13 | from collections import defaultdict | 13 | from collections import defaultdict |
14 | from site_builder import get_template, get_output_name | 14 | |
15 | from blog import load_post_index | 15 | from . import get_template |
16 | from feeds import Atom1Feed | 16 | from .blog import load_post_index |
17 | from .feeds import Atom1Feed | ||
17 | 18 | ||
18 | 19 | ||
19 | def build_feed(output_dir, post_index, title, url, feed_url): | 20 | def build_feed(output_dir, post_index, title, url, feed_url): |
20 | page_name = os.path.join(output_dir, 'feed.atom') | 21 | page_name = os.path.join(output_dir, "feed.atom") |
21 | feed = Atom1Feed(post_index, title, feed_url, | 22 | feed = Atom1Feed(post_index, title, feed_url, |
22 | post_index[0].post_date, url) | 23 | post_index[0].post_date, url) |
23 | 24 | ||
24 | open(page_name, 'w').write(feed.get_feed()) | 25 | open(page_name, "w").write(feed.get_feed()) |
25 | 26 | ||
26 | 27 | ||
27 | def build_tags(output_dir, post_index): | 28 | def build_tags(output_dir, post_index): |
28 | tag_index = defaultdict(list) | 29 | tag_index = defaultdict(list) |
29 | 30 | ||
30 | template = get_template('blog_tags.html') | 31 | template = get_template("blog_tags.html") |
31 | page_name = os.path.join(output_dir, 'tags.html') | 32 | page_name = os.path.join(output_dir, "tags.html") |
32 | 33 | ||
33 | for post in post_index: | 34 | for post in post_index: |
34 | for tag in post.tags: | 35 | for tag in post.tags: |
35 | tag_index[tag].append(post) | 36 | tag_index[tag].append(post) |
36 | 37 | ||
37 | tag_index = sorted(tag_index.items()) | 38 | tag_index = sorted(tag_index.items()) |
38 | open(page_name, 'w').write(template.render(posts=tag_index, | 39 | open(page_name, "w").write(template.render(posts=tag_index, |
39 | build_date=datetime.now().strftime("%B %d, %Y"))) | 40 | build_date=datetime.now().strftime("%B %d, %Y"))) |
40 | 41 | ||
41 | 42 | ||
42 | def build_archive(output_dir, post_index): | 43 | def build_archive(output_dir, post_index): |
43 | date_index = defaultdict(list) | 44 | date_index = defaultdict(list) |
44 | 45 | ||
45 | template = get_template('blog_archive.html') | 46 | template = get_template("blog_archive.html") |
46 | page_name = os.path.join(output_dir, 'archive.html') | 47 | page_name = os.path.join(output_dir, "archive.html") |
47 | 48 | ||
48 | for post in post_index: | 49 | for post in post_index: |
49 | date_index[post.post_date.year].append(post) | 50 | date_index[post.post_date.year].append(post) |
50 | 51 | ||
51 | date_index = sorted(date_index.items(), reverse=True) | 52 | date_index = sorted(date_index.items(), reverse=True) |
52 | open(page_name, 'w').write(template.render(posts=date_index, | 53 | open(page_name, "w").write(template.render(posts=date_index, |
53 | build_date=datetime.now().strftime("%B %d, %Y"))) | 54 | build_date=datetime.now().strftime("%B %d, %Y"))) |
54 | 55 | ||
55 | 56 | ||
56 | def build_index(output_dir, post_index): | 57 | def build_index(output_dir, post_index): |
57 | template = get_template('blog_index.html') | 58 | template = get_template("blog_index.html") |
58 | page_name = os.path.join(output_dir, 'index.html') | 59 | page_name = os.path.join(output_dir, "index.html") |
59 | 60 | ||
60 | open(page_name, 'w').write(template.render(posts=post_index[:3], | 61 | open(page_name, "w").write(template.render(posts=post_index[:3], |
61 | build_date=datetime.now().strftime("%B %d, %Y"))) | 62 | build_date=datetime.now().strftime("%B %d, %Y"))) |
62 | 63 | ||
63 | 64 | ||
64 | def build_blog(base_dir, output_dir): | 65 | def build_blog(base_dir, output_dir): |
65 | config = json.load(open(os.path.join(base_dir, 'blog.cfg'))) | 66 | config = json.load(open(os.path.join(base_dir, "blog.cfg"))) |
66 | 67 | ||
67 | post_index = load_post_index(base_dir) | 68 | post_index = load_post_index(base_dir) |
68 | post_index.sort(key=operator.attrgetter('post_date'), reverse=True) | 69 | post_index.sort(key=operator.attrgetter("post_date"), reverse=True) |
69 | 70 | ||
70 | try: | 71 | try: |
71 | os.makedirs(output_dir) | 72 | os.makedirs(output_dir) |
@@ -73,17 +74,17 @@ def build_blog(base_dir, output_dir): | |||
73 | pass # directory already exists | 74 | pass # directory already exists |
74 | 75 | ||
75 | for post in post_index: | 76 | for post in post_index: |
76 | template = get_template('blog_post.html') | 77 | template = get_template("blog_post.html") |
77 | 78 | ||
78 | out_filename = os.path.join(output_dir, post.filename) | 79 | out_filename = os.path.join(output_dir, post.filename) |
79 | out_filename = out_filename[:-len('rst')] + 'html' | 80 | out_filename = out_filename[:-len("rst")] + "html" |
80 | 81 | ||
81 | print "BUILDING BLOG: ", out_filename | 82 | print("BUILDING BLOG: ", out_filename) |
82 | 83 | ||
83 | open(out_filename, 'w').write(template.render(post=post)) | 84 | open(out_filename, "w").write(template.render(post=post)) |
84 | 85 | ||
85 | build_index(output_dir, post_index) | 86 | build_index(output_dir, post_index) |
86 | build_archive(output_dir, post_index) | 87 | build_archive(output_dir, post_index) |
87 | build_tags(output_dir, post_index) | 88 | build_tags(output_dir, post_index) |
88 | build_feed(output_dir, post_index, | 89 | build_feed(output_dir, post_index, |
89 | config['title'], config['blog_url'], config['feed_url']) | 90 | config["title"], config["blog_url"], config["feed_url"]) |