From 06aa59563df0d45b2dd958e503583478ed511b13 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 1 Dec 2013 11:26:49 +0530 Subject: [PATCH] Implement mark as title page --- src/calibre/ebooks/oeb/polish/container.py | 2 ++ src/calibre/ebooks/oeb/polish/cover.py | 17 +++++++++++++++++ src/calibre/gui2/tweak_book/boss.py | 6 +++++- src/calibre/gui2/tweak_book/file_list.py | 15 ++++++++++++++- 4 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/calibre/ebooks/oeb/polish/container.py b/src/calibre/ebooks/oeb/polish/container.py index 904c5e60ab..8700aa5346 100644 --- a/src/calibre/ebooks/oeb/polish/container.py +++ b/src/calibre/ebooks/oeb/polish/container.py @@ -93,6 +93,7 @@ class Container(object): # {{{ ''' book_type = 'oeb' + SUPPORTS_TITLEPAGES = True def __init__(self, rootpath, opfpath, log, clone_data=None): self.root = clone_data['root'] if clone_data is not None else os.path.abspath(rootpath) @@ -998,6 +999,7 @@ def do_explode(path, dest): class AZW3Container(Container): book_type = 'azw3' + SUPPORTS_TITLEPAGES = False def __init__(self, pathtoazw3, log, clone_data=None, tdir=None): if clone_data is not None: diff --git a/src/calibre/ebooks/oeb/polish/cover.py b/src/calibre/ebooks/oeb/polish/cover.py index 342c989dcd..8c6871de53 100644 --- a/src/calibre/ebooks/oeb/polish/cover.py +++ b/src/calibre/ebooks/oeb/polish/cover.py @@ -148,6 +148,23 @@ def mark_as_cover_epub(container, name): container.dirty(container.opf_name) +def mark_as_titlepage(container, name, move_to_start=True): + if move_to_start: + for item, q, linear in container.spine_iter: + if name == q: + break + if not linear: + item.set('linear', 'yes') + if item.getparent().index(item) > 0: + container.insert_into_xml(item.getparent(), item, 0) + for ref in container.opf_xpath('//opf:guide/opf:reference[@type="cover"]'): + ref.getparent().remove(ref) + + for guide in container.opf_xpath('//opf:guide'): + 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) + def find_cover_page(container): 'Find a document marked as a cover in the OPF' mm = container.mime_map diff --git a/src/calibre/gui2/tweak_book/boss.py b/src/calibre/gui2/tweak_book/boss.py index 1b7072a958..2295e7102b 100644 --- a/src/calibre/gui2/tweak_book/boss.py +++ b/src/calibre/gui2/tweak_book/boss.py @@ -19,7 +19,7 @@ from calibre.ptempfile import PersistentTemporaryDirectory from calibre.ebooks.oeb.base import urlnormalize from calibre.ebooks.oeb.polish.main import SUPPORTED, tweak_polish from calibre.ebooks.oeb.polish.container import get_container as _gc, clone_container, guess_type -from calibre.ebooks.oeb.polish.cover import mark_as_cover +from calibre.ebooks.oeb.polish.cover import mark_as_cover, mark_as_titlepage from calibre.ebooks.oeb.polish.pretty import fix_all_html, pretty_all from calibre.ebooks.oeb.polish.replace import rename_files from calibre.ebooks.oeb.polish.split import split, merge, AbortError @@ -78,6 +78,10 @@ class Boss(QObject): c = current_container() if action == 'cover': mark_as_cover(current_container(), name) + elif action.startswith('titlepage:'): + action, move_to_start = action.partition(':')[0::2] + move_to_start = move_to_start == 'True' + mark_as_titlepage(current_container(), name, move_to_start=move_to_start) if c.opf_name in editors: editors[c.opf_name].replace_data(c.raw_data(c.opf_name)) diff --git a/src/calibre/gui2/tweak_book/file_list.py b/src/calibre/gui2/tweak_book/file_list.py index 698761e52b..32e221fb54 100644 --- a/src/calibre/gui2/tweak_book/file_list.py +++ b/src/calibre/gui2/tweak_book/file_list.py @@ -20,7 +20,7 @@ from calibre.ebooks.oeb.base import OEB_STYLES, OEB_DOCS from calibre.ebooks.oeb.polish.container import guess_type, OEB_FONTS from calibre.ebooks.oeb.polish.cover import ( get_cover_page_name, get_raster_cover_name, is_raster_image) -from calibre.gui2 import error_dialog, choose_files +from calibre.gui2 import error_dialog, choose_files, question_dialog from calibre.gui2.tweak_book import current_container, elided_text from calibre.gui2.tweak_book.editor import syntax_from_mime from calibre.gui2.tweak_book.templates import template_for @@ -309,9 +309,12 @@ class FileList(QTreeWidget): if ci is not None: cn = unicode(ci.data(0, NAME_ROLE).toString()) mt = unicode(ci.data(0, MIME_ROLE).toString()) + cat = unicode(ci.data(0, CATEGORY_ROLE).toString()) m.addAction(QIcon(I('modified.png')), _('&Rename %s') % (elided_text(self.font(), cn)), self.edit_current_item) if is_raster_image(mt): m.addAction(QIcon(I('default_cover.png')), _('Mark %s as cover image') % elided_text(self.font(), cn), partial(self.mark_as_cover, cn)) + elif current_container().SUPPORTS_TITLEPAGES and mt in OEB_DOCS and cat == 'text': + m.addAction(QIcon(I('default_cover.png')), _('Mark %s as title/cover page') % elided_text(self.font(), cn), partial(self.mark_as_titlepage, cn)) selected_map = defaultdict(list) for item in sel: @@ -348,6 +351,16 @@ class FileList(QTreeWidget): def mark_as_cover(self, name): self.mark_requested.emit(name, 'cover') + def mark_as_titlepage(self, name): + first = unicode(self.categories['text'].child(0).data(0, NAME_ROLE).toString()) == name + move_to_start = False + if not first: + move_to_start = question_dialog(self, _('Not first item'), _( + '%s is not the first text item. You should only mark the' + ' first text item as cover. Do you want to make it the' + ' first item?') % elided_text(self.font(), name)) + self.mark_requested.emit(name, 'titlepage:%r' % move_to_start) + def keyPressEvent(self, ev): if ev.key() in (Qt.Key_Delete, Qt.Key_Backspace): ev.accept()