From f676c2da59fbf1cad3ca4c67437ebb18e6cc8c5e Mon Sep 17 00:00:00 2001 From: Stefan Ritter Date: Thu, 19 May 2011 11:36:00 +0200 Subject: utf8 fix --- blogthon.cgi | 53 ++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 42 insertions(+), 11 deletions(-) diff --git a/blogthon.cgi b/blogthon.cgi index 963f120..96dcbe3 100755 --- a/blogthon.cgi +++ b/blogthon.cgi @@ -23,8 +23,9 @@ import codecs # print() will output ascii, but we want utf-8 (python3) try: sys.stdout = codecs.getwriter('utf8')(sys.stdout.buffer) + utf8 = True except: - None + utf8 = False # Backward compatibilty to python2 try: @@ -227,10 +228,16 @@ if cname and ctext and ctitle: else: comments_file = os.path.join(entries_dir, ctitle + ".comments") if not os.path.exists(comments_file): - content = open(comments_file, "w", encoding="utf8") + if utf8: + content = open(comments_file, "w", encoding="utf8") + else: + content = open(comments_file, "w") content.close() - content = open(comments_file, "a", encoding="utf8") + if utf8: + content = open(comments_file, "a", encoding="utf8") + else: + content = open(comments_file, "a") content.write("-." + cname + "\n") content.write("+." + time.strftime("%c", time.localtime()) + "\n") ctext = ctext.split("\n") @@ -306,7 +313,10 @@ if feed_display == "atom": print(ind*3 + "urn:uuid:%s" % title_md5sum) print(ind*3 + "%s-%s-%sT%s:%s:%sZ" % (str(date[0]), month, day, hour, min, sec)) print(ind*3 + "") - content = open(str(entries[i][1]), "r", encoding="utf8") + if utf8: + content = open(str(entries[i][1]), "r", encoding="utf8") + else: + content = open(str(entries[i][1]), "r") for h in range(0, int(feed_preview)): rss_line = content.readline().strip() if rss_line != "": @@ -336,7 +346,10 @@ elif feed_display == "rss": print(ind*3 + "%s?p=%s" % (blog_url, title)) print(ind*3 + "%s?p=%s" % (blog_url, title)) print(ind*3 + "%s" % date) - content = open(str(entries[i][1]), "r", encoding="utf8") + if utf8: + content = open(str(entries[i][1]), "r", encoding="utf8") + else: + content = open(str(entries[i][1]), "r") rss_description= "" for h in range(0, int(feed_preview)): line = content.readline().strip() @@ -400,7 +413,10 @@ else: print(ind*3 + "
") print(ind*4 + "
    ") for staticpage in staticpages_list: - file = open(staticpage, "r", encoding="utf8") + if utf8: + file = open(staticpage, "r", encoding="utf8") + else: + file = open(staticpage, "r") header = file.readline() if header.split(":", 1)[0] == "extern_link": link = header.split(":", 1)[1].strip() @@ -442,7 +458,10 @@ else: print(ind*3 + "