#!/usr/bin/python
import os,re,sys
from Cheetah.Template import Template

dir = os.listdir('.')
list=[(os.stat(name).st_mtime, name) for name in dir if name[-4:]=='.txt']

list.sort()
list.reverse()

channel = {
  'title':	 "Sam Ruby",
  'link':	 "http://www.intertwingly.net/blog/",
  'description': "It's just data",
}

Items=[]
 
for mtime, filename in list[:20]:
    entry = filename[:-4]
 
    file = open(filename)
    title = file.readline().strip()
    body = "".join(file.read())
    file.close()
 
    cmtPattern = re.compile(entry+"-.*\.cmt")
    comments = len([name for name in dir if cmtPattern.match(name)])
 
    Items.append({
      'entry':	entry,
      'title':	title,
      'link':	entry + '.html',
      'mtime':	mtime,
      'description': body,
      'comments': comments,
    })
 
data={'channel':channel, 'Items':Items}
print Template(open('template.html').read(), [data])
