diff --git a/src/calibre/ebooks/oeb/polish/toc.py b/src/calibre/ebooks/oeb/polish/toc.py index 1da902a1b7..7f119b2556 100644 --- a/src/calibre/ebooks/oeb/polish/toc.py +++ b/src/calibre/ebooks/oeb/polish/toc.py @@ -45,6 +45,16 @@ class TOC(object): self.children.remove(child) child.parent = None + def remove_from_parent(self): + if self.parent is None: + return + idx = self.parent.children.index(self) + for child in reversed(self.children): + child.parent = self.parent + self.parent.children.insert(idx, child) + self.parent.children.remove(self) + self.parent = None + def __iter__(self): for c in self.children: yield c @@ -407,3 +417,18 @@ def commit_toc(container, toc, lang=None, uid=None): container.replace(tocname, root) container.pretty_print.add(tocname) +def remove_names_from_toc(container, names): + toc = get_toc(container) + if len(toc) == 0: + return False + remove = [] + names = frozenset(names) + for node in toc.iterdescendants(): + if node.dest in names: + remove.append(node) + if remove: + for node in reversed(remove): + node.remove_from_parent() + commit_toc(container, toc) + return True + return False diff --git a/src/calibre/gui2/tweak_book/boss.py b/src/calibre/gui2/tweak_book/boss.py index 1502e3de4e..eca91db1e6 100644 --- a/src/calibre/gui2/tweak_book/boss.py +++ b/src/calibre/gui2/tweak_book/boss.py @@ -23,6 +23,7 @@ from calibre.ebooks.oeb.polish.cover import mark_as_cover, mark_as_titlepage from calibre.ebooks.oeb.polish.pretty import fix_all_html, pretty_all from calibre.ebooks.oeb.polish.replace import rename_files, replace_file from calibre.ebooks.oeb.polish.split import split, merge, AbortError +from calibre.ebooks.oeb.polish.toc import remove_names_from_toc from calibre.gui2 import error_dialog, choose_files, question_dialog, info_dialog, choose_save_file from calibre.gui2.dialogs.confirm_delete import confirm from calibre.gui2.tweak_book import set_current_container, current_container, tprefs, actions, editors @@ -257,11 +258,14 @@ class Boss(QObject): c.remove_item(name) self.set_modified() self.gui.file_list.delete_done(spine_items, other_items) - for name in [x for x, remove in spine_items if remove] + list(other_items): + spine_names = [x for x, remove in spine_items if remove] + for name in spine_names + list(other_items): if name in editors: self.close_editor(name) if not editors: self.gui.preview.clear() + if remove_names_from_toc(current_container(), spine_names + list(other_items)): + self.gui.toc_view.update_if_visible() def commit_dirty_opf(self): c = current_container()