Created
November 29, 2009 06:39
-
-
Save EnigmaCurry/244820 to your computer and use it in GitHub Desktop.
Blogofile template for generating a index of posts for each category
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%inherit file="_templates/site.mako" /> | |
These are all the categories: | |
<ul> | |
% for category, link in category_link_map.items(): | |
<li><a href="${link}">${category}</a></li> | |
% endfor | |
</ul> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%inherit file="_templates/site.mako" /> | |
All the posts in the ${category} category: | |
<ul> | |
% for post in posts: | |
<li><a href="${post.permalink}">${post.title}</a></li> | |
% endfor | |
</ul> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from blogofile.cache import bf | |
def run(): | |
category_post_map = generate_map() | |
write_category_index(category_post_map) | |
write_post_indexes(category_post_map) | |
def generate_map(): | |
# Generate a map of categories to posts | |
category_post_map = {} | |
for p in bf.posts: | |
for c in p.categories: | |
try: | |
category_post_map[c.name].add(p) | |
except KeyError: | |
category_post_map[c.name] = set([p]) | |
return category_post_map | |
def category_url(category): | |
return "/category_indexes/"+category.lower().replace(" ","-") | |
def write_category_index(category_post_map): | |
"""Write a page listing all the categories""" | |
template = bf.writer.template_lookup.get_template("category_index.mako") | |
template.output_encoding = "utf-8" | |
category_link_map = {} | |
for category in category_post_map.keys(): | |
category_link_map[category] = category_url(category) | |
html = bf.writer.template_render(template, { | |
"category_link_map" : category_link_map}) | |
path = bf.util.path_join(bf.writer.output_dir,"category_indexes") | |
bf.util.mkdir(path) | |
f = open(bf.util.path_join(path,"index.html"),"w") | |
f.write(html) | |
f.close() | |
def write_post_indexes(category_post_map): | |
"""Write a page for each category listing all the posts for each category""" | |
template = bf.writer.template_lookup.get_template("category_index_posts.mako") | |
template.output_encoding = "utf-8" | |
for category, posts in category_post_map.items(): | |
html = bf.writer.template_render(template, { | |
"posts" : posts, | |
"category" : category}) | |
path = bf.util.path_join( | |
bf.writer.output_dir,category_url(category)) | |
bf.util.mkdir(path) | |
f = open(bf.util.path_join(path,"index.html"),"w") | |
f.write(html) | |
f.close() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment