summaryrefslogtreecommitdiffstats
path: root/_controllers/blog/archives.py
blob: ed9e45f2da1bf86afaa92ffc349fab956adedbc5 (plain)
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
################################################################################
## Archives controller
##
## Writes out yearly, monthly, and daily archives.
## Each archive is navigable to the next and previous archive
## in which posts were made.
################################################################################

import operator

from blogofile.cache import bf
import chronological

blog = bf.config.controllers.blog


def run():
    write_monthly_archives()


def sort_into_archives():
    #This is run in 0.initial.py
    for post in blog.posts:
        link = post.date.strftime("archive/%Y/%m")
        try:
            blog.archived_posts[link].append(post)
        except KeyError:
            blog.archived_posts[link] = [post]
    for archive, posts in sorted(
        blog.archived_posts.items(), key=operator.itemgetter(0), reverse=True):
        name = posts[0].date.strftime("%B %Y")
        blog.archive_links.append((archive, name, len(posts)))


def write_monthly_archives():
    for link, posts in blog.archived_posts.items():
        name = posts[0].date.strftime("%B %Y")
        chronological.write_blog_chron(posts, root=link)