py3: make html output work

The templates are initialized as decoded unicode strings, and templite
expects to work in unicode strings as well. However, etree.tostring
returns a bytestring unless explicitly told to use the 'unicode'
encoding ('utf-8' is not enough)
This commit is contained in:
Eli Schwartz 2019-05-19 17:05:01 -04:00
parent f75ea236e5
commit fda2ab0024
No known key found for this signature in database
GPG Key ID: CEB167EFB5722BD6

View File

@ -79,7 +79,7 @@ class HTMLOutput(OutputFormatPlugin):
from lxml import etree from lxml import etree
root = self.generate_toc(oeb_book, ref_url, output_dir) root = self.generate_toc(oeb_book, ref_url, output_dir)
return etree.tostring(root, pretty_print=True, encoding='utf-8', return etree.tostring(root, pretty_print=True, encoding='unicode',
xml_declaration=False) xml_declaration=False)
def convert(self, oeb_book, output_path, input_plugin, opts, log): def convert(self, oeb_book, output_path, input_plugin, opts, log):
@ -161,14 +161,14 @@ class HTMLOutput(OutputFormatPlugin):
# get & clean HTML <HEAD>-data # get & clean HTML <HEAD>-data
head = root.xpath('//h:head', namespaces={'h': 'http://www.w3.org/1999/xhtml'})[0] head = root.xpath('//h:head', namespaces={'h': 'http://www.w3.org/1999/xhtml'})[0]
head_content = etree.tostring(head, pretty_print=True, encoding='utf-8') head_content = etree.tostring(head, pretty_print=True, encoding='unicode')
head_content = re.sub(r'\<\/?head.*\>', '', head_content) head_content = re.sub(r'\<\/?head.*\>', '', head_content)
head_content = re.sub(re.compile(r'\<style.*\/style\>', re.M|re.S), '', head_content) head_content = re.sub(re.compile(r'\<style.*\/style\>', re.M|re.S), '', head_content)
head_content = re.sub(r'<(title)([^>]*)/>', r'<\1\2></\1>', head_content) head_content = re.sub(r'<(title)([^>]*)/>', r'<\1\2></\1>', head_content)
# get & clean HTML <BODY>-data # get & clean HTML <BODY>-data
body = root.xpath('//h:body', namespaces={'h': 'http://www.w3.org/1999/xhtml'})[0] body = root.xpath('//h:body', namespaces={'h': 'http://www.w3.org/1999/xhtml'})[0]
ebook_content = etree.tostring(body, pretty_print=True, encoding='utf-8') ebook_content = etree.tostring(body, pretty_print=True, encoding='unicode')
ebook_content = re.sub(r'\<\/?body.*\>', '', ebook_content) ebook_content = re.sub(r'\<\/?body.*\>', '', ebook_content)
ebook_content = re.sub(r'<(div|a|span)([^>]*)/>', r'<\1\2></\1>', ebook_content) ebook_content = re.sub(r'<(div|a|span)([^>]*)/>', r'<\1\2></\1>', ebook_content)
@ -202,7 +202,7 @@ class HTMLOutput(OutputFormatPlugin):
# write html to file # write html to file
with open(path, 'wb') as f: with open(path, 'wb') as f:
f.write(t) f.write(t.encode('utf-8'))
item.unload_data_from_memory(memory=path) item.unload_data_from_memory(memory=path)
zfile = zipfile.ZipFile(output_path, "w") zfile = zipfile.ZipFile(output_path, "w")