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)

This commit is contained in:
Kovid Goyal 2013-04-20 16:38:49 +05:30
parent bf391cf726
commit 1dd6a3d9b7

View File

@ -46,10 +46,11 @@ def is_raster_image(media_type):
return media_type and media_type.lower() in { return media_type and media_type.lower() in {
'image/png', 'image/jpeg', 'image/jpg', 'image/gif'} 'image/png', 'image/jpeg', 'image/jpg', 'image/gif'}
COVER_TYPES = { 'coverimagestandard', 'other.ms-coverimage-standard', COVER_TYPES = {
'other.ms-titleimage-standard', 'other.ms-titleimage', 'coverimagestandard', 'other.ms-coverimage-standard',
'other.ms-coverimage', 'other.ms-thumbimage-standard', 'other.ms-titleimage-standard', 'other.ms-titleimage',
'other.ms-thumbimage', 'thumbimagestandard', 'cover'} 'other.ms-coverimage', 'other.ms-thumbimage-standard',
'other.ms-thumbimage', 'thumbimagestandard', 'cover'}
def find_cover_image(container): def find_cover_image(container):
'Find a raster image marked as a cover in the OPF' '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): def find_cover_image_in_page(container, cover_page):
root = container.parsed(cover_page) root = container.parsed(cover_page)
body = XPath('//h:body')(root) body = XPath('//h:body')(root)
if len(body) != 1: return if len(body) != 1:
return
body = body[0] body = body[0]
images = [] images = []
for img in XPath('descendant::h:img[@src]|descendant::svg:svg/descendant::svg:image')(body): 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' ar = 'xMidYMid meet' if keep_aspect else 'none'
templ = CoverManager.SVG_TEMPLATE.replace('__ar__', ar) templ = CoverManager.SVG_TEMPLATE.replace('__ar__', ar)
templ = templ.replace('__viewbox__', '0 0 %d %d'%(width, height)) 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)) templ = templ.replace('__height__', str(height))
titlepage_item = container.generate_item('titlepage.xhtml', titlepage_item = container.generate_item('titlepage.xhtml',
id_prefix='titlepage') id_prefix='titlepage')
@ -179,7 +181,7 @@ def create_epub_cover(container, cover_path):
guide = container.opf_get_or_create('guide') guide = container.opf_get_or_create('guide')
container.insert_into_xml(guide, guide.makeelement( container.insert_into_xml(guide, guide.makeelement(
OPF('reference'), type='cover', title=_('Cover'), 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') metadata = container.opf_get_or_create('metadata')
meta = metadata.makeelement(OPF('meta'), name='cover') meta = metadata.makeelement(OPF('meta'), name='cover')
meta.set('content', raster_cover_item.get('id')) meta.set('content', raster_cover_item.get('id'))