From 3a22851b83b1114860241033bfc821f4b1a4cdf0 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 20 Nov 2016 23:07:33 +0530 Subject: [PATCH] Edit Book: Check Book: Add a check for nav documents that contain no ToC in EPUB 3 books --- src/calibre/ebooks/oeb/polish/check/opf.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/calibre/ebooks/oeb/polish/check/opf.py b/src/calibre/ebooks/oeb/polish/check/opf.py index d3c83bda5e..de3b3f15b0 100644 --- a/src/calibre/ebooks/oeb/polish/check/opf.py +++ b/src/calibre/ebooks/oeb/polish/check/opf.py @@ -10,7 +10,7 @@ from lxml import etree from calibre import prepare_string_for_xml as xml from calibre.ebooks.oeb.polish.check.base import BaseError, WARN -from calibre.ebooks.oeb.polish.toc import find_existing_nav_toc +from calibre.ebooks.oeb.polish.toc import find_existing_nav_toc, parse_nav from calibre.ebooks.oeb.polish.utils import guess_type from calibre.ebooks.oeb.base import OPF, OPF2_NS, DC, DC11_NS, XHTML_MIME @@ -119,6 +119,16 @@ class MissingNav(BaseError): BaseError.__init__(self, _('Missing navigation document'), name, lnum) +class EmptyNav(BaseError): + + HELP = _('The nav document for this book contains no table of contents, or an empty table of contents.' + ' Use the Table of Contents tool to add a Table of Contents to this book.') + LEVEL = WARN + + def __init__(self, name, lnum): + BaseError.__init__(self, _('Missing ToC in navigation document'), name, lnum) + + class MissingHref(BaseError): HELP = _('A file listed in the manifest is missing, you should either remove' @@ -333,8 +343,13 @@ def check_opf(container): errors.append(MissingNCXRef(container.opf_name, spine.sourceline, ncx_id)) if opf_version.major > 2: - if find_existing_nav_toc(container) is None: + existing_nav = find_existing_nav_toc(container) + if existing_nav is None: errors.append(MissingNav(container.opf_name, 0)) + else: + toc = parse_nav(container, existing_nav) + if len(toc) == 0: + errors.append(EmptyNav(existing_nav, 0)) covers = container.opf_xpath('/opf:package/opf:metadata/opf:meta[@name="cover"]') if len(covers) > 0: