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):