#!/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. # Author: Stefan Ritter # Description: A simple blogging software 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') linklist = configuration.get('look', 'linklist') permalinks = configuration.get('look', 'permalinks') comments = configuration.get('look', 'comments') 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') allentries_display = action.getvalue('a') if not month_display: month_display = "" if not post_display: post_display = "" if not static_display: static_display = "" if not allentries_display: allentries_display = "" # 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("<", "<") cname = cname.replace(">", ">") cname = cname.replace("\"", """) ctext = ctext.replace("<", "<") ctext = ctext.replace(">", ">") ctext = ctext.replace("\"", """) # 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') 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") content.close() print 'Content-type: text/html\n' print '' print '' print ' ' print ' ' + blog_title + '' print ' ' print ' ' print ' ' print ' ' print ' ' print ' ' print '
' + blog_title + '
' 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 '
' print '
' if static_display != "": # Show Staticpage content = open(staticpages_dir + static_display, "r") print '
' + re.sub('\d+?-', '', static_display) + '
' print '

' for line in content: print ' ' + line.strip() + '
' print '

' 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: if allentries_display == "1" or entry_counter < entries_per_page: content = open(entry, "r") if permalinks: # Title as permalink... print ' ' else: # ... or not print '
' + title + ' (' + date + ')
' print '

' for line in content: print ' ' + line.strip() + '
' # Comments are shown when post_display and comments_file comments_file = glob.glob(entries_dir + title + '.comments') if post_display: if comments_file: print '


' 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 '
' print ' ' + line.split(".", 1)[1].strip() + ' wrote at ' elif line.split(".", 1)[0] == "+": print ' ' + line.split(".", 1)[1].strip() + ':
' else: line = line.split(".", 1)[1] print '   ' + line.strip() + '
' comments_content.close() # Form for adding comments print '


' print '
' print ' ' print ' ' print '
' print '
' print '
' if comments == "True": comments_file = glob.glob(entries_dir + title + '.comments') if not comments_file and not post_display: print '
' print ' ' print '

' 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 '
' comments_content.close() print '

' print '

' content.close() entry_counter += 1 if not month_display and not post_display and not allentries_display and entry_counter == entries_per_page: # Display pagelist print '
View all entries...
' print '
' print ' ' print '' # vim: set tw=0 ts=4: