#!/usr/bin/python

import common
import time

def archive(suffix=".blog.txt", prefix="", title=""):

    content = ""

    for bp in common.blog_posts(suffix=suffix):

        comments = ""
        if len(bp.comments) == 1:
            comments = " (1 comment)"
        elif len(bp.comments) > 1:
            comments = " (%d comments)" % len(bp.comments)
        
        content += '<b>%s:</b> <a href="%s">%s</a>%s<br />\n' % (
            time.strftime("%Y-%02m-%02d", time.localtime(bp.posted)),
            prefix + bp.permalink,
            bp.title, comments)

    subs = {}
    subs['title'] = title
    subs['content'] = content

    return common.format_page(subs)

def main():
    f = file(common.webdir + "/archives.shtml", "w")
    f.write(archive(prefix="/blog/", title="Archives"))
    f.close()

if __name__ == "__main__":
    main()
