Implement mark as title page

This commit is contained in:
Kovid Goyal 2013-12-01 11:26:49 +05:30
parent 2f194b2776
commit 06aa59563d
4 changed files with 38 additions and 2 deletions

View File

@ -93,6 +93,7 @@ class Container(object): # {{{
''' '''
book_type = 'oeb' book_type = 'oeb'
SUPPORTS_TITLEPAGES = True
def __init__(self, rootpath, opfpath, log, clone_data=None): 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) 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): class AZW3Container(Container):
book_type = 'azw3' book_type = 'azw3'
SUPPORTS_TITLEPAGES = False
def __init__(self, pathtoazw3, log, clone_data=None, tdir=None): def __init__(self, pathtoazw3, log, clone_data=None, tdir=None):
if clone_data is not None: if clone_data is not None:

View File

@ -148,6 +148,23 @@ def mark_as_cover_epub(container, name):
container.dirty(container.opf_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): def find_cover_page(container):
'Find a document marked as a cover in the OPF' 'Find a document marked as a cover in the OPF'
mm = container.mime_map mm = container.mime_map

View File

@ -19,7 +19,7 @@ from calibre.ptempfile import PersistentTemporaryDirectory
from calibre.ebooks.oeb.base import urlnormalize from calibre.ebooks.oeb.base import urlnormalize
from calibre.ebooks.oeb.polish.main import SUPPORTED, tweak_polish 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.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.pretty import fix_all_html, pretty_all
from calibre.ebooks.oeb.polish.replace import rename_files from calibre.ebooks.oeb.polish.replace import rename_files
from calibre.ebooks.oeb.polish.split import split, merge, AbortError from calibre.ebooks.oeb.polish.split import split, merge, AbortError
@ -78,6 +78,10 @@ class Boss(QObject):
c = current_container() c = current_container()
if action == 'cover': if action == 'cover':
mark_as_cover(current_container(), name) 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: if c.opf_name in editors:
editors[c.opf_name].replace_data(c.raw_data(c.opf_name)) editors[c.opf_name].replace_data(c.raw_data(c.opf_name))

View File

@ -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.container import guess_type, OEB_FONTS
from calibre.ebooks.oeb.polish.cover import ( from calibre.ebooks.oeb.polish.cover import (
get_cover_page_name, get_raster_cover_name, is_raster_image) 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 import current_container, elided_text
from calibre.gui2.tweak_book.editor import syntax_from_mime from calibre.gui2.tweak_book.editor import syntax_from_mime
from calibre.gui2.tweak_book.templates import template_for from calibre.gui2.tweak_book.templates import template_for
@ -309,9 +309,12 @@ class FileList(QTreeWidget):
if ci is not None: if ci is not None:
cn = unicode(ci.data(0, NAME_ROLE).toString()) cn = unicode(ci.data(0, NAME_ROLE).toString())
mt = unicode(ci.data(0, MIME_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) m.addAction(QIcon(I('modified.png')), _('&Rename %s') % (elided_text(self.font(), cn)), self.edit_current_item)
if is_raster_image(mt): 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)) 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) selected_map = defaultdict(list)
for item in sel: for item in sel:
@ -348,6 +351,16 @@ class FileList(QTreeWidget):
def mark_as_cover(self, name): def mark_as_cover(self, name):
self.mark_requested.emit(name, 'cover') 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): def keyPressEvent(self, ev):
if ev.key() in (Qt.Key_Delete, Qt.Key_Backspace): if ev.key() in (Qt.Key_Delete, Qt.Key_Backspace):
ev.accept() ev.accept()