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