#!/usr/bin/env python
#coding: utf8
# blitiri - A single-file blog engine.
# Alberto Bertogli (albertito@gmail.com)
#
# Configuration section
#
# You can edit these values, or create a file named "config.py" and put them
# there to make updating easier. The ones in config.py take precedence.
#
# Directory where entries are stored
data_path = "/tmp/blog/data"
# Path where templates are stored. Use an empty string for the built-in
# default templates. If they're not found, the built-in ones will be used.
templates_path = "/tmp/blog/templates"
# URL to the blog, including the name. Can be a full URL or just the path.
blog_url = "/blog/blitiri.cgi"
# Style sheet (CSS) URL. Can be relative or absolute. To use the built-in
# default, set it to blog_url + "/style".
css_url = blog_url + "/style"
# Blog title
title = "I don't like blogs"
# Default author
author = "Hartmut Kegan"
# Article encoding
encoding = "utf8"
#
# End of configuration
# DO *NOT* EDIT ANYTHING PAST HERE
#
import sys
import os
import time
import datetime
import calendar
import zlib
import urllib
import cgi
from docutils.core import publish_parts
# Before importing the config, add our cwd to the Python path
sys.path.append(os.getcwd())
# Load the config file, if there is one
try:
from config import *
except:
pass
# Pimp *_path config variables to support relative paths
data_path = os.path.realpath(data_path)
templates_path = os.path.realpath(templates_path)
# Default template
default_main_header = """\
""" % vars
print ""
def render_style():
print 'Content-type: text/css\r\n\r\n',
print default_css
def handle_cgi():
import cgitb; cgitb.enable()
form = cgi.FieldStorage()
year = int(form.getfirst("year", 0))
month = int(form.getfirst("month", 0))
day = int(form.getfirst("day", 0))
tags = set(form.getlist("tag"))
uuid = None
atom = False
style = False
post = False
artlist = False
if os.environ.has_key('PATH_INFO'):
path_info = os.environ['PATH_INFO']
style = path_info == '/style'
atom = path_info == '/atom'
tag = path_info.startswith('/tag/')
post = path_info.startswith('/post/')
artlist = path_info.startswith('/list')
if not style and not atom and not post and not tag \
and not artlist:
date = path_info.split('/')[1:]
try:
if len(date) > 1 and date[0]:
year = int(date[0])
if len(date) > 2 and date[1]:
month = int(date[1])
if len(date) > 3 and date[2]:
day = int(date[2])
except ValueError:
pass
elif post:
uuid = path_info.replace('/post/', '')
uuid = uuid.replace('/', '')
elif tag:
t = path_info.replace('/tag/', '')
t = t.replace('/', '')
t = urllib.unquote_plus(t)
tags = set((t,))
db = DB(data_path + '/db')
if atom:
articles = db.get_articles(tags = tags)
articles.sort(reverse = True)
render_atom(articles[:10])
elif style:
render_style()
elif post:
render_html( [db.get_article(uuid)], db, year )
elif artlist:
articles = db.get_articles()
articles.sort(cmp = Article.title_cmp)
render_artlist(articles, db)
else:
articles = db.get_articles(year, month, day, tags)
articles.sort(reverse = True)
if not year and not month and not day and not tags:
articles = articles[:10]
render_html(articles, db, year)
def usage():
print 'Usage: %s {add|rm|update} article_path' % sys.argv[0]
def handle_cmd():
if len(sys.argv) != 3:
usage()
return 1
cmd = sys.argv[1]
art_path = os.path.realpath(sys.argv[2])
if os.path.commonprefix([data_path, art_path]) != data_path:
print "Error: article (%s) must be inside data_path (%s)" % \
(art_path, data_path)
return 1
art_path = art_path[len(data_path):]
if not os.path.isfile(data_path + '/db'):
open(data_path + '/db', 'w').write('')
db = DB(data_path + '/db')
if cmd == 'add':
article = Article(art_path)
for a in db.articles:
if a == article:
print 'Error: article already exists'
return 1
db.articles.append(article)
article.created = datetime.datetime.now()
article.updated = datetime.datetime.now()
db.save()
elif cmd == 'rm':
article = Article(art_path)
for a in db.articles:
if a == article:
break
else:
print "Error: no such article"
return 1
db.articles.remove(a)
db.save()
elif cmd == 'update':
article = Article(art_path)
for a in db.articles:
if a == article:
break
else:
print "Error: no such article"
return 1
a.updated = datetime.datetime.now()
db.save()
else:
usage()
return 1
return 0
if os.environ.has_key('GATEWAY_INTERFACE'):
handle_cgi()
else:
sys.exit(handle_cmd())