1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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
|