aboutsummaryrefslogtreecommitdiffstats
path: root/blogthon.cgi
diff options
context:
space:
mode:
authorStefan Ritter <xeno@thehappy.de>2010-12-16 11:11:41 +0100
committerStefan Ritter <xeno@thehappy.de>2010-12-16 11:11:41 +0100
commitb32a982b81bcd1f1d3c41414e84ed5cb505d15e8 (patch)
tree4482bd1a2774a3313bed6b0da3cdc3d7aa9c9664 /blogthon.cgi
parentfad60904312f4be9de6045c01ff08aca982c1a01 (diff)
Code cleanup:
* Fix blog_url if there is no http:// at the beginning or/and no / at the end * Use %s for string concatenation (improving performance)
Diffstat (limited to '')
-rwxr-xr-xblogthon.cgi114
1 files changed, 60 insertions, 54 deletions
diff --git a/blogthon.cgi b/blogthon.cgi
index 56a9335..bcb0db1 100755
--- a/blogthon.cgi
+++ b/blogthon.cgi
@@ -106,14 +106,20 @@ try:
except ConfigParser.Error, error:
errorpage(str(error))
+if not re.match("^http:\/\/.*$", blog_url):
+ blog_url = "http://" + blog_url
+
+if not re.match("^.*\/$", blog_url):
+ blog_url = blog_url + "/"
+
if not os.path.exists(entries_dir):
- errorpage("Directory \"" + entries_dir + "\" does not exist!")
+ errorpage("Directory \"%s\" does not exist!" % entries_dir)
if not os.path.exists(staticpages_dir):
- errorpage("Directory \"" + staticpages_dir + "\" does not exist!")
+ errorpage("Directory \"%s\" does not exist!" % staticpages_dir)
if not os.path.exists(plugins_dir):
- errorpage("Directory \"" + plugins_dir + "\" does not exist!")
+ errorpage("Directory \"%s\" does not exist!" % plugins_dir)
if not os.path.exists("linklist"):
errorpage("File \"linklist\" does not exist!")
@@ -206,7 +212,7 @@ if cname and ctext and ctitle:
content = open(comments_file, "w")
content.close()
except:
- errorpage("\"" + entries_dir + "\" isn\'t writable!")
+ errorpage("\"%s\" isn\'t writable!" % entries_dir)
try:
content = open(comments_file, "a")
@@ -219,7 +225,7 @@ if cname and ctext and ctitle:
# Send mail?
if not new_comment_mail == "False":
- msg = "From: Blogthon\nTo: " + mail_to + "\nSubject: New comment on " + blog_title + "\n\nSomeone wrote a comment to this entry: " + blog_url + "?p=" + ctitle.replace(" ", "-")
+ msg = "From: Blogthon\nTo: %s\nSubject: New comment on %s\n\nSomeone wrote a comment to this entry: %s?p=%s" % (mail_to, blog_title, blog_url, ctitle.replace(" ", "-"))
smtp = SMTP(smtp_host)
smtp.starttls()
smtp.sendmail(blog_title, mail_to, msg)
@@ -267,13 +273,13 @@ if feed_display == "atom":
sec = "%(#)02d" % {"#": int(date[5])}
document_header("atom")
- print "<link href=\"" + blog_url + "/?feed=atom\" rel=\"self\" type=\"application/atom+xml\"/>"
+ print "<link href=\"%s?feed=atom\" rel=\"self\" type=\"application/atom+xml\"/>" % blog_url
print tab + "<author>"
- print tab*2 + "<name>" + blog_title + "</name>"
+ print tab*2 + "<name>%s</name>" % blog_title
print tab + "</author>"
- print tab + "<title>" + blog_title + "</title>"
- print tab + "<id>urn:uuid:" + blog_title_md5sum + "</id>"
- print tab + "<updated>" + str(date[0]) + "-" + month + "-" + day + "T" + hour + ":" + min + ":" + sec + "Z</updated>"
+ print tab + "<title>%s</title>" % blog_title
+ print tab + "<id>urn:uuid:%s</id>" % blog_title_md5sum
+ print tab + "<updated>%s-%s-%sT%s:%s:%sZ</updated>" % (str(date[0]), month, day, hour, min, sec)
print ""
j = len(entries)
if j > 10: j = 10
@@ -282,10 +288,10 @@ if feed_display == "atom":
date = entries[i][0]
title_md5sum = generate_uuid(title)
print tab*2 + "<entry>"
- print tab*3 + "<title>" + title + "</title>"
- print tab*3 + "<link href=\"" + blog_url + "?p=" + title + "\"/>"
- print tab*3 + "<id>urn:uuid:" + title_md5sum + "</id>"
- print tab*3 + "<updated>" + str(date[0]) + "-" + month + "-" + day + "T" + hour + ":" + min + ":" + sec + "Z</updated>"
+ print tab*3 + "<title>%s</title>" % title
+ print tab*3 + "<link href=\"%s?p=%s\"/>" % (blog_url, title)
+ print tab*3 + "<id>urn:uuid:%s</id>" % title_md5sum
+ print tab*3 + "<updated>%s-%s-%sT%s:%s:%sZ</updated>" % (str(date[0]), month, day, hour, min, sec)
print tab*3 + "<summary>"
content = open(str(entries[i][1]), "r")
for h in xrange(0, int(feed_preview)):
@@ -301,11 +307,11 @@ if feed_display == "atom":
elif feed_display == "rss":
document_header("rss")
print tab + "<channel>"
- print tab*2 + "<title>" + blog_title + "</title>"
- print tab*2 + "<link>" + blog_url + "</link>"
- print tab*2 + "<description>" + blog_subtitle + "</description>"
+ print tab*2 + "<title>%s</title>" % blog_title
+ print tab*2 + "<link>%s</link>" % blog_url
+ print tab*2 + "<description>%s</description>" % blog_subtitle
date = time.strftime("%a, %d %b %Y %H:%M:%S", time.gmtime(time.mktime(entries[0][0])))
- print tab*2 + "<pubDate>" + date + "</pubDate>"
+ print tab*2 + "<pubDate>%s</pubDate>" % date
print ""
j = len(entries)
if j > 10: j = 10
@@ -313,18 +319,18 @@ elif feed_display == "rss":
title = str(entries[i][1]).replace(entries_dir, "", 1).replace("." + entries_suffix, "")
date = time.strftime("%a, %d %b %Y %H:%M:%S %z", time.gmtime(time.mktime(entries[i][0])))
print tab*2 + "<item>"
- print tab*3 + "<title>" + title + "</title>"
- print tab*3 + "<link>" + blog_url + "?p=" + title + "</link>"
- print tab*3 + "<guid>" + blog_url + "?p=" + title + "</guid>"
- print tab*3 + "<pubDate>" + date + "</pubDate>"
+ print tab*3 + "<title>%s</title>" % title
+ print tab*3 + "<link>%s?p=%s</link>" % (blog_url, title)
+ print tab*3 + "<guid>%s?p=%s</guid>" % (blog_url, title)
+ print tab*3 + "<pubDate>%s</pubDate>" % date
content = open(str(entries[i][1]), "r")
rss_description= ""
for h in xrange(0, int(feed_preview)):
line = content.readline().strip()
if line:
- rss_description = rss_description + line + "<br />"
+ rss_description = "%s%s<br />" % (rss_description, line)
content.close()
- print tab*3 + "<description><![CDATA[" + rss_description + "]]></description>"
+ print tab*3 + "<description><![CDATA[%s]]></description>" % rss_description
print tab*2 + "</item>"
print tab + "</channel>"
print "</rss>"
@@ -333,11 +339,11 @@ elif feed_display == "rss":
else:
document_header("xhtml-strict")
print tab + "<head>"
- print tab*2 + "<title>" + blog_title + "</title>"
+ print tab*2 + "<title>%s</title>" % blog_title
print tab*2 + "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />"
- print tab*2 + "<meta name=\"keywords\" content=\"" + keywords + "\" />"
- print tab*2 + "<meta name=\"description\" content=\"" + blog_title + "\" />"
- print tab*2 + "<link rel=\"stylesheet\" type=\"text/css\" href=\"styles/" + style + "/" + style + ".css\" />"
+ print tab*2 + "<meta name=\"keywords\" content=\"%s\" />" % keywords
+ print tab*2 + "<meta name=\"description\" content=\"%s\" />" % blog_title
+ print tab*2 + "<link rel=\"stylesheet\" type=\"text/css\" href=\"styles/%s/%s.css\" />" % (style, style)
print tab + "</head>"
print tab + "<body>"
print ""
@@ -350,10 +356,10 @@ else:
# Site header
print tab*2 + "<div class=\"header\">"
print tab*3 + "<div class=\"header_title\">"
- print tab*4 + "<a href=\"?\" class=\"header_link\">" + blog_title + "</a>"
+ print tab*4 + "<a href=\"?\" class=\"header_link\">%s</a>" % blog_title
print tab*3 + "</div>"
print tab*3 + "<div class=\"header_subtitle\">"
- print tab*4 + "<span class=\"header_subtitle\">" + blog_subtitle + "</span>"
+ print tab*4 + "<span class=\"header_subtitle\">%s</span>" % blog_subtitle
print tab*3 + "</div>"
print tab*2 + "</div>"
print ""
@@ -376,7 +382,7 @@ else:
staticpages_list = glob(os.path.join(staticpages_dir, "*"))
staticpages_list.sort()
print tab*2 + "<div class=\"pages\">"
- print tab*3 + "<div class=\"pages_title\">" + blog_locale[0] + "</div>"
+ print tab*3 + "<div class=\"pages_title\">%s</div>" % blog_locale[0]
print tab*3 + "<div class=\"pages_list\">"
print tab*4 + "<ul class=\"pages_list\">"
for staticpage in staticpages_list:
@@ -386,10 +392,10 @@ else:
link = header.split(":", 1)[1].strip()
else:
link = re.sub("\w+?\/", "", staticpage)
- link = "?s=" + link
+ link = "?s=%s" % link
file.close()
title = re.sub("\w+?\/\d+?-", "", staticpage)
- print tab*5 + "<li class=\"pages_list_entry\"><a href=\"" + link + "\" class=\"pages_list_entry\">" + title + "</a></li>"
+ print tab*5 + "<li class=\"pages_list_entry\"><a href=\"%s\" class=\"pages_list_entry\">%s</a></li>" % (link, title)
print tab*4 + "</ul>"
print tab*3 + "</div>"
print tab*3 + "<div class=\"pages_footer\"></div>"
@@ -400,14 +406,14 @@ else:
if monthlist == "True":
olddate = ""
print tab*2 + "<div class=\"months\">"
- print tab*3 + "<div class=\"months_title\">" + blog_locale[1] + "</div>"
+ print tab*3 + "<div class=\"months_title\">%s</div>" % blog_locale[1]
print tab*3 + "<div class=\"months_list\">"
print tab*4 + "<ul class=\"months_list\">"
for entry in entries:
- date = time.strftime("%m%Y", entry[0])
+ date = time.strftime("%m%Y", entry[0])
date_display = time.strftime("%h %Y", entry[0])
if not olddate == date:
- print tab*5 + "<li class=\"months_list_entry\"><a href=\"?m=" + date + "\" class=\"months_list_entry\">" + date_display + "</a></li>"
+ print tab*5 + "<li class=\"months_list_entry\"><a href=\"?m=%s\" class=\"months_list_entry\">%s</a></li>" % (date, date_display)
olddate = date
print tab*4 + "</ul>"
print tab*3 + "</div>"
@@ -418,7 +424,7 @@ else:
# Linklist
if linklist == "True":
print tab*2 + "<div class=\"linklist\">"
- print tab*3 + "<div class=\"linklist_title\">" + blog_locale[2] + "</div>"
+ print tab*3 + "<div class=\"linklist_title\">%s</div>" % blog_locale[2]
print tab*3 + "<div class=\"linklist_list\">"
print tab*4 + "<ul class=\"linklist_list\">"
try:
@@ -427,7 +433,7 @@ else:
if line.strip() is "":
print "<br />"
else:
- print tab*5 + "<li class=\"linklist_list_entry\"><a href=\"" + line.split(" ")[0] + "\" class=\"months_list_entry\">" + line.split(" ", 1)[1].strip() + "</a></li>"
+ print tab*5 + "<li class=\"linklist_list_entry\"><a href=\"%s\" class=\"months_list_entry\">%s</a></li>" % (line.split(" ")[0], line.split(" ", 1)[1].strip())
content.close()
except:
print ""
@@ -444,7 +450,7 @@ else:
if static_display != "":
content = open(os.path.join(staticpages_dir, static_display), "r")
print tab*3 + "<div class=\"entry\">"
- print tab*4 + "<div class=\"entry_title\">" + re.sub("^\.", "", re.sub("\d+?-", "", static_display)) + "</div>"
+ print tab*4 + "<div class=\"entry_title\">%s</div>" % re.sub("^\.", "", re.sub("\d+?-", "", static_display))
print tab*4 + "<div class=\"entry_content\">"
print tab*5 + "<p>"
for line in content:
@@ -484,10 +490,10 @@ else:
content = open(entry, "r")
print tab*3 + "<div class=\"entry\">"
if permalinks:
- print tab*4 + "<div class=\"entry_title\"><a href=\"?p=" + title.replace(" ", "-") + "\" class=\"entry_title\">" + title + "</a></div>"
+ print tab*4 + "<div class=\"entry_title\"><a href=\"?p=%s\" class=\"entry_title\">%s</a></div>" % (title.replace(" ", "-"), title)
else:
- print tab*4 + "<div class=\"entry_title\">" + title + "</div>"
- print tab*4 + "<div class=\"entry_date\">" + date + "</div>"
+ print tab*4 + "<div class=\"entry_title\">%s</div>" % title
+ print tab*4 + "<div class=\"entry_date\">%s</div>" % date
print tab*4 + "<div class=\"entry_content\">"
for line in content:
if no_break.match(line):
@@ -525,11 +531,11 @@ else:
# Label for each comment
label_count += 1
- print tab*4 + "<a name=\"" + str(label_count) + "\"></a>"
+ print tab*4 + "<a name=\"%s\"></a>" % str(label_count)
- print tab*4 + "<div class=\"comment_author\">" + line.split(".", 1)[1].strip() + "</div>"
+ print tab*4 + "<div class=\"comment_author\">%s</div>" % line.split(".", 1)[1].strip()
elif line_start_plus.match(line):
- print tab*4 + "<div class=\"comment_date\">" + line.split(".", 1)[1].strip() + "</div>"
+ print tab*4 + "<div class=\"comment_date\">%s</div>" % line.split(".", 1)[1].strip()
print tab*4 + "<div class=\"comment_content\">"
else:
notfirstline = 1
@@ -551,12 +557,12 @@ else:
cquizv = random_int_a + random_int_b
print tab*3 + "<div class=\"submit_comment\">"
print tab*4 + "<form action=\"\" method=\"post\">"
- print tab*5 + "<input type=\"hidden\" name=\"ctitle\" value=\"" + title + "\" />"
- print tab*5 + "<input type=\"hidden\" name=\"cquizv\" value=\"" + str(cquizv) + "\" />"
- print tab*5 + "<label class=\"submit_comment_name\">" + blog_locale[6] + ":</label><input class=\"submit_comment_name_input\" type=\"text\" id=\"cname\" name=\"cname\" />"
- print tab*5 + "<br /><label class=\"submit_comment_text\">" + blog_locale[7] + ":</label><textarea class=\"submit_comment_textarea\" id=\"ctext\" name=\"ctext\"></textarea>"
- print tab*5 + "<br /><label class=\"submit_comment_quiz\">" + str(random_int_a) + "+" + str(random_int_b) + "=</label><input class=\"submit_comment_quiz_input\" type=\"text\" id=\"cquiz\" name=\"cquiz\" />"
- print tab*5 + "<br /><input class=\"submit_comment_button\" type=\"submit\" id=\"submit\" value=\"" + blog_locale[8] + "\" />"
+ print tab*5 + "<input type=\"hidden\" name=\"ctitle\" value=\"%s\" />" % title
+ print tab*5 + "<input type=\"hidden\" name=\"cquizv\" value=\"%s\" />" % str(cquizv)
+ print tab*5 + "<label class=\"submit_comment_name\">%s:</label><input class=\"submit_comment_name_input\" type=\"text\" id=\"cname\" name=\"cname\" />" % blog_locale[6]
+ print tab*5 + "<br /><label class=\"submit_comment_text\">%s:</label><textarea class=\"submit_comment_textarea\" id=\"ctext\" name=\"ctext\"></textarea>" % blog_locale[7]
+ print tab*5 + "<br /><label class=\"submit_comment_quiz\">%s+%s=</label><input class=\"submit_comment_quiz_input\" type=\"text\" id=\"cquiz\" name=\"cquiz\" />" % (str(random_int_a), str(random_int_b))
+ print tab*5 + "<br /><input class=\"submit_comment_button\" type=\"submit\" id=\"submit\" value=\"%s\" />" % blog_locale[8]
print tab*4 + "</form>"
print tab*3 + "</div>"
else:
@@ -567,7 +573,7 @@ else:
comments_file = glob(os.path.join(entries_dir, title + ".comments"))
if not comments_file and not post_display:
print tab*4 + "<div class=\"entry_comment\">"
- print tab*5 + "<a href=\"?p=" + title.replace(" " ,"-") + "\" class=\"entry_comment\">" + blog_locale[3] + "</a>"
+ print tab*5 + "<a href=\"?p=%s\" class=\"entry_comment\">%s</a>" % (title.replace(" ", "-"), blog_locale[3])
print tab*4 + "</div>"
print tab*3 + "</div>"
print ""
@@ -577,7 +583,7 @@ else:
for line in comments_content:
if line.split(".", 1)[0] == "-": comments_counter += 1
print tab*4 + "<div class=\"entry_comment\">"
- print tab*5 + "<a href=\"?p=" + title.replace(" ", "-") + "\" class=\"entry_comment\">" + blog_locale[4] + " (" + str(comments_counter) + ")</a>"
+ print tab*5 + "<a href=\"?p=%s\" class=\"entry_comment\">%s (%s)</a>" % (title.replace(" ", "-"), blog_locale[4], str(comments_counter))
print tab*4 + "</div>"
print tab*3 + "</div>"
print ""
@@ -590,7 +596,7 @@ else:
entry_counter += 1
if not month_display and not post_display and not allentries_display and entry_counter == entries_per_page: # Display pagelist
- print tab*3 + "<div class=\"entry\"><a href=\"?a=1\">" + blog_locale[5] + "</a></div>"
+ print tab*3 + "<div class=\"entry\"><a href=\"?a=1\">%s</a></div>" % blog_locale[5]
print tab*2 + "</div>"
print ""