From 31b7b41d65dc0b94bb07e90b006c67c7f4f411ea Mon Sep 17 00:00:00 2001 From: Stefan Ritter Date: Wed, 2 Dec 2009 12:19:30 +0100 Subject: Code cleanup: * Removed some regexp * Using String Formatting Operation to fill 1byte string with '0' --- blogthon.cgi | 42 ++++++++++++++++++------------------------ 1 file changed, 18 insertions(+), 24 deletions(-) diff --git a/blogthon.cgi b/blogthon.cgi index 8533183..614a482 100755 --- a/blogthon.cgi +++ b/blogthon.cgi @@ -125,8 +125,8 @@ if static_display: static_display = re.sub('/', '', static_display) post_display = action.getvalue('p') if post_display: - post_display = re.sub(' ', '-', post_display) - post_display = re.sub('/', '', post_display) + post_display = post_display.replace(' ', '-') \ + .replace('/', '') allentries_display = action.getvalue('a') feed_display = action.getvalue('feed') @@ -151,12 +151,12 @@ if not cquizv: cquizv = "" # Comment to commit? if cname and ctext and ctitle: # Prevent XSS hacks - cname = cname.replace("<", "<") \ - .replace(">", ">") \ - .replace("\"", """) - ctext = ctext.replace("<", "<") \ - .replace(">", ">") \ - .replace("\"", """) + cname = cname.replace('<', '<') \ + .replace('>', '>') \ + .replace('\'', '"') + ctext = ctext.replace('<', '<') \ + .replace('>', '>') \ + .replace('\'', '"') # Add comment if not cquiz == cquizv: @@ -169,7 +169,6 @@ if cname and ctext and ctitle: content.close() except: errorpage(entries_dir, 'isn\'t writable!') - comments_file = glob.glob(entries_dir + ctitle + '.comments') try: content = open(comments_file[0], "a+") content.write("-." + cname + "\n") @@ -209,17 +208,12 @@ if feed_display == "atom": blog_title_md5sum = generate_uuid(blog_title) title_md5sum = generate_uuid(title) - # Atom needs a 2byte string - month = str(date[1]) - day = str(date[2]) - hour = str(date[3]) - min = str(date[4]) - sec = str(date[5]) - if len(str(date[1])) == 1: month = '0' + str(date[1]) - if len(str(date[2])) == 1: day = '0' + str(date[2]) - if len(str(date[3])) == 1: hour = '0' + str(date[3]) - if len(str(date[4])) == 1: min = '0' + str(date[4]) - if len(str(date[5])) == 1: sec = '0' + str(date[5]) + # Append 0 to the beginning if len of integer is 1 (value<10) + month = '%(#)02d' % {'#': int(date[1])} + day = '%(#)02d' % {'#': int(date[2])} + hour = '%(#)02d' % {'#':int(date[3])} + min = '%(#)02d' % {'#': int(date[4])} + sec = '%(#)02d' % {'#': int(date[5])} document_header("atom") print '' @@ -260,11 +254,11 @@ elif feed_display == "rss": print ' ' print ' ' print '' + print date # Generate regular page else: document_header("xhtml-strict") - # XHTML Header print ' ' print ' ' + blog_title + '' print ' ' @@ -403,7 +397,7 @@ else: title = title.replace('.' + entries_suffix, '') if month_display == date_to_compare or not month_display: - if post_display == re.sub(' ', '-', title) or not post_display: + if post_display == post_display.replace(' ', '-') or not post_display: if allentries_display == "1" or entry_counter < entries_per_page: content = open(entry, "r") print '
' @@ -492,7 +486,7 @@ else: comments_file = glob.glob(entries_dir + title + '.comments') if not comments_file and not post_display: print '
' - print ' no comments' + print ' no comments' print '
' print '
' print '' @@ -502,7 +496,7 @@ else: for line in comments_content: if line.split(".", 1)[0] == "-": comments_counter += 1 print '
' - print ' comments (' + str(comments_counter) + ')' + print ' comments (' + str(comments_counter) + ')' print '
' print ' ' print '' -- cgit v1.2.3