From 1acc8f0c730d6701bb4e00598faa93992f241adf Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 6 Jul 2011 12:10:08 -0600 Subject: [PATCH] Conversion pipeline: Add option to control if duplicate entries are allowed when generating the Table of Contents from links. Fixes #806095 ([Enhancement] Preserve duplicate chapter names in TOC) --- src/calibre/ebooks/conversion/cli.py | 2 +- src/calibre/ebooks/conversion/plumber.py | 8 ++++++ .../ebooks/oeb/transforms/structure.py | 18 +++++++------ src/calibre/gui2/convert/toc.py | 2 +- src/calibre/gui2/convert/toc.ui | 27 ++++++++++++------- 5 files changed, 37 insertions(+), 20 deletions(-) diff --git a/src/calibre/ebooks/conversion/cli.py b/src/calibre/ebooks/conversion/cli.py index 1767019972..6527dfb855 100644 --- a/src/calibre/ebooks/conversion/cli.py +++ b/src/calibre/ebooks/conversion/cli.py @@ -176,7 +176,7 @@ def add_pipeline_options(parser, plumber): [ 'level1_toc', 'level2_toc', 'level3_toc', 'toc_threshold', 'max_toc_links', 'no_chapters_in_toc', - 'use_auto_toc', 'toc_filter', + 'use_auto_toc', 'toc_filter', 'duplicate_links_in_toc', ] ), diff --git a/src/calibre/ebooks/conversion/plumber.py b/src/calibre/ebooks/conversion/plumber.py index 9ec474e60f..d0d427bf74 100644 --- a/src/calibre/ebooks/conversion/plumber.py +++ b/src/calibre/ebooks/conversion/plumber.py @@ -265,6 +265,14 @@ OptionRecommendation(name='toc_filter', ) ), +OptionRecommendation(name='duplicate_links_in_toc', + recommended_value=False, level=OptionRecommendation.LOW, + help=_('When creating a TOC from links in the input document, ' + 'allow duplicate entries, i.e. allow more than one entry ' + 'with the same text, provided that they point to a ' + 'different location.') + ), + OptionRecommendation(name='chapter', recommended_value="//*[((name()='h1' or name()='h2') and " diff --git a/src/calibre/ebooks/oeb/transforms/structure.py b/src/calibre/ebooks/oeb/transforms/structure.py index 613429c3ec..f2a61ba6e1 100644 --- a/src/calibre/ebooks/oeb/transforms/structure.py +++ b/src/calibre/ebooks/oeb/transforms/structure.py @@ -121,14 +121,16 @@ class DetectStructure(object): if not self.oeb.toc.has_href(href): text = xml2text(a) text = text[:100].strip() - if not self.oeb.toc.has_text(text): - num += 1 - self.oeb.toc.add(text, href, - play_order=self.oeb.toc.next_play_order()) - if self.opts.max_toc_links > 0 and \ - num >= self.opts.max_toc_links: - self.log('Maximum TOC links reached, stopping.') - return + if (not self.opts.duplicate_links_in_toc and + self.oeb.toc.has_text(text)): + continue + num += 1 + self.oeb.toc.add(text, href, + play_order=self.oeb.toc.next_play_order()) + if self.opts.max_toc_links > 0 and \ + num >= self.opts.max_toc_links: + self.log('Maximum TOC links reached, stopping.') + return diff --git a/src/calibre/gui2/convert/toc.py b/src/calibre/gui2/convert/toc.py index 8121f23192..dab7559f1b 100644 --- a/src/calibre/gui2/convert/toc.py +++ b/src/calibre/gui2/convert/toc.py @@ -22,7 +22,7 @@ class TOCWidget(Widget, Ui_Form): Widget.__init__(self, parent, ['level1_toc', 'level2_toc', 'level3_toc', 'toc_threshold', 'max_toc_links', 'no_chapters_in_toc', - 'use_auto_toc', 'toc_filter', + 'use_auto_toc', 'toc_filter', 'duplicate_links_in_toc', ] ) self.db, self.book_id = db, book_id diff --git a/src/calibre/gui2/convert/toc.ui b/src/calibre/gui2/convert/toc.ui index 342e44f5d8..da15551f80 100644 --- a/src/calibre/gui2/convert/toc.ui +++ b/src/calibre/gui2/convert/toc.ui @@ -21,7 +21,7 @@ - + Number of &links to add to Table of Contents @@ -31,14 +31,14 @@ - + 10000 - + Chapter &threshold @@ -48,7 +48,7 @@ - + @@ -58,7 +58,7 @@ - + TOC &Filter: @@ -68,19 +68,19 @@ - + - + - + - + - + Qt::Vertical @@ -93,6 +93,13 @@ + + + + Allow &duplicate links when creating the Table of Contents + + +