def _clean_book_items(self): """ Removes the items that are not supposed to be shown according to section settings """ output_name = self.convert.name settings = get_sections_settings(self.original_book) count = 1 # mark chapters and sections to be removed (if any) for toc_item in parse_toc_nav(self.original_book): if isinstance(toc_item[1], list): section_title, chapters = toc_item key = self.build_section_key(section_title, count) section_settings = json.loads(settings.get(key, '{}')) show_in_outputs = section_settings.get('show_in_outputs', {}) # means to remove the chapters that belongs to this section if not show_in_outputs.get(output_name, True): self.chapters_to_remove += [x[1] for x in chapters] self.sections_to_remove.append(key) # increment if a section is found count += 1 # now let's loop over all the book items and exclude the ones are marked to be removed new_items = [] for i, item in enumerate(list(self.original_book.items)): if item.get_name() not in self.chapters_to_remove: new_items.append(item) self.original_book.items = new_items
def _clean_book_items(self): """ Removes the items that are not supposed to be shown according to section settings """ output_name = self.convert.name settings = get_sections_settings(self.original_book) count = 1 for toc_item in parse_toc_nav(self.original_book): if isinstance(toc_item[1], list): section_title, chapters = toc_item key = self._get_section_key(section_title, count) section_settings = json.loads(settings.get(key, '{}')) show_in_outputs = section_settings.get('show_in_outputs', {}) # means to remove the chapters that belongs to this section if show_in_outputs.get(output_name, 'true') == 'false': self.chapters_to_remove += [x[1] for x in chapters] self.sections_to_remove.append(key) # increment if a section is found count += 1 new_items = [] for i, item in enumerate(list(self.original_book.items)): if item.get_name() not in self.chapters_to_remove: new_items.append(item) self.original_book.items = new_items
def _clean_book_items(self): """ Removes the items that are not supposed to be shown according to section settings """ output_name = self.convert.name settings = get_sections_settings(self.original_book) count = 1 for toc_item in parse_toc_nav(self.original_book): if isinstance(toc_item[1], list): section_title, chapters = toc_item key = self._get_section_key(section_title, count) section_settings = json.loads(settings.get(key, "{}")) show_in_outputs = section_settings.get("show_in_outputs", {}) # means to remove the chapters that belongs to this section if show_in_outputs.get(output_name, "true") == "false": self.chapters_to_remove += [x[1] for x in chapters] self.sections_to_remove.append(key) # increment if a section is found count += 1 new_items = [] for i, item in enumerate(list(self.original_book.items)): if item.get_name() not in self.chapters_to_remove: new_items.append(item) self.original_book.items = new_items
def import_based_on_epub(epub_file, book_dest): """ It will import an epub file into a existent book on the system. This will also try to import sections settings and stuff Keyword arguments: epub_file -- EPUB file to be imported into book_dest book_dest -- Destiny book TODO: add docstrings of return info """ notifier = CollectNotifier() delegate = Delegate() epub_importer = EpubImporter() epub_importer.notifier = notifier epub_importer.delegate = delegate result = {} try: epub_book = epub_importer.import_file(epub_file, book_dest) except Exception as e: epub_book = None logger.error('ImporterView::Some kind of error while importing book.') logger.exception(e) notifier.errors.append(str(e)) # let's try to save sections settings if epub_book is not None: settings_dict = get_sections_settings(epub_book) book_dest_version = book_dest.get_version(None) sec_count = 1 for toc_item in book_dest_version.get_toc(): if toc_item.is_section(): url_title = booktype_slugify(toc_item.name) section_key = SectionsSettingsPlugin.build_section_key( url_title, sec_count) section_settings = settings_dict.get(section_key, None) if section_settings is not None: toc_item.settings = section_settings toc_item.save() sec_count += 1 result['infos'] = notifier.infos result['warnings'] = notifier.warnings result['errors'] = notifier.errors return result
def _fix_nav_content(self): """Just fixes the nav content according to the sections to be removed""" output_name = self.convert.name settings = get_sections_settings(self.original_book) nav_item = next((item for item in self.original_book.items if isinstance(item, ebooklib.epub.EpubNav)), None) if nav_item: html_node = ebooklib.utils.parse_html_string(nav_item.content) nav_node = html_node.xpath('//nav[@*="toc"]')[0] list_node = nav_node.find('ol') # loop over sections element, they should be in the same order as # they were in parse_toc_nav(original_book) count = 1 for item_node in list_node.findall('li'): sublist_node = item_node.find('ol') if sublist_node is not None: section_name = item_node[0].text section_key = self.build_section_key(section_name, count) section_settings = json.loads( settings.get(section_key, '{}')) toc_setting = section_settings.get('toc', {}).get( output_name, '') mark_section_as = section_settings.get( 'mark_section_as', None) # check if custom mark was given if mark_section_as == 'custom': mark_section_as = section_settings.get( 'custom_mark', None) if mark_section_as and section_key not in self.sections_to_remove: for item in sublist_node.iterchildren('li'): chapter_href = item[0].get('href') item = self.original_book.get_item_with_href( chapter_href) item.content = self._mark_chapter_content( item.content, mark_section_as) # if whole section is hidden, we should also remove # the whole entry in the TOC in the EpubNav file # cause why to show the toc entry if section content is hidden? :) if section_key in self.sections_to_remove: item_node.drop_tree() else: if toc_setting == TocSettings.SHOW_SECTION_SHOW_CHAPTERS: pass # nothing to do here :) elif toc_setting == TocSettings.HIDE_SECTION_SHOW_CHAPTERS: # removing section label/title section_label = item_node[0] item_node.remove(section_label) parent = item_node.getparent() index = parent.index(item_node) for child in sublist_node.iterchildren('li'): parent.insert(index, child) index += 1 # now removing the empty sublist_node sublist_node.getparent().remove(sublist_node) elif toc_setting == TocSettings.SHOW_SECTION_HIDE_CHAPTERS: # section name should point to first chapter under it # otherwise it doesn't make sense to show just the label # AND because we have a filter to remove empty sections :) parent = item_node.getparent() index = parent.index(item_node) if len(sublist_node) > 0: section_label = item_node[0] item_node.remove(section_label) child = sublist_node[0] child[0].text = section_label.text parent.insert(index, child) sublist_node.getparent().remove(sublist_node) elif toc_setting == TocSettings.HIDE_SECTION_HIDE_CHAPTERS: item_node.drop_tree() # increment if a section is found count += 1 nav_item.content = etree.tostring(html_node, pretty_print=True, encoding='utf-8', xml_declaration=True)
def _fix_nav_content(self): """Just fixes the nav content according to the sections to be removed""" output_name = self.convert.name settings = get_sections_settings(self.original_book) nav_item = next((item for item in self.original_book.items if isinstance(item, ebooklib.epub.EpubNav)), None) if nav_item: html_node = ebooklib.utils.parse_html_string(nav_item.content) nav_node = html_node.xpath('//nav[@*="toc"]')[0] list_node = nav_node.find("ol") # loop over sections element, they should be in the same order as # they were in parse_toc_nav(original_book) count = 1 for item_node in list_node.findall("li"): sublist_node = item_node.find("ol") if sublist_node is not None: section_name = item_node[0].text key = self._get_section_key(section_name, count) section_settings = json.loads(settings.get(key, "{}")) toc_setting = section_settings.get("toc", {}).get(output_name, "") mark_section_as = section_settings.get("mark_section_as", None) # check if custom mark was given if mark_section_as == "custom": mark_section_as = section_settings.get("custom_mark", None) if mark_section_as and key not in self.sections_to_remove: for item in sublist_node.iterchildren("li"): chapter_href = item[0].get("href") item = self.original_book.get_item_with_href(chapter_href) item.content = self._mark_chapter_content(item.content, mark_section_as) # if whole section is hidden, we should also remove # the whole entry in the TOC in the EpubNav file # cause why to show the toc entry if section content is hidden? :) if key in self.sections_to_remove: item_node.drop_tree() else: if toc_setting == "show_section_show_chapters": pass # nothing to do here :) elif toc_setting == "hide_section_show_chapters": # removing section label/title section_label = item_node[0] item_node.remove(section_label) parent = item_node.getparent() index = parent.index(item_node) for child in sublist_node.iterchildren("li"): parent.insert(index, child) index += 1 # now removing the empty sublist_node sublist_node.getparent().remove(sublist_node) elif toc_setting == "show_section_hide_chapters": # section name should point to first chapter under it # otherwise it doesn't make sense to show just the label # AND because we have a filter to remove empty sections :) # chaps = sublist_node.find('li') # href = chaps[0].get('href') if len(chaps) > 0 else "#" # # item_node[0].tag = 'a' # item_node[0].set('href', href) # sublist_node.getparent().remove(sublist_node) for chap in sublist_node.iterchildren("li"): chap.getparent().remove(chap) elif toc_setting == "hide_section_hide_chapters": item_node.drop_tree() # increment if a section is found count += 1 nav_item.content = etree.tostring(html_node, pretty_print=True, encoding="utf-8", xml_declaration=True)
def _fix_nav_content(self): """Just fixes the nav content according to the sections to be removed""" output_name = self.convert.name settings = get_sections_settings(self.original_book) nav_item = next((item for item in self.original_book.items if isinstance(item, ebooklib.epub.EpubNav)), None) if nav_item: html_node = ebooklib.utils.parse_html_string(nav_item.content) nav_node = html_node.xpath('//nav[@*="toc"]')[0] list_node = nav_node.find('ol') # loop over sections element, they should be in the same order as # they were in parse_toc_nav(original_book) count = 1 for item_node in list_node.findall('li'): sublist_node = item_node.find('ol') if sublist_node is not None: section_name = item_node[0].text section_key = self.build_section_key(section_name, count) section_settings = json.loads(settings.get(section_key, '{}')) toc_setting = section_settings.get('toc', {}).get(output_name, '') mark_section_as = section_settings.get('mark_section_as', None) # check if custom mark was given if mark_section_as == 'custom': mark_section_as = section_settings.get('custom_mark', None) if mark_section_as and section_key not in self.sections_to_remove: for item in sublist_node.iterchildren('li'): chapter_href = item[0].get('href') item = self.original_book.get_item_with_href(chapter_href) item.content = self._mark_chapter_content(item.content, mark_section_as) # if whole section is hidden, we should also remove # the whole entry in the TOC in the EpubNav file # cause why to show the toc entry if section content is hidden? :) if section_key in self.sections_to_remove: item_node.drop_tree() else: if toc_setting == TocSettings.SHOW_SECTION_SHOW_CHAPTERS: pass # nothing to do here :) elif toc_setting == TocSettings.HIDE_SECTION_SHOW_CHAPTERS: # removing section label/title section_label = item_node[0] item_node.remove(section_label) parent = item_node.getparent() index = parent.index(item_node) for child in sublist_node.iterchildren('li'): parent.insert(index, child) index += 1 # now removing the empty sublist_node sublist_node.getparent().remove(sublist_node) elif toc_setting == TocSettings.SHOW_SECTION_HIDE_CHAPTERS: # section name should point to first chapter under it # otherwise it doesn't make sense to show just the label # AND because we have a filter to remove empty sections :) parent = item_node.getparent() index = parent.index(item_node) if len(sublist_node) > 0: section_label = item_node[0] item_node.remove(section_label) child = sublist_node[0] child[0].text = section_label.text parent.insert(index, child) sublist_node.getparent().remove(sublist_node) elif toc_setting == TocSettings.HIDE_SECTION_HIDE_CHAPTERS: item_node.drop_tree() # increment if a section is found count += 1 nav_item.content = etree.tostring( html_node, pretty_print=True, encoding='utf-8', xml_declaration=True)