diff --git a/src/calibre/ebooks/oeb/output.py b/src/calibre/ebooks/oeb/output.py index 585b56c7b6..6709141a01 100644 --- a/src/calibre/ebooks/oeb/output.py +++ b/src/calibre/ebooks/oeb/output.py @@ -32,6 +32,12 @@ class OEBOutput(OutputFormatPlugin): for key in (OPF_MIME, NCX_MIME, PAGE_MAP_MIME): href, root = results.pop(key, [None, None]) if root is not None: + if key == OPF_MIME: + try: + self.workaround_nook_cover_bug(root) + except: + self.log.exception('Something went wrong while trying to' + ' workaround Nook cover bug, ignoring') raw = etree.tostring(root, pretty_print=True, encoding='utf-8', xml_declaration=True) if key == OPF_MIME: @@ -49,3 +55,24 @@ class OEBOutput(OutputFormatPlugin): with open(path, 'wb') as f: f.write(str(item)) item.unload_data_from_memory(memory=path) + + def workaround_nook_cover_bug(self, root): # {{{ + cov = root.xpath('//*[local-name() = "meta" and @name="cover" and' + ' @content != "cover"]') + if len(cov) == 1: + manpath = ('//*[local-name() = "manifest"]/*[local-name() = "item" ' + ' and @id="%s" and @media-type]') + cov = cov[0] + covid = cov.get('content') + manifest_item = root.xpath(manpath%covid) + has_cover = root.xpath(manpath%'cover') + if len(manifest_item) == 1 and not has_cover and \ + manifest_item[0].get('media-type', + '').startswith('image/'): + self.log.warn('The cover image has an id != "cover". Renaming' + ' to work around Nook Color bug') + manifest_item = manifest_item[0] + manifest_item.set('id', 'cover') + cov.set('content', 'cover') + # }}} +