#!/usr/bin/python

import common
import os
import time

template = '''\
<p align=center>
%(navbar)s
</p>

<p class="dateline">%(dateline)s</p>

<p>
%(body)s
</p>

<p class="artcontrol">- Tom |
<a href="%(permalink)s">permalink</a> |
<a href="/viewcvs.cgi/blog/%(filename)s">changelog</a> |
Last updated: %(lastmodified)s</p>

<p align=center>
%(navbar)s
</p>

<a name="comments" />
<p class="commentheader">Comments</p>

%(comments)s

<!-- <p><b><a href="/comment.cgi?id=%(posted)d&page=/blog/%(permalink)s">Post a new comment.</a></b></b> -->
<p><b>Commenting has been suspended due to spam.</b></p>

'''

def view(bp):
    """
    This writes out the html corresponding to a single post.
    """

    subs = bp.subs()

    nav = []

    if bp.previous:
        nav.append('<a href="%s">Previous Entry</a>' % (bp.previous.permalink))
    else:
        nav.append('Previous Entry')

    nav.append('<a href="/index.shtml">Home</a>')

    if bp.next:
        nav.append('<a href="%s">Next Entry</a>' % (bp.next.permalink))
    else:
        nav.append('Next Entry')

    subs['navbar'] = ' <b>|</b> '.join(nav)

    subs['comments'] = ''

    for cid, comment in bp.comments:
        subs['comments'] += '<a name="%s" />' % cid
        subs['comments'] += comment
                
    subs['content'] = template % subs

    fn = common.webdir + "/blog/" + bp.permalink

    f = file(fn, "w")
    f.write(common.format_page(subs))
    f.close()

    # os.utime(fn, (bp.pagemodified, bp.pagemodified))

def main():
    posts = common.blog_posts()

    # Build up a linked list of archive members.
    next = None

    for bp in posts:
        bp.next = next
        if next:
            next.previous = bp

        next = bp
    
    posts[-1].previous = None
        
    for bp in posts:
        view(bp)

if __name__ == "__main__":
    main()
