#!/usr/bin/python2.2 """Pyllbox blog software, by Sean B. Palmer. License: GPL 2. Try http://miscoranda.com/pyllbox/ for more information.""" import sys, os, re, cgi, cgitb, time; cgitb.enable() from glob import glob title, by = '$${blogname}', '$${authorname}' site = 'http://%s/' % os.environ.get('SERVER_NAME', 'localhost') r_var = re.compile('\$\{([a-z]+)\}') template = lambda fn=None: r_var.sub('%(\g<1>)s', open(fn or 'main.t').read()) if os.environ.get('REQUEST_METHOD') == 'POST': date = time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime()) form = cgi.FieldStorage() form.__call__ = lambda s: form[s].value def serve(status, body): sys.stdout.write("Status: %s\r\n" % status) sys.stdout.write("Content-Type: text/html; charset=utf-8\r\n\r\n") sys.stdout.write(body) sys.exit() class Entry(dict): def __init__(self, fn): f = open("%s.txt" % fn) self['title'] = f.readline().rstrip() self['date'] = f.readline().rstrip() self['content'] = f.read() f.close() self['number'] = str(fn) self['comments'] = str(len(glob('./comments/%s-*' % self['number']))) def __str__(self): return template('entry.t') % self def mkentry(n): data = form('title') + '\n' + date + '\n\n' + form('text') + '\n' open('%s.txt' % n, 'w').write(data) build('*') and serve(200, "Made entry %s" % n) def rmentry(n): if os.path.isfile('%s.txt' % n) and n.isdigit(): os.remove('%s.txt' % n) build('*') and serve(200, 'Deleted entry %s' % n) else: serve(200, "Couldn't delete %s" % n) def homepage(): index = [int(fn[:-4]) for fn in glob('[0-9]*.txt') if fn[:-4].isdigit()] index.sort() if os.environ.get('REQUEST_METHOD') == 'GET': index.reverse() body = '\n\n'.join([str(Entry(i)) for i in index[:10]]) serve(200, template() % {'title': title + ' - by ' + by, 'body': body, 'blurb': open('blurb.txt').read()}) elif form.has_key('title'): mkentry((index or [0]).pop() + 1) else: build(form('build')) and serve(200, "Built %s" % form('build')) def entry(n): if os.environ.get('REQUEST_METHOD') == 'GET': if not os.path.exists("%s.txt" % n): serve(404, "Not found: %s" % n) e, nav = Entry(n), '' for (way, i) in (('Previous:', int(n)-1), ('Next:', int(n)+1)): if os.path.exists("%s.txt" % i): nav += '%s %s \n' % (way, i, Entry(i)['title']) info = {'title': title+': '+e['title'], 'body': str(e)+nav, 'blurb': ''} if os.path.isdir('comments'): cinfo = {'weblog': '.', 'comments': 'comments', 'path': str(n)} info['body'] += '\n\n' + template('./comments/form.t') % cinfo serve(200, template('main.t') % info) elif form.has_key('title'): mkentry(n) else: rmentry(n) def build(s): index = [int(fn[:-4]) for fn in glob('[0-9]*.txt') if fn[:-4].isdigit()] index.sort() index.reverse() assert s in ('*', 'archives', 'archives.html', 'index.rdf') or '-' in s if (s == '*') or (s in ('archives', 'archives.html')): info = {'body': '', 'title': '%s - archives' % title, 'blurb': ''} for i in index: # entries()[::-1]: entry = Entry(str(i)) info['body'] += '
  • %s' % (i, entry['title']) info['body'] += ' (%s)
  • \n' % entry['date'][:10] info['body'] = '' open('archives.html', 'w').write(template() % info) if (s == '*') or ('-' in s): if s == '*': month = date[:7] else: month = s[:] info = {'body': '', 'title': '%s - %s' % (title, month), 'blurb': ''} for i in index: # entries()[::-1]: entry = Entry(str(i)) if entry['date'].startswith(month): info['body'] += str(entry)+'\n' if info['body']: open(month+'.html', 'w').write(template() % info) if (s == '*') or (s == 'index.rdf'): path = os.environ.get('REQUEST_URI', '/').split('/') path = site + '/'.join(path[:-1])[1:] # @@ os.path stuff info = {'date': date, 'rdfseq': '', 'entries': ''} for i in index[:10]: # entries()[:-11:-1]: entry = Entry(str(i)) info['rdfseq'] += '\n' % (path, i) rinf = {'number': str(i), 'title': entry['title'], 'path': path, 'text': '', 'date': entry['date']} if os.path.isdir('comments'): rinf['text'] += '\n