Read posts with unicode instead binary

This commit is contained in:
Gregory Soutade 2016-03-19 17:03:43 +01:00
parent 97176ed454
commit 8bf132976d
2 changed files with 28 additions and 6 deletions

View File

@ -25,6 +25,7 @@ import xml
from xml.parsers.expat import *
import xml.parsers.expat
from xml.dom.minidom import parse, parseString
import codecs
from dynastie.generators.generator import DynastieGenerator, StrictUTF8Writer
from django.db import models
from dynastie.generators import markdown2
@ -202,7 +203,10 @@ class Index(DynastieGenerator):
break
try:
dom = parseString(code[start:end+11])
try:
dom = parseString(code[start:end+11])
except UnicodeEncodeError:
dom = parseString(code[start:end+11].encode('utf-8'))
except xml.dom.DOMException as e:
self.addError('Error parsing ' + self.filename)
break
@ -210,7 +214,6 @@ class Index(DynastieGenerator):
res = self.createCode(dom, dom.firstChild)
if res:
code = code.replace(code[start:end+11], res)
return code
def _have_I_right(self, user, post_id):
@ -246,7 +249,8 @@ class Index(DynastieGenerator):
if not post: continue
new_content = self._loadPostContent(post)
if new_content:
text = text.replace('[[' + str(post_id) + ']]', new_content)
p = '[[' + str(post_id) + ']]'
text = text.replace(p, new_content)
if internal_posts: return text
# HTML replace
if not post or (post and post.content_format == Post.CONTENT_HTML):
@ -267,7 +271,7 @@ class Index(DynastieGenerator):
if not post: break
new_content = self._loadPostContent(post)
if new_content:
text = text.replace(text[start:end+18], new_content.encode('utf-8'))
text = text.replace(text[start:end+18], new_content)
return text
def _loadPostContent(self, post):
@ -287,7 +291,7 @@ class Index(DynastieGenerator):
filename = filename2
if not filename in self.hash_posts_content:
f = open(filename, 'rb')
f = codecs.open(filename, 'rb', 'utf-8')
post_content = f.read()
f.close()
self.parent_posts.append(post.id)
@ -498,7 +502,7 @@ class Index(DynastieGenerator):
# Remove <pre> after <div class="highlight">
code = code[28:-13]
code = '<div class="highlight">' + code + '</div>'
code = u'<div class="highlight">' + unicode(code, 'utf-8') + u'</div>'
return code

View File

@ -1,5 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<ljdc>
<entry>
<id>138932499327</id>
<address>http://thecodinglove.com/post/138932499327/seeing-my-own-code-after-years</address>
<title>Seeing my own code after years</title>
<img>http://tclhost.com/nDmdJau.gif</img>
</entry>
<entry>
<id>137804239758</id>
<address>http://thecodinglove.com/post/137804239758/when-you-discover-a-serious-bug-during-a-client</address>
<title>When you discover a serious bug during a client...</title>
<img>http://tclhost.com/rzUXIEl.gif</img>
</entry>
<entry>
<id>137344478111</id>
<address>http://thecodinglove.com/post/137344478111/arriving-late-to-an-important-meeting</address>
<title>Arriving late to an important meeting</title>
<img>http://tclhost.com/CXW1sJ4.gif</img>
</entry>
<entry>
<id>132019829293</id>
<address>http://thecodinglove.com/post/132019829293/when-a-mad-boss-looking-for-me-enters-the-open</address>