diff --git a/src/calibre/ebooks/oeb/base.py b/src/calibre/ebooks/oeb/base.py index 450d50fd40..bf9287d53a 100644 --- a/src/calibre/ebooks/oeb/base.py +++ b/src/calibre/ebooks/oeb/base.py @@ -1463,9 +1463,17 @@ class TOC(object): except ValueError: return 1 - def __str__(self): - return 'TOC: %s --> %s'%(self.title, self.href) + def get_lines(self, lvl=0): + ans = [(u'\t'*lvl) + u'TOC: %s --> %s'%(self.title, self.href)] + for child in self: + ans.extend(child.get_lines(lvl+1)) + return ans + def __str__(self): + return b'\n'.join([x.encode('utf-8') for x in self.get_lines()]) + + def __unicode__(self): + return u'\n'.join(self.get_lines()) def to_opf1(self, tour): for node in self.nodes: diff --git a/src/calibre/ebooks/oeb/transforms/htmltoc.py b/src/calibre/ebooks/oeb/transforms/htmltoc.py index 9f0fd21754..26b2ccf41c 100644 --- a/src/calibre/ebooks/oeb/transforms/htmltoc.py +++ b/src/calibre/ebooks/oeb/transforms/htmltoc.py @@ -8,7 +8,7 @@ __copyright__ = '2008, Marshall T. Vandegrift ' from calibre.ebooks.oeb.base import XML, XHTML, XHTML_NS from calibre.ebooks.oeb.base import XHTML_MIME, CSS_MIME -from calibre.ebooks.oeb.base import element +from calibre.ebooks.oeb.base import element, XPath __all__ = ['HTMLTOCAdder'] @@ -62,18 +62,24 @@ class HTMLTOCAdder(object): return cls(title=opts.toc_title) def __call__(self, oeb, context): + has_toc = getattr(getattr(oeb, 'toc', False), 'nodes', False) + if 'toc' in oeb.guide: # Ensure toc pointed to in is in spine from calibre.ebooks.oeb.base import urlnormalize href = urlnormalize(oeb.guide['toc'].href) if href in oeb.manifest.hrefs: item = oeb.manifest.hrefs[href] - if oeb.spine.index(item) < 0: - oeb.spine.add(item, linear=False) - return + if (hasattr(item.data, 'xpath') and + XPath('//h:a[@href]')(item.data)): + if oeb.spine.index(item) < 0: + oeb.spine.add(item, linear=False) + return + elif has_toc: + oeb.guide.remove('toc') else: oeb.guide.remove('toc') - if not getattr(getattr(oeb, 'toc', False), 'nodes', False): + if not has_toc: return oeb.logger.info('Generating in-line TOC...') title = self.title or oeb.translate(DEFAULT_TITLE)