diff --git a/src/calibre/ebooks/epub/__init__.py b/src/calibre/ebooks/epub/__init__.py index 5ef305466d..a64dd43ac3 100644 --- a/src/calibre/ebooks/epub/__init__.py +++ b/src/calibre/ebooks/epub/__init__.py @@ -100,7 +100,7 @@ to auto-generate a Table of Contents. toc('no_chapters_in_toc', ['--no-chapters-in-toc'], default=False, help=_("Don't add auto-detected chapters to the Table of Contents.")) toc('toc_threshold', ['--toc-threshold'], default=6, - help=_('If fewer than this number of chapters is detected, then links are added to the Table of Contents.')) + help=_('If fewer than this number of chapters is detected, then links are added to the Table of Contents. Default: %default')) toc('level1_toc', ['--level1-toc'], default=None, help=_('XPath expression that specifies all tags that should be added to the Table of Contents at level one. If this is specified, it takes precedence over other forms of auto-detection.')) toc('level2_toc', ['--level2-toc'], default=None, diff --git a/src/calibre/ebooks/html.py b/src/calibre/ebooks/html.py index 063317a8ce..c811982184 100644 --- a/src/calibre/ebooks/html.py +++ b/src/calibre/ebooks/html.py @@ -641,7 +641,6 @@ class Processor(Parser): if len(toc) > 0: return # Add chapters to TOC - if not self.opts.no_chapters_in_toc: for elem in getattr(self, 'detected_chapters', []): text = (u''.join(elem.xpath('string()'))).strip() @@ -651,6 +650,8 @@ class Processor(Parser): elem.set('id', id) add_item(href, id, text, toc, type='chapter') + if len(list(toc.flat())) >= self.opts.toc_threshold: + return referrer = toc if self.htmlfile.referrer is not None: try: @@ -668,7 +669,6 @@ class Processor(Parser): href = 'content/'+name referrer = add_item(href, None, text, toc) - # Add links to TOC if int(self.opts.max_toc_links) > 0: for link in list(self.LINKS_PATH(self.root))[:self.opts.max_toc_links]: @@ -676,13 +676,15 @@ class Processor(Parser): if text: href = link.get('href', '') if href and not (href.startswith('http://') or href.startswith('https://')): + href = href.strip() + if href.startswith('#'): + href = self.htmlfile_map[self.htmlfile.path] + href href = 'content/'+href parts = href.split('#') href, fragment = parts[0], None if len(parts) > 1: fragment = parts[1] add_item(href, fragment, text, referrer) - @classmethod def preprocess_css(cls, css, dpi=96): diff --git a/src/calibre/ebooks/metadata/toc.py b/src/calibre/ebooks/metadata/toc.py index f50a525264..e4f9161ebb 100644 --- a/src/calibre/ebooks/metadata/toc.py +++ b/src/calibre/ebooks/metadata/toc.py @@ -31,7 +31,15 @@ class TOC(list): self.base_path = base_path self.play_order = play_order self.type = type - + + def __str__(self): + lines = ['TOC: %s#%s'%(self.href, self.fragment)] + for child in self: + c = str(child).splitlines() + for l in c: + lines.append('\t'+l) + return '\n'.join(lines) + def count(self, type): return len([i for i in self.flat() if i.type == type])