MOBI Output: Ignore the Table of Contents pointed to by the guide, if it contains no links

This commit is contained in:
Kovid Goyal 2012-03-12 09:18:49 +05:30
parent bc74856b2d
commit 0fb6d7bd59
2 changed files with 21 additions and 7 deletions

View File

@ -1463,9 +1463,17 @@ class TOC(object):
except ValueError: except ValueError:
return 1 return 1
def __str__(self): def get_lines(self, lvl=0):
return 'TOC: %s --> %s'%(self.title, self.href) 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): def to_opf1(self, tour):
for node in self.nodes: for node in self.nodes:

View File

@ -8,7 +8,7 @@ __copyright__ = '2008, Marshall T. Vandegrift <llasram@gmail.com>'
from calibre.ebooks.oeb.base import XML, XHTML, XHTML_NS 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 XHTML_MIME, CSS_MIME
from calibre.ebooks.oeb.base import element from calibre.ebooks.oeb.base import element, XPath
__all__ = ['HTMLTOCAdder'] __all__ = ['HTMLTOCAdder']
@ -62,18 +62,24 @@ class HTMLTOCAdder(object):
return cls(title=opts.toc_title) return cls(title=opts.toc_title)
def __call__(self, oeb, context): def __call__(self, oeb, context):
has_toc = getattr(getattr(oeb, 'toc', False), 'nodes', False)
if 'toc' in oeb.guide: if 'toc' in oeb.guide:
# Ensure toc pointed to in <guide> is in spine # Ensure toc pointed to in <guide> is in spine
from calibre.ebooks.oeb.base import urlnormalize from calibre.ebooks.oeb.base import urlnormalize
href = urlnormalize(oeb.guide['toc'].href) href = urlnormalize(oeb.guide['toc'].href)
if href in oeb.manifest.hrefs: if href in oeb.manifest.hrefs:
item = oeb.manifest.hrefs[href] item = oeb.manifest.hrefs[href]
if (hasattr(item.data, 'xpath') and
XPath('//h:a[@href]')(item.data)):
if oeb.spine.index(item) < 0: if oeb.spine.index(item) < 0:
oeb.spine.add(item, linear=False) oeb.spine.add(item, linear=False)
return return
elif has_toc:
oeb.guide.remove('toc')
else: else:
oeb.guide.remove('toc') oeb.guide.remove('toc')
if not getattr(getattr(oeb, 'toc', False), 'nodes', False): if not has_toc:
return return
oeb.logger.info('Generating in-line TOC...') oeb.logger.info('Generating in-line TOC...')
title = self.title or oeb.translate(DEFAULT_TITLE) title = self.title or oeb.translate(DEFAULT_TITLE)