From aebc9bd8bc0d606e5620fade6a966acdbdc97851 Mon Sep 17 00:00:00 2001 From: John Schember Date: Tue, 4 Aug 2009 19:08:53 -0400 Subject: [PATCH] Fix bug #3073: Gracefully ignore invalid images. Fix bug #3071: Set metadata for FB2 output. --- src/calibre/ebooks/fb2/fb2ml.py | 28 +++++++++++++++++++---- src/calibre/ebooks/pdb/ereader/writer.py | 29 +++++++++++++----------- src/calibre/ebooks/rb/writer.py | 28 +++++++++++++---------- src/calibre/ebooks/rtf/rtfml.py | 4 ++-- 4 files changed, 58 insertions(+), 31 deletions(-) diff --git a/src/calibre/ebooks/fb2/fb2ml.py b/src/calibre/ebooks/fb2/fb2ml.py index 2de2c599cf..42b71c8fba 100644 --- a/src/calibre/ebooks/fb2/fb2ml.py +++ b/src/calibre/ebooks/fb2/fb2ml.py @@ -17,7 +17,7 @@ from calibre import prepare_string_for_xml from calibre.constants import __appname__, __version__ from calibre.ebooks.oeb.base import XHTML, XHTML_NS, barename, namespace from calibre.ebooks.oeb.stylizer import Stylizer -from calibre.ebooks.oeb.base import OEB_IMAGES +from calibre.ebooks.oeb.base import OEB_RASTER_IMAGES TAG_MAP = { 'b' : 'strong', @@ -60,12 +60,32 @@ class FB2MLizer(object): return u'\n%s' % etree.tostring(etree.fromstring(output), encoding=unicode, pretty_print=True) def fb2_header(self): + author_first = u'' + author_middle = u'' + author_last = u'' + author_parts = self.oeb_book.metadata.creator[0].value.split(' ') + + if len(author_parts) == 1: + author_last = author_parts[0] + elif len(author_parts == 2): + author_first = author_parts[0] + author_last = author_parts[1] + else: + author_first = author_parts[0] + author_middle = ' '.join(author_parts[1:-2]) + author_last = author_parts[-1] + return u'\n' \ - '\n%s ' \ + '\n\n ' \ + '\n%s\n%s' \ + '\n%s\n\n' \ + '%s ' \ ' ' \ '%s - %s\n' \ - '\n\n
' % (self.oeb_book.metadata.title[0].value, __appname__, __version__) + '\n\n
' % (author_first, author_middle, + author_last, self.oeb_book.metadata.title[0].value, + __appname__, __version__) def fb2_body_footer(self): return u'\n
\n' @@ -76,7 +96,7 @@ class FB2MLizer(object): def fb2mlize_images(self): images = u'' for item in self.oeb_book.manifest: - if item.media_type in OEB_IMAGES: + if item.media_type in OEB_RASTER_IMAGES: raw_data = b64encode(item.data) # Don't put the encoded image on a single line. data = '' diff --git a/src/calibre/ebooks/pdb/ereader/writer.py b/src/calibre/ebooks/pdb/ereader/writer.py index dc9e99bb52..9bf83c33b0 100644 --- a/src/calibre/ebooks/pdb/ereader/writer.py +++ b/src/calibre/ebooks/pdb/ereader/writer.py @@ -20,7 +20,7 @@ except ImportError: import cStringIO from calibre.ebooks.pdb.formatwriter import FormatWriter -from calibre.ebooks.oeb.base import OEB_IMAGES +from calibre.ebooks.oeb.base import OEB_RASTER_IMAGES from calibre.ebooks.pdb.header import PdbHeaderBuilder from calibre.ebooks.pdb.ereader import image_name from calibre.ebooks.pml.pmlml import PMLMLizer @@ -72,21 +72,24 @@ class Writer(FormatWriter): images = [] for item in manifest: - if item.media_type in OEB_IMAGES: - header = 'PNG ' + if item.media_type in OEB_RASTER_IMAGES: + try: + im = Image.open(cStringIO.StringIO(item.data)).convert('P') + im.thumbnail((300,300), Image.ANTIALIAS) - header += image_name(item.href) - header = header.ljust(62, '\x00') + data = cStringIO.StringIO() + im.save(data, 'PNG') + data = data.getvalue() - im = Image.open(cStringIO.StringIO(item.data)).convert('P') - im.thumbnail((300,300), Image.ANTIALIAS) + header = 'PNG ' + header += image_name(item.href) + header = header.ljust(62, '\x00') - data = cStringIO.StringIO() - im.save(data, 'PNG') - data = data.getvalue() - - if len(data) + len(header) < 65505: - images.append((header, data)) + if len(data) + len(header) < 65505: + images.append((header, data)) + except Exception as e: + self.log.error('Error: Could not include file %s becuase ' \ + '%s.' % (item.href, e)) return images diff --git a/src/calibre/ebooks/rb/writer.py b/src/calibre/ebooks/rb/writer.py index 41e01af77f..81ffcf0bb3 100644 --- a/src/calibre/ebooks/rb/writer.py +++ b/src/calibre/ebooks/rb/writer.py @@ -19,7 +19,7 @@ import cStringIO from calibre.ebooks.rb.rbml import RBMLizer from calibre.ebooks.rb import HEADER from calibre.ebooks.rb import unique_name -from calibre.ebooks.oeb.base import OEB_IMAGES +from calibre.ebooks.oeb.base import OEB_RASTER_IMAGES from calibre.constants import __appname__, __version__ TEXT_RECORD_SIZE = 4096 @@ -116,20 +116,24 @@ class RBWriter(object): used_names = [] for item in manifest: - if item.media_type in OEB_IMAGES: - data = '' + if item.media_type in OEB_RASTER_IMAGES: + try: + data = '' - im = Image.open(cStringIO.StringIO(item.data)).convert('L') - data = cStringIO.StringIO() - im.save(data, 'PNG') - data = data.getvalue() + im = Image.open(cStringIO.StringIO(item.data)).convert('L') + data = cStringIO.StringIO() + im.save(data, 'PNG') + data = data.getvalue() - name = '%s.png' % os.path.splitext(os.path.basename(item.href))[0] - name = unique_name(name, used_names) - used_names.append(name) - self.name_map[os.path.basename(item.href)] = name + name = '%s.png' % os.path.splitext(os.path.basename(item.href))[0] + name = unique_name(name, used_names) + used_names.append(name) + self.name_map[os.path.basename(item.href)] = name - images.append((name, data)) + images.append((name, data)) + except Exception as e: + self.log.error('Error: Could not include file %s becuase ' \ + '%s.' % (item.href, e)) return images diff --git a/src/calibre/ebooks/rtf/rtfml.py b/src/calibre/ebooks/rtf/rtfml.py index e55ae670f0..84370e5b3d 100644 --- a/src/calibre/ebooks/rtf/rtfml.py +++ b/src/calibre/ebooks/rtf/rtfml.py @@ -20,7 +20,7 @@ except ImportError: import cStringIO from calibre.ebooks.oeb.base import XHTML, XHTML_NS, barename, namespace, \ - OEB_IMAGES + OEB_RASTER_IMAGES from calibre.ebooks.oeb.stylizer import Stylizer from calibre.ebooks.metadata import authors_to_string @@ -115,7 +115,7 @@ class RTFMLizer(object): def insert_images(self, text): for item in self.oeb_book.manifest: - if item.media_type in OEB_IMAGES: + if item.media_type in OEB_RASTER_IMAGES: src = os.path.basename(item.href) data, width, height = self.image_to_hexstring(item.data) text = text.replace('SPECIAL_IMAGE-%s-REPLACE_ME' % src, '\n\n{\\*\\shppict{\\pict\\picw%i\\pich%i\\jpegblip \n%s\n}}\n\n' % (width, height, data))