diff options
author | guyzmo <guyzmo@leloop.org> | 2011-01-14 11:51:54 +0100 |
---|---|---|
committer | guyzmo <guyzmo@leloop.org> | 2011-01-14 11:51:54 +0100 |
commit | 48c34a6f62e12f7cb2a7a710dc99cb8d53957cfe (patch) | |
tree | f2fd775016309f35ea57a1ee071a9d7231a193f3 /_controllers/blog/categories.py |
init
Diffstat (limited to '_controllers/blog/categories.py')
-rw-r--r-- | _controllers/blog/categories.py | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/_controllers/blog/categories.py b/_controllers/blog/categories.py new file mode 100644 index 0000000..024ce8c --- /dev/null +++ b/_controllers/blog/categories.py @@ -0,0 +1,82 @@ +import os +import shutil +import operator +import feed +from blogofile.cache import bf + +blog = bf.config.controllers.blog + + +def run(): + write_categories() + + +def sort_into_categories(): + categories = set() + for post in blog.posts: + categories.update(post.categories) + for category in categories: + category_posts = [post for post in blog.posts + if category in post.categories] + blog.categorized_posts[category] = category_posts + for category, posts in sorted( + blog.categorized_posts.items(), key=operator.itemgetter(0)): + blog.all_categories.append((category, len(posts))) + + +def write_categories(): + """Write all the blog posts in categories""" + root = bf.util.path_join(blog.path, blog.category_dir) + #Find all the categories: + categories = set() + for post in blog.posts: + categories.update(post.categories) + for category, category_posts in blog.categorized_posts.items(): + #Write category RSS feed + rss_path = bf.util.fs_site_path_helper( + blog.path, blog.category_dir, + category.url_name, "feed") + feed.write_feed(category_posts,rss_path, "rss.mako") + atom_path = bf.util.fs_site_path_helper( + blog.path, blog.category_dir, + category.url_name, "feed", "atom") + feed.write_feed(category_posts, atom_path, "atom.mako") + page_num = 1 + while True: + path = bf.util.path_join(root, category.url_name, + str(page_num), "index.html") + page_posts = category_posts[:blog.posts_per_page] + category_posts = category_posts[blog.posts_per_page:] + #Forward and back links + if page_num > 1: + prev_link = bf.util.site_path_helper( + blog.path, blog.category_dir, category.url_name, + str(page_num - 1)) + else: + prev_link = None + if len(category_posts) > 0: + next_link = bf.util.site_path_helper( + blog.path, blog.category_dir, category.url_name, + str(page_num + 1)) + else: + next_link = None + + env = { + "category": category, + "posts": page_posts, + "prev_link": prev_link, + "next_link": next_link + } + bf.writer.materialize_template("chronological.mako", path, env) + + #Copy category/1 to category/index.html + if page_num == 1: + shutil.copyfile( + bf.util.path_join(bf.writer.output_dir, path), + bf.util.path_join( + bf.writer.output_dir, root, category.url_name, + "index.html")) + #Prepare next iteration + page_num += 1 + if len(category_posts) == 0: + break |