From 1a3ed93de4cbcc0fdc70eb8149516fb1eb1a960a Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 22 Aug 2018 13:30:17 +0530 Subject: [PATCH] ToC Editor: When generating ToCs using headings/xpath ignore duplicate entries at the same level that have the same text. Fixes #1735799 [[Enhancement] Filter dupes in TOC editor](https://bugs.launchpad.net/calibre/+bug/1735799) --- src/calibre/ebooks/oeb/polish/toc.py | 13 +++++++++++++ src/calibre/gui2/toc/main.py | 1 + 2 files changed, 14 insertions(+) diff --git a/src/calibre/ebooks/oeb/polish/toc.py b/src/calibre/ebooks/oeb/polish/toc.py index 5546481892..b3c8ce3cbe 100644 --- a/src/calibre/ebooks/oeb/polish/toc.py +++ b/src/calibre/ebooks/oeb/polish/toc.py @@ -78,6 +78,19 @@ class TOC(object): for gc in child.iterdescendants(): yield gc + def remove_duplicates(self, only_text=True): + seen = set() + remove = [] + for child in self: + key = child.title if only_text else (child.title, child.dest, (child.frag or None)) + if key in seen: + remove.append(child) + else: + seen.add(key) + child.remove_duplicates() + for child in remove: + self.remove(child) + @property def depth(self): """The maximum depth of the navigation tree rooted at this node.""" diff --git a/src/calibre/gui2/toc/main.py b/src/calibre/gui2/toc/main.py index ace86330ba..b9ae81fecb 100644 --- a/src/calibre/gui2/toc/main.py +++ b/src/calibre/gui2/toc/main.py @@ -933,6 +933,7 @@ class TOCView(QWidget): # {{{ if len(toc) == 0: return error_dialog(self, _('No items found'), _('No items were found that could be added to the Table of Contents.'), show=True) + toc.remove_duplicates() self.insert_toc_fragment(toc) def create_from_links(self):