From 27d199a74be7ae683fffe193c202f529063fe70a Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 3 May 2012 18:45:29 +0530 Subject: [PATCH] EPUB Input: Handle the case of the metadata ToC containing a reference to the cover HTML file correctly. Fixes #993812 (Update cover page in toc.ncx when converting) --- .../ebooks/conversion/plugins/epub_input.py | 20 ++++++++++++++++++- src/calibre/ebooks/conversion/plumber.py | 7 +++++++ src/calibre/ebooks/oeb/transforms/cover.py | 4 ++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/calibre/ebooks/conversion/plugins/epub_input.py b/src/calibre/ebooks/conversion/plugins/epub_input.py index 77da1bb290..e19d466659 100644 --- a/src/calibre/ebooks/conversion/plugins/epub_input.py +++ b/src/calibre/ebooks/conversion/plugins/epub_input.py @@ -65,6 +65,7 @@ class EPUBInput(InputFormatPlugin): return False def rationalize_cover(self, opf, log): + removed = None from lxml import etree guide_cover, guide_elem = None, None for guide_elem in opf.iterguide(): @@ -91,6 +92,7 @@ class EPUBInput(InputFormatPlugin): # specially if not self.for_viewer: spine[0].getparent().remove(spine[0]) + removed = guide_cover guide_elem.set('href', 'calibre_raster_cover.jpg') from calibre.ebooks.oeb.base import OPF t = etree.SubElement(elem[0].getparent(), OPF('item'), @@ -109,6 +111,7 @@ class EPUBInput(InputFormatPlugin): if renderer is not None: open('calibre_raster_cover.jpg', 'wb').write( renderer) + return removed def find_opf(self): from lxml import etree @@ -170,7 +173,7 @@ class EPUBInput(InputFormatPlugin): for elem in opf.iterguide(): elem.set('href', delta+elem.get('href')) - self.rationalize_cover(opf, log) + self.removed_cover = self.rationalize_cover(opf, log) self.optimize_opf_parsing = opf for x in opf.itermanifest(): @@ -198,3 +201,18 @@ class EPUBInput(InputFormatPlugin): nopf.write(opf.render()) return os.path.abspath(u'content.opf') + + def postprocess_book(self, oeb, opts, log): + rc = getattr(self, 'removed_cover', None) + if rc: + cover_toc_item = None + for item in oeb.toc.iterdescendants(): + if item.href == rc: + cover_toc_item = item + break + spine = {x.href for x in oeb.spine} + if (cover_toc_item is not None and cover_toc_item.count() == 0 and + cover_toc_item not in spine): + oeb.toc.item_that_refers_to_cover = cover_toc_item + + diff --git a/src/calibre/ebooks/conversion/plumber.py b/src/calibre/ebooks/conversion/plumber.py index fcef0c374d..8d523f431a 100644 --- a/src/calibre/ebooks/conversion/plumber.py +++ b/src/calibre/ebooks/conversion/plumber.py @@ -1011,6 +1011,13 @@ OptionRecommendation(name='search_replace', pr(0.35) self.flush() + if self.output_plugin.file_type != 'epub': + # Remove the toc reference to the html cover, if any, except for + # epub, as the epub output plugin will do the right thing with it. + item = getattr(self.oeb.toc, 'item_that_refers_to_cover', None) + if item is not None: + self.oeb.toc.remove(item) + from calibre.ebooks.oeb.transforms.flatcss import CSSFlattener fbase = self.opts.base_font_size if fbase < 1e-4: diff --git a/src/calibre/ebooks/oeb/transforms/cover.py b/src/calibre/ebooks/oeb/transforms/cover.py index 4a4f512293..8facbab785 100644 --- a/src/calibre/ebooks/oeb/transforms/cover.py +++ b/src/calibre/ebooks/oeb/transforms/cover.py @@ -167,5 +167,9 @@ class CoverManager(object): self.oeb.guide.refs['cover'].href = item.href if 'titlepage' in self.oeb.guide.refs: self.oeb.guide.refs['titlepage'].href = item.href + titem = getattr(self.oeb.toc, 'item_that_refers_to_cover', None) + if titem is not None: + titem.href = item.href +