epub converter: fix headings levels

This commit is contained in:
shirshasa
2021-04-30 15:21:35 +03:00
parent d21b11f99a
commit b472c5b9f7
3 changed files with 49 additions and 23 deletions

View File

@@ -14,6 +14,7 @@ from html_epub_preprocessor import unwrap_structural_tags, get_tags_between_ids,
update_src_links_in_images, preprocess_footnotes
from css_reader import clean_css, add_inline_style_to_html_soup
from livecarta_config import LawCartaConfig
class EpubPostprocessor:
@@ -209,7 +210,7 @@ class EpubPostprocessor:
for point in nav_points:
self.build_one_anchored_section(point)
def node2livecarta_chapter_item(self, node: NavPoint) -> ChapterItem:
def node2livecarta_chapter_item(self, node: NavPoint, lvl=1) -> ChapterItem:
title = node.title
if node.id:
content: BeautifulSoup = self.id_anchor2soup[(node.href, node.id)]
@@ -217,13 +218,16 @@ class EpubPostprocessor:
content: BeautifulSoup = self.href2soup_html[node.href]
update_src_links_in_images(content, self.href2img_bytes, path_to_html=node.href, access=self.access)
title_preprocessed, content_preprocessed = prepare_title_and_content(title, content)
is_chapter = lvl <= LawCartaConfig.SUPPORTED_LEVELS
title_preprocessed, content_preprocessed = prepare_title_and_content(title, content,
remove_title_from_chapter=is_chapter)
sub_nodes = []
# warning! not EpubHtmlItems won;t be added to chapter
if self.adjacency_list.get(node):
for sub_node in self.adjacency_list[node]:
sub_chapter_item = self.node2livecarta_chapter_item(sub_node)
sub_chapter_item = self.node2livecarta_chapter_item(sub_node, lvl+1)
sub_nodes.append(sub_chapter_item)
# print(f'Chapter: {title} is prepared.')