Nicer formatting for generated nav toc

Also handle the case of the nav toc having a link to itself
This commit is contained in:
Kovid Goyal 2016-06-15 20:13:35 +05:30
parent 60c1948130
commit fadba2dfa1

View File

@ -21,7 +21,7 @@ from calibre import __version__
from calibre.ebooks.oeb.base import ( from calibre.ebooks.oeb.base import (
XPath, uuid_id, xml2text, NCX, NCX_NS, XML, XHTML, XHTML_NS, serialize, EPUB_NS) XPath, uuid_id, xml2text, NCX, NCX_NS, XML, XHTML, XHTML_NS, serialize, EPUB_NS)
from calibre.ebooks.oeb.polish.errors import MalformedMarkup from calibre.ebooks.oeb.polish.errors import MalformedMarkup
from calibre.ebooks.oeb.polish.utils import guess_type from calibre.ebooks.oeb.polish.utils import guess_type, extract
from calibre.ebooks.oeb.polish.opf import set_guide_item, get_book_language from calibre.ebooks.oeb.polish.opf import set_guide_item, get_book_language
from calibre.ebooks.oeb.polish.pretty import pretty_html_tree from calibre.ebooks.oeb.polish.pretty import pretty_html_tree
from calibre.translations.dynamic import translate from calibre.translations.dynamic import translate
@ -154,13 +154,13 @@ def parse_ncx(container, ncx_name):
break break
return toc_root return toc_root
def add_from_li(container, li, parent, ncx_name): def add_from_li(container, li, parent, nav_name):
dest = frag = text = None dest = frag = text = None
for x in li.iterchildren(XHTML('a'), XHTML('span')): for x in li.iterchildren(XHTML('a'), XHTML('span')):
text = etree.tostring(x, method='text', encoding=unicode, with_tail=False) or ' '.join('descendant-or-self::*/@title') text = etree.tostring(x, method='text', encoding=unicode, with_tail=False) or ' '.join('descendant-or-self::*/@title')
href = x.get('href') href = x.get('href')
if href: if href:
dest = container.href_to_name(href, base=ncx_name) dest = nav_name if href.startswith('#') else container.href_to_name(href, base=nav_name)
frag = urlparse(href).fragment or None frag = urlparse(href).fragment or None
break break
return parent.add(text or None, dest or None, frag or None) return parent.add(text or None, dest or None, frag or None)
@ -553,7 +553,7 @@ def commit_nav_toc(container, toc, lang=None):
et = '{%s}type' % EPUB_NS et = '{%s}type' % EPUB_NS
navs = [n for n in root.iterdescendants(XHTML('nav')) if n.get(et) == 'toc'] navs = [n for n in root.iterdescendants(XHTML('nav')) if n.get(et) == 'toc']
for x in navs[1:]: for x in navs[1:]:
x.getparent().remove(x) extract(x)
if navs: if navs:
nav = navs[0] nav = navs[0]
tail = nav.tail tail = nav.tail
@ -594,6 +594,10 @@ def commit_nav_toc(container, toc, lang=None):
process_node(ol, child) process_node(ol, child)
process_node(rnode, toc) process_node(rnode, toc)
pretty_xml_tree(rnode) pretty_xml_tree(rnode)
for li in rnode.iterdescendants(XHTML('li')):
if len(li) == 1:
li.text = None
li[0].tail = None
container.replace(tocname, root) container.replace(tocname, root)
def commit_toc(container, toc, lang=None, uid=None): def commit_toc(container, toc, lang=None, uid=None):