- if cover_fn:
- cover = StringIO()
- cover_fn(book_info.author.readable(), book_info.title).save(cover, format='JPEG')
- zip.writestr(os.path.join('OPS', 'cover.jpg'), cover.getvalue())
- del cover
- zip.writestr('OPS/cover.html', open(get_resource('epub/cover.html')).read())
+ if cover:
+ cover_file = StringIO()
+ c = cover(book_info.author.readable(), book_info.title)
+ c.save(cover_file)
+ c_name = 'cover.%s' % c.ext()
+ zip.writestr(os.path.join('OPS', c_name), cover_file.getvalue())
+ del cover_file
+
+ cover_tree = etree.parse(get_resource('epub/cover.html'))
+ cover_tree.find('//' + XHTMLNS('img')).set('src', c_name)
+ zip.writestr('OPS/cover.html', etree.tostring(
+ cover_tree, method="html", pretty_print=True))
+