blogthon/blogthon.cgi

231 lines
9.0 KiB
Plaintext
Raw Normal View History

2009-02-28 18:02:23 +01:00
#!/usr/bin/python
# This program is free software. It comes without any warranty, to
# the extent permitted by applicable law. You can redistribute it
# and/or modify it under the terms of the Do What The Fuck You Want
# To Public License, Version 2, as published by Sam Hocevar. See
# http://sam.zoy.org/wtfpl/COPYING for more details.
2009-03-01 13:55:09 +01:00
# Author: Stefan Ritter <xeno@thehappy.de>
# Description: A simple blogging software
2009-02-28 18:02:23 +01:00
import cgi
import os
import time
import glob
import re
import ConfigParser
configuration = ConfigParser.ConfigParser()
configuration.read('configuration')
blog_title = configuration.get('personal', 'blog_title')
keywords = configuration.get('personal', 'keywords')
entries_dir = configuration.get('personal', 'entries_dir')
entries_suffix = configuration.get('personal', 'entries_suffix')
staticpages_dir = configuration.get('personal', 'staticpages_dir')
style = configuration.get('look', 'style')
entries_per_page = configuration.getint('look', 'entries_per_page')
monthlist = configuration.get('look', 'monthlist')
staticpages = configuration.get('look', 'staticpages')
2009-03-02 17:06:22 +01:00
linklist = configuration.get('look', 'linklist')
2009-02-28 18:02:23 +01:00
permalinks = configuration.get('look', 'permalinks')
2009-03-03 07:28:29 +01:00
comments = configuration.get('look', 'comments')
2009-02-28 18:02:23 +01:00
newest_first = configuration.get('look', 'newest_first')
action = cgi.FieldStorage()
month_display = action.getvalue('m')
post_display = action.getvalue('p')
static_display = action.getvalue('s')
2009-03-02 01:50:14 +01:00
allentries_display = action.getvalue('a')
2009-02-28 18:02:23 +01:00
if not month_display: month_display = ""
if not post_display: post_display = ""
if not static_display: static_display = ""
2009-03-02 01:50:14 +01:00
if not allentries_display: allentries_display = ""
2009-02-28 18:02:23 +01:00
2009-03-08 19:17:59 +01:00
# Commentstuff
ctitle = action.getvalue('ctitle')
cname = action.getvalue('cname')
ctext = action.getvalue('ctext')
if not ctitle: ctitle = ""
if not cname: cname = ""
if not ctext: ctext = ""
# Comment to commit?
if cname and ctext and ctitle:
# Prevent XSS hacks
cname = cname.replace("<", "&lt;")
cname = cname.replace(">", "&gt;")
cname = cname.replace("\"", "&quot;")
ctext = ctext.replace("<", "&lt;")
ctext = ctext.replace(">", "&gt;")
ctext = ctext.replace("\"", "&quot;")
# Add comment
comments_file = glob.glob(entries_dir + ctitle + '.comments')
if not comments_file:
content = open(entries_dir + ctitle + '.comments', "w")
content.close()
comments_file = glob.glob(entries_dir + ctitle + '.comments')
2009-03-08 19:17:59 +01:00
content = open(comments_file[0], "a+")
content.write("-." + cname + "\n")
content.write("+." + time.asctime() + "\n")
ctext = ctext.split("\n")
for line in ctext:
content.write("." + line + "\n")
2009-03-08 19:17:59 +01:00
content.close()
2009-02-28 18:02:23 +01:00
print 'Content-type: text/html\n'
print '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"'
print ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'
print '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">'
print ' <head>'
print ' <title>' + blog_title + '</title>'
print ' <meta http-equiv="content-type" content="text/html; charset=utf-8" />'
print ' <meta name="keywords" content="' + keywords + '" />'
print ' <meta name="description" content="' + blog_title + '" />'
print ' <link rel="stylesheet" type="text/css" href="styles/' + style + '" />'
print ' </head>'
print ' <body>'
print ' <div class="title"><a href="?" class="title">' + blog_title + '</a></div>'
entries = []
entries_list = glob.glob(entries_dir + '*.' + entries_suffix)
for entry in entries_list:
timestamp = os.stat(entry)
timestamp = time.localtime(timestamp[8])
entry = timestamp, entry
entries.append(entry)
if newest_first:
entries.sort(reverse=True)
else:
entries.sort()
print ' <div class="screen"><div class="sidebar">'
2009-02-28 18:02:23 +01:00
if staticpages == "True":
2009-02-28 18:02:23 +01:00
staticpages = []
staticpages_list = glob.glob(staticpages_dir + '*')
staticpages_list.sort()
2009-03-02 17:06:22 +01:00
print ' <div class="sidebarentry">'
print ' <small>pages</small><br />'
2009-02-28 18:02:23 +01:00
for staticpage in staticpages_list:
title = re.sub('\w+?\/\d+?-', '', staticpage)
link = re.sub('\w+?\/', '', staticpage)
print ' &nbsp;<a href="?s=' + link + '">' + title + '</a>&nbsp;<br />'
if monthlist == "True": print ' <br />'
2009-02-28 18:02:23 +01:00
print ' </div>'
if monthlist == "True":
2009-02-28 18:02:23 +01:00
olddate = ""
2009-03-02 17:06:22 +01:00
print ' <div class="sidebarentry">'
print ' <small>months</small><br />'
2009-02-28 18:02:23 +01:00
for entry in entries:
date = time.strftime("%m%Y", entry[0])
date_display = time.strftime("%h %Y", entry[0])
if not olddate == date:
2009-03-02 17:06:22 +01:00
print ' &nbsp;<a href="?m=' + date + '">' + date_display + '</a>&nbsp;<br />'
2009-02-28 18:02:23 +01:00
olddate = date
if linklist == "True": print ' <br />'
2009-02-28 18:02:23 +01:00
print ' </div>'
if linklist == "True":
2009-03-02 17:06:22 +01:00
content = open("linklist", "r")
print ' <div class="sidebarentry">'
print ' <small>links</small><br />'
for line in content:
2009-03-03 07:28:29 +01:00
print ' &nbsp;<a href=' + line.split(" ")[0] + '>' + line.split(" ", 1)[1].strip() + '</a>&nbsp;<br />'
2009-03-02 17:06:22 +01:00
print ' </div>'
content.close()
2009-02-28 18:02:23 +01:00
print ' </div>'
print ' <div class="content">'
if static_display != "": # Show Staticpage
content = open(staticpages_dir + static_display, "r")
print ' <div class="entrytitle">' + re.sub('\d+?-', '', static_display) + '</div>'
print ' <div class="entry"><p>'
for line in content:
print ' ' + line.strip() + '<br />'
print ' </p></div>'
content.close()
else: # Show regular entry
entry_counter = 0
for entry in entries:
date = time.strftime("%c", entry[0])
date_to_compare = time.strftime("%m%Y", entry[0]) # Needed for permalinks
entry = entry[1]
title = entry.replace('entries/', '', 1)
title = title.replace('.txt', '')
if month_display == date_to_compare or not month_display:
if post_display == title or not post_display:
2009-03-02 01:50:14 +01:00
if allentries_display == "1" or entry_counter < entries_per_page:
2009-02-28 18:02:23 +01:00
content = open(entry, "r")
if permalinks: # Title as permalink...
2009-02-28 18:02:23 +01:00
print ' <div class="entrytitle"><a href="?p=' + title + '" class="entrytitle">' + title + ' <small>(' + date + ')</small></a></div>'
else: # ... or not
2009-02-28 18:02:23 +01:00
print ' <div class="entrytitle">' + title + ' <small>(' + date + ')</small></div>'
print ' <div class="entry"><p>'
for line in content:
print ' ' + line.strip() + '<br />'
# Comments are shown when post_display and comments_file
comments_file = glob.glob(entries_dir + title + '.comments')
if post_display:
if comments_file:
print ' <br /><hr>'
comments_file = glob.glob(entries_dir + title + '.comments')
comments_content = open(comments_file[0], "r")
for line in comments_content:
if line.split(".", 1)[0] == "-":
print ' <br />'
print ' <b><i>' + line.split(".", 1)[1].strip() + '</i><small> wrote at '
elif line.split(".", 1)[0] == "+":
print ' ' + line.split(".", 1)[1].strip() + ':</small></b><br />'
else:
line = line.split(".", 1)[1]
print ' &nbsp;&nbsp;' + line.strip() + '<br />'
comments_content.close()
# Form for adding comments
print ' <br /><br /><br />'
2009-03-08 19:17:59 +01:00
print ' <form method="post">'
print ' <input type="hidden" name="ctitle" value="' + title + '">'
print ' <label for="cname">name:</label><input type="text" id="cname" name="cname">'
print ' <br /><label for="ctext">text:</label><textarea rows="5" id="ctext" name="ctext"></textarea>'
print ' <br /><input type="submit" id="submit" value="post comment">'
print ' </form>'
2009-03-03 07:28:29 +01:00
if comments == "True":
comments_file = glob.glob(entries_dir + title + '.comments')
if not comments_file and not post_display:
print ' <div class="comment">'
print ' <ul><li><a href="?p=' + title + '" class="comment">no comments</a></li></ul>'
print ' </div><br />'
elif comments_file and not post_display:
comments_content = open(comments_file[0], "r")
comments_counter = 0
for line in comments_content:
if line.split(".", 1)[0] == "-": comments_counter += 1
print ' <div class="comment">'
print ' <ul><li><a href="?p=' + title + '" class="comment">comments (' + str(comments_counter) + ')</a></li></ul>'
print ' </div><br />'
comments_content.close()
2009-02-28 18:02:23 +01:00
print ' </p></div>'
print ' <br /><br />'
content.close()
2009-03-02 01:50:14 +01:00
entry_counter += 1
2009-02-28 18:02:23 +01:00
2009-03-02 01:50:14 +01:00
if not month_display and not post_display and not allentries_display and entry_counter == entries_per_page: # Display pagelist
print ' <div class="entry"><a href=?a=1>View all entries...</a></div>'
2009-02-28 18:02:23 +01:00
print ' </div></div>'
2009-02-28 18:02:23 +01:00
print ' </body>'
print '</html>'
# vim: set tw=0 ts=4: