Edit book: Upgrade book: Ask whether to keep the NCX based Table of Contents. Fixes #1287 (Add an option to remove NCX TOC file when upgrading book internals)

This commit is contained in:
Kovid Goyal 2020-11-15 20:11:27 +05:30
parent 687dc10cfa
commit 8e44992888
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 18 additions and 6 deletions

View File

@ -44,6 +44,7 @@ CUSTOMIZATION = {
'remove_unused_classes': False,
'merge_identical_selectors': False,
'merge_rules_with_identical_properties': False,
'remove_ncx': True,
}
SUPPORTED = {'EPUB', 'AZW3'}
@ -250,7 +251,7 @@ def polish_one(ebook, opts, report, customization=None):
if opts.upgrade_book:
rt(_('Upgrading book, if possible'))
if upgrade_book(ebook, report):
if upgrade_book(ebook, report, remove_ncx=customization['remove_ncx']):
changed = True
report('')

View File

@ -114,12 +114,12 @@ def create_nav(container, toc, landmarks, previous_nav=None):
commit_nav_toc(container, toc, lang=lang, landmarks=landmarks, previous_nav=previous_nav)
def epub_2_to_3(container, report, previous_nav=None):
def epub_2_to_3(container, report, previous_nav=None, remove_ncx=True):
upgrade_metadata(container.opf)
collect_properties(container)
toc = get_toc(container)
toc_name = find_existing_ncx_toc(container)
if toc_name:
if toc_name and remove_ncx:
container.remove_item(toc_name)
container.opf_xpath('./opf:spine')[0].attrib.pop('toc', None)
landmarks = get_landmarks(container)
@ -132,11 +132,11 @@ def epub_2_to_3(container, report, previous_nav=None):
container.dirty(container.opf_name)
def upgrade_book(container, report):
def upgrade_book(container, report, remove_ncx=True):
if container.book_type != 'epub' or container.opf_version_parsed.major >= 3:
report(_('No upgrade needed'))
return False
epub_2_to_3(container, report)
epub_2_to_3(container, report, remove_ncx=remove_ncx)
report(_('Updated EPUB from version 2 to 3'))
return True

View File

@ -17,7 +17,7 @@ from PyQt5.Qt import (
from calibre import human_readable, fit_image, force_unicode
from calibre.ebooks.oeb.polish.main import CUSTOMIZATION
from calibre.gui2 import empty_index
from calibre.gui2 import empty_index, question_dialog
from calibre.gui2.tweak_book import tprefs, current_container, set_current_container
from calibre.gui2.tweak_book.widgets import Dialog
from calibre.utils.icu import numeric_sort_key
@ -78,6 +78,17 @@ def get_customization(action, name, parent):
try:
if action == 'remove_unused_css':
customize_remove_unused_css(name, parent, ans)
elif action == 'upgrade_book':
ans['remove_ncx'] = question_dialog(
parent, _('Remove NCX ToC file'),
_('Remove the legacy Table of Contents in NCX form?'),
_('This form of Table of Contents is superseded by the new HTML based Table of Contents.'
' Leaving it behind is useful only if you expect this book to be read on very'
' old devices that lack proper support for EPUB 3'),
skip_dialog_name='edit-book-remove-ncx',
skip_dialog_msg=_('Ask this question again in the future'),
yes_text=_('Remove NCX'), no_text=_('Keep NCX')
)
except Abort:
return None
return ans