Only use PDF outline if it has at least 3 items

This commit is contained in:
Kovid Goyal 2015-12-10 19:18:01 +05:30
parent 090f8e4e5f
commit 9ab8581d66

View File

@ -128,6 +128,7 @@ def parse_outline(raw, output_dir):
from calibre.ebooks.oeb.polish.toc import TOC, create_ncx from calibre.ebooks.oeb.polish.toc import TOC, create_ncx
outline = outline[0] outline = outline[0]
toc = TOC() toc = TOC()
count = [0]
def process_node(node, toc): def process_node(node, toc):
for child in node.iterdescendants('*'): for child in node.iterdescendants('*'):
@ -137,7 +138,9 @@ def parse_outline(raw, output_dir):
else: else:
page = child.get('page', '1') page = child.get('page', '1')
toc.add(child.text, 'index.html', page) toc.add(child.text, 'index.html', page)
count[0] += 1
process_node(outline, toc) process_node(outline, toc)
if count[0] > 2:
root = create_ncx(toc, (lambda x:x), 'pdftohtml', 'en', 'pdftohtml') root = create_ncx(toc, (lambda x:x), 'pdftohtml', 'en', 'pdftohtml')
with open(os.path.join(output_dir, 'toc.ncx'), 'wb') as f: with open(os.path.join(output_dir, 'toc.ncx'), 'wb') as f:
f.write(etree.tostring(root, pretty_print=True, with_tail=False, encoding='utf-8', xml_declaration=True)) f.write(etree.tostring(root, pretty_print=True, with_tail=False, encoding='utf-8', xml_declaration=True))