From 184b788e2beb7e56dc04e5843273c1d1dfa001da Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 10 Jan 2014 08:42:39 +0530 Subject: [PATCH] Edit book: Fix marking a file as titlepage not working if the opf does not contain a section. Fixes #1267643 [Edit Book: Mark as Cover Page doesn't seem to work](https://bugs.launchpad.net/calibre/+bug/1267643) --- src/calibre/ebooks/oeb/polish/cover.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/calibre/ebooks/oeb/polish/cover.py b/src/calibre/ebooks/oeb/polish/cover.py index 4fb5def47c..0f531385d9 100644 --- a/src/calibre/ebooks/oeb/polish/cover.py +++ b/src/calibre/ebooks/oeb/polish/cover.py @@ -123,6 +123,14 @@ def find_cover_image(container, strict=False): if largest_cover[0]: return largest_cover[0] +def get_guides(container): + guides = container.opf_xpath('//opf:guide') + if not guides: + container.insert_into_xml(container.opf, container.opf.makeelement( + OPF('guide'))) + guides = container.opf_xpath('//opf:guide') + return guides + def mark_as_cover_epub(container, name): mmap = {v:k for k, v in container.manifest_id_map.iteritems()} if name not in mmap: @@ -148,7 +156,7 @@ def mark_as_cover_epub(container, name): # If no entry for titlepage exists in guide, insert one that points to this # image if not container.opf_xpath('//opf:guide/opf:reference[@type="cover"]'): - for guide in container.opf_xpath('//opf:guide'): + for guide in get_guides(container): container.insert_into_xml(guide, guide.makeelement( OPF('reference'), type='cover', href=container.name_to_href(name, container.opf_name))) @@ -166,7 +174,7 @@ def mark_as_titlepage(container, name, move_to_start=True): for ref in container.opf_xpath('//opf:guide/opf:reference[@type="cover"]'): ref.getparent().remove(ref) - for guide in container.opf_xpath('//opf:guide'): + for guide in get_guides(container): container.insert_into_xml(guide, guide.makeelement( OPF('reference'), type='cover', href=container.name_to_href(name, container.opf_name))) container.dirty(container.opf_name)