def _create_body(self, book): """Create body html file with main content of the book. Created html file will be used by booktype2mpdf.php script to create final PDF file. :Args: - book: EPUB Book object """ def _toc(depth, toc_items): items = [] for toc_item in toc_items: if isinstance(toc_item[1], list): section_title, chapters = toc_item items += [{ 'type': 'section', 'level': depth, 'title': section_title, 'url_title': booktype_slugify(section_title), }] items += _toc(depth + 1, chapters) else: chapter_title, chapter_href = toc_item chapter_item = book.get_item_with_href(chapter_href) content = self._get_chapter_content(chapter_item) content = self._fix_horrible_mpdf(content) href_filename, file_extension = os.path.splitext( chapter_href) items.append({ 'type': 'chapter', 'level': depth, 'title': chapter_title, 'url_title': booktype_slugify(chapter_title), 'href': chapter_href, 'href_filename': href_filename, 'content': content }) return items book_toc = _toc(0, parse_toc_nav(book)) data = self._get_data(book) data.update(self.get_extra_data(book)) data.update({'book_items': book_toc}) # if self.theme_name != '': body_name = get_body(self.theme_name, self.name) html = render_to_string(body_name, data) # else: # body_name = 'body_{}.html'.format(self.name) # html = render_to_string('themes/{}'.format(body_name), data) html_path = os.path.join(self.sandbox_path, self._body_html_name) f = codecs.open(html_path, 'wt', 'utf8') f.write(html) f.close()
def _create_body(self, book): """Create body html file with main content of the book. Created html file will be used by booktype2mpdf.php script to create final PDF file. :Args: - book: EPUB Book object """ settings_dict = get_sections_settings(book) def _toc(depth, toc_items, parent=None, toc_setting=None): items = [] sec_count = 1 for toc_item in toc_items: # SECTIONS if isinstance(toc_item[1], list): section_title, chapters = toc_item url_title = booktype_slugify(section_title) # let's build a section key and try to get settings for current section section_key = SectionsSettingsPlugin.build_section_key(url_title, sec_count) section_settings = json.loads(settings_dict.get(section_key, '{}')) toc_setting = section_settings.get('toc', {}).get(self.name, '') # jump to next item (continue) if the whole section should be hidden show_in_outputs = section_settings.get('show_in_outputs', {}) show_section_in_current_converter = show_in_outputs.get(self.name, True) if not show_section_in_current_converter: continue toc_item = TocItem({ 'type': 'section', 'level': depth, 'title': section_title, 'url_title': url_title, 'show_in_toc': 'hide_section' not in toc_setting }) items.append(toc_item) items += _toc(depth + 1, chapters, section_title, toc_setting) sec_count += 1 # CHAPTERS else: chapter_title, chapter_href = toc_item chapter_item = book.get_item_with_href(chapter_href) content = self._get_chapter_content(chapter_item) content = self._fix_horrible_mpdf(content) href_filename, file_extension = os.path.splitext(chapter_href) if not parent: toc_setting = '' toc_item = TocItem({ 'type': 'chapter', 'level': depth, 'title': chapter_title, 'url_title': booktype_slugify(chapter_title), 'href': chapter_href, 'href_filename': href_filename, 'content': content, 'show_in_toc': 'hide_chapters' not in toc_setting }) items.append(toc_item) return items book_toc = _toc(0, self.original_toc_nav) data = self._get_data(book) data.update(self.get_extra_data(book)) data.update({ 'book_items': book_toc }) body_name = get_body(self.theme_name, self.name) html = render_to_string(body_name, data) html_path = os.path.join(self.sandbox_path, self._body_html_name) f = codecs.open(html_path, 'wt', 'utf8') f.write(html) f.close()
def _create_body(self, book): """Create body html file with main content of the book. Created html file will be used by booktype2mpdf.php script to create final PDF file. :Args: - book: EPUB Book object """ def _toc(depth, toc_items): items = [] for toc_item in toc_items: if isinstance(toc_item[1], list): section_title, chapters = toc_item items += [{ 'type': 'section', 'level': depth, 'title': section_title, 'url_title': booktype_slugify(section_title), }] items += _toc(depth + 1, chapters) else: chapter_title, chapter_href = toc_item chapter_item = book.get_item_with_href(chapter_href) content = self._get_chapter_content(chapter_item) content = self._fix_horrible_mpdf(content) href_filename, file_extension = os.path.splitext(chapter_href) items.append({ 'type': 'chapter', 'level': depth, 'title': chapter_title, 'url_title': booktype_slugify(chapter_title), 'href': chapter_href, 'href_filename': href_filename, 'content': content}) return items book_toc = _toc(0, parse_toc_nav(book)) data = self._get_data(book) data.update(self.get_extra_data(book)) data.update({ 'book_items': book_toc }) # if self.theme_name != '': body_name = get_body(self.theme_name, self.name) html = render_to_string(body_name, data) # else: # body_name = 'body_{}.html'.format(self.name) # html = render_to_string('themes/{}'.format(body_name), data) html_path = os.path.join(self.sandbox_path, self._body_html_name) f = codecs.open(html_path, 'wt', 'utf8') f.write(html) f.close()
def _create_body(self, book): """Create body html file with main content of the book. Created html file will be used by booktype2mpdf.php script to create final PDF file. :Args: - book: EPUB Book object """ settings = get_sections_settings(book) def _toc(depth, toc_items, parent=None, toc_setting=None): items = [] sec_count = 1 for toc_item in toc_items: if isinstance(toc_item[1], list): section_title, chapters = toc_item url_title = booktype_slugify(section_title) key = 'section_%s_%s' % (url_title, sec_count) section_settings = json.loads(settings.get(key, '{}')) toc_setting = section_settings.get('toc', {}).get(self.name, '') # continue if the whole section should be hidden show_in_outputs = section_settings.get( 'show_in_outputs', {}) if show_in_outputs.get(self.name, 'true') == 'false': continue items += [{ 'type': 'section', 'level': depth, 'title': section_title, 'url_title': url_title, 'show_in_toc': 'hide_section' not in toc_setting }] items += _toc(depth + 1, chapters, section_title, toc_setting) sec_count += 1 else: chapter_title, chapter_href = toc_item chapter_item = book.get_item_with_href(chapter_href) content = self._get_chapter_content(chapter_item) content = self._fix_horrible_mpdf(content) href_filename, file_extension = os.path.splitext( chapter_href) if not parent: toc_setting = '' items.append({ 'type': 'chapter', 'level': depth, 'title': chapter_title, 'url_title': booktype_slugify(chapter_title), 'href': chapter_href, 'href_filename': href_filename, 'content': content, 'show_in_toc': 'hide_chapters' not in toc_setting }) return items book_toc = _toc(0, self.original_toc_nav) data = self._get_data(book) data.update(self.get_extra_data(book)) data.update({'book_items': book_toc}) body_name = get_body(self.theme_name, self.name) html = render_to_string(body_name, data) html_path = os.path.join(self.sandbox_path, self._body_html_name) f = codecs.open(html_path, 'wt', 'utf8') f.write(html) f.close()