Fix regression in TOC generation when creating EPUB. --toc-threshold now works and TOC entries created from links with only a fragment also work.

This commit is contained in:
Kovid Goyal 2008-11-28 14:42:21 -08:00
parent 5dac2771b1
commit 651d609784
3 changed files with 15 additions and 5 deletions

View File

@ -100,7 +100,7 @@ to auto-generate a Table of Contents.
toc('no_chapters_in_toc', ['--no-chapters-in-toc'], default=False, toc('no_chapters_in_toc', ['--no-chapters-in-toc'], default=False,
help=_("Don't add auto-detected chapters to the Table of Contents.")) help=_("Don't add auto-detected chapters to the Table of Contents."))
toc('toc_threshold', ['--toc-threshold'], default=6, 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, 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.')) 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, toc('level2_toc', ['--level2-toc'], default=None,

View File

@ -641,7 +641,6 @@ class Processor(Parser):
if len(toc) > 0: if len(toc) > 0:
return return
# Add chapters to TOC # Add chapters to TOC
if not self.opts.no_chapters_in_toc: if not self.opts.no_chapters_in_toc:
for elem in getattr(self, 'detected_chapters', []): for elem in getattr(self, 'detected_chapters', []):
text = (u''.join(elem.xpath('string()'))).strip() text = (u''.join(elem.xpath('string()'))).strip()
@ -651,6 +650,8 @@ class Processor(Parser):
elem.set('id', id) elem.set('id', id)
add_item(href, id, text, toc, type='chapter') add_item(href, id, text, toc, type='chapter')
if len(list(toc.flat())) >= self.opts.toc_threshold:
return
referrer = toc referrer = toc
if self.htmlfile.referrer is not None: if self.htmlfile.referrer is not None:
try: try:
@ -668,7 +669,6 @@ class Processor(Parser):
href = 'content/'+name href = 'content/'+name
referrer = add_item(href, None, text, toc) referrer = add_item(href, None, text, toc)
# Add links to TOC # Add links to TOC
if int(self.opts.max_toc_links) > 0: if int(self.opts.max_toc_links) > 0:
for link in list(self.LINKS_PATH(self.root))[:self.opts.max_toc_links]: for link in list(self.LINKS_PATH(self.root))[:self.opts.max_toc_links]:
@ -676,13 +676,15 @@ class Processor(Parser):
if text: if text:
href = link.get('href', '') href = link.get('href', '')
if href and not (href.startswith('http://') or href.startswith('https://')): 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 href = 'content/'+href
parts = href.split('#') parts = href.split('#')
href, fragment = parts[0], None href, fragment = parts[0], None
if len(parts) > 1: if len(parts) > 1:
fragment = parts[1] fragment = parts[1]
add_item(href, fragment, text, referrer) add_item(href, fragment, text, referrer)
@classmethod @classmethod
def preprocess_css(cls, css, dpi=96): def preprocess_css(cls, css, dpi=96):

View File

@ -31,7 +31,15 @@ class TOC(list):
self.base_path = base_path self.base_path = base_path
self.play_order = play_order self.play_order = play_order
self.type = type 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): def count(self, type):
return len([i for i in self.flat() if i.type == type]) return len([i for i in self.flat() if i.type == type])