When adding a new cover image to an EPUB file that does not have a marked cover image, also check the guide for any images that are marked as covers

This commit is contained in:
Kovid Goyal 2016-06-18 10:16:24 +05:30
parent 525216d957
commit 8ff668d26c
2 changed files with 26 additions and 5 deletions

View File

@ -46,10 +46,18 @@ def set_metadata_opf2(root, cover_prefix, mi, opf_version, cover_data=None, appl
opf.timestamp = mi.timestamp opf.timestamp = mi.timestamp
raster_cover = opf.raster_cover raster_cover = opf.raster_cover
if raster_cover is None and cover_data is not None: if raster_cover is None and cover_data is not None:
if cover_prefix and not cover_prefix.endswith('/'): guide_raster_cover = opf.guide_raster_cover
cover_prefix += '/' i = None
name = cover_prefix + 'cover.jpg' if guide_raster_cover is not None:
i = create_manifest_item(opf.root, name, 'cover') i = guide_raster_cover
raster_cover = i.get('href')
else:
if cover_prefix and not cover_prefix.endswith('/'):
cover_prefix += '/'
name = cover_prefix + 'cover.jpg'
i = create_manifest_item(opf.root, name, 'cover')
if i is not None:
raster_cover = name
if i is not None: if i is not None:
if opf_version.major < 3: if opf_version.major < 3:
[x.getparent().remove(x) for x in opf.root.xpath('//*[local-name()="meta" and @name="cover"]')] [x.getparent().remove(x) for x in opf.root.xpath('//*[local-name()="meta" and @name="cover"]')]
@ -59,7 +67,6 @@ def set_metadata_opf2(root, cover_prefix, mi, opf_version, cover_data=None, appl
for x in opf.root.xpath('//*[local-name()="item" and contains(@properties, "cover-image")]'): for x in opf.root.xpath('//*[local-name()="item" and contains(@properties, "cover-image")]'):
x.set('properties', x.get('properties').replace('cover-image', '').strip()) x.set('properties', x.get('properties').replace('cover-image', '').strip())
i.set('properties', 'cover-image') i.set('properties', 'cover-image')
raster_cover = name
with pretty_print: with pretty_print:
return opf.render(), raster_cover return opf.render(), raster_cover

View File

@ -523,6 +523,7 @@ class OPF(object): # {{{
pubdate_path = XPath('descendant::*[re:match(name(), "date", "i")]') pubdate_path = XPath('descendant::*[re:match(name(), "date", "i")]')
raster_cover_path = XPath('descendant::*[re:match(name(), "meta", "i") and ' + raster_cover_path = XPath('descendant::*[re:match(name(), "meta", "i") and ' +
're:match(@name, "cover", "i") and @content]') 're:match(@name, "cover", "i") and @content]')
guide_cover_path = XPath('descendant::*[local-name()="guide"]/*[local-name()="reference" and re:match(@type, "cover", "i")]/@href')
identifier_path = XPath('descendant::*[re:match(name(), "identifier", "i")]') identifier_path = XPath('descendant::*[re:match(name(), "identifier", "i")]')
application_id_path = XPath('descendant::*[re:match(name(), "identifier", "i") and '+ application_id_path = XPath('descendant::*[re:match(name(), "identifier", "i") and '+
'(re:match(@opf:scheme, "calibre|libprs500", "i") or re:match(@scheme, "calibre|libprs500", "i"))]') '(re:match(@opf:scheme, "calibre|libprs500", "i") or re:match(@scheme, "calibre|libprs500", "i"))]')
@ -1173,6 +1174,19 @@ class OPF(object): # {{{
if mt and 'xml' not in mt and 'html' not in mt: if mt and 'xml' not in mt and 'html' not in mt:
return item.get('href', None) return item.get('href', None)
@property
def guide_raster_cover(self):
covers = self.guide_cover_path(self.root)
if covers:
mt_map = {i.get('href'):i for i in self.itermanifest()}
for href in covers:
if href:
i = mt_map.get(href)
if i is not None:
iid, mt = i.get('id'), i.get('media-type')
if iid and mt and mt.lower() in {'image/png', 'image/jpeg', 'image/jpg', 'image/gif'}:
return i
@property @property
def epub3_nav(self): def epub3_nav(self):
if self.package_version >= 3.0: if self.package_version >= 3.0: