From d1e9ce237bdc283464163ac17416d5257a276ff4 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 28 Apr 2010 07:54:59 -0600 Subject: [PATCH] When converting EPUB to EPUB multiple times and creating book jacket from metadata, if an existing book jacket is found, replace it. This will only work with EPUBs created with the current release onwards --- src/calibre/ebooks/oeb/transforms/jacket.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/calibre/ebooks/oeb/transforms/jacket.py b/src/calibre/ebooks/oeb/transforms/jacket.py index 597c6f59cd..2991e28d02 100644 --- a/src/calibre/ebooks/oeb/transforms/jacket.py +++ b/src/calibre/ebooks/oeb/transforms/jacket.py @@ -113,14 +113,27 @@ class Jacket(object): jacket=escape(_('Book Jacket')), series=series, tags=tags, rating=self.get_rating(mi.rating)) id, href = self.oeb.manifest.generate('jacket', 'jacket.xhtml') - from calibre.ebooks.oeb.base import RECOVER_PARSER + from calibre.ebooks.oeb.base import RECOVER_PARSER, XPath try: root = etree.fromstring(generate_html(comments), parser=RECOVER_PARSER) except: root = etree.fromstring(generate_html(escape(orig_comments)), parser=RECOVER_PARSER) - item = self.oeb.manifest.add(id, href, guess_type(href)[0], data=root) - self.oeb.spine.insert(0, item, True) + jacket = XPath('//h:meta[@name="calibre-content" and @content="jacket"]') + found = None + for item in list(self.oeb.spine)[:4]: + try: + if jacket(item.data): + found = item + break + except: + continue + if found is None: + item = self.oeb.manifest.add(id, href, guess_type(href)[0], data=root) + self.oeb.spine.insert(0, item, True) + else: + self.log('Found existing book jacket, replacing...') + found.data = root def __call__(self, oeb, opts, metadata):