From 1dd6a3d9b77ab0a8e674b8a9af7c75c7061772a9 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 20 Apr 2013 16:38:49 +0530 Subject: [PATCH] Book polishing: Fix inserting cover into an epub with no cover could lead to incorrect guide entry if the opf is not at the root of the epub. Fixes #1167941 (Polish book - Update cover) --- src/calibre/ebooks/oeb/polish/cover.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/calibre/ebooks/oeb/polish/cover.py b/src/calibre/ebooks/oeb/polish/cover.py index 01b9e25e59..5cee827d72 100644 --- a/src/calibre/ebooks/oeb/polish/cover.py +++ b/src/calibre/ebooks/oeb/polish/cover.py @@ -46,10 +46,11 @@ def is_raster_image(media_type): return media_type and media_type.lower() in { 'image/png', 'image/jpeg', 'image/jpg', 'image/gif'} -COVER_TYPES = { 'coverimagestandard', 'other.ms-coverimage-standard', - 'other.ms-titleimage-standard', 'other.ms-titleimage', - 'other.ms-coverimage', 'other.ms-thumbimage-standard', - 'other.ms-thumbimage', 'thumbimagestandard', 'cover'} +COVER_TYPES = { + 'coverimagestandard', 'other.ms-coverimage-standard', + 'other.ms-titleimage-standard', 'other.ms-titleimage', + 'other.ms-coverimage', 'other.ms-thumbimage-standard', + 'other.ms-thumbimage', 'thumbimagestandard', 'cover'} def find_cover_image(container): 'Find a raster image marked as a cover in the OPF' @@ -92,7 +93,8 @@ def find_cover_page(container): def find_cover_image_in_page(container, cover_page): root = container.parsed(cover_page) body = XPath('//h:body')(root) - if len(body) != 1: return + if len(body) != 1: + return body = body[0] images = [] for img in XPath('descendant::h:img[@src]|descendant::svg:svg/descendant::svg:image')(body): @@ -152,7 +154,7 @@ def create_epub_cover(container, cover_path): ar = 'xMidYMid meet' if keep_aspect else 'none' templ = CoverManager.SVG_TEMPLATE.replace('__ar__', ar) templ = templ.replace('__viewbox__', '0 0 %d %d'%(width, height)) - templ = templ.replace('__width__', str(width)) + templ = templ.replace('__width__', str(width)) templ = templ.replace('__height__', str(height)) titlepage_item = container.generate_item('titlepage.xhtml', id_prefix='titlepage') @@ -179,7 +181,7 @@ def create_epub_cover(container, cover_path): guide = container.opf_get_or_create('guide') container.insert_into_xml(guide, guide.makeelement( OPF('reference'), type='cover', title=_('Cover'), - href=container.name_to_href(titlepage))) + href=container.name_to_href(titlepage, base=container.opf_name))) metadata = container.opf_get_or_create('metadata') meta = metadata.makeelement(OPF('meta'), name='cover') meta.set('content', raster_cover_item.get('id'))