fix error on book without right TOC

This commit is contained in:
shirshasa
2020-07-21 15:43:41 +03:00
parent 2c014ae36e
commit 89989b66f4

View File

@@ -594,7 +594,6 @@ class Book:
footnotes.append(content) footnotes.append(content)
self.footnotes = footnotes self.footnotes = footnotes
def _process_images(self): def _process_images(self):
@@ -705,28 +704,29 @@ class Book:
headers_info = [] headers_info = []
header_tags = self.body_tag.find_all(re.compile("^h[1-9]$")) header_tags = self.body_tag.find_all(re.compile("^h[1-9]$"))
headers_outline = [int(re.sub(r"^h", "", tag.name)) for tag in header_tags] headers_outline = [int(re.sub(r"^h", "", tag.name)) for tag in header_tags]
top_level_outline = min(headers_outline) if headers_outline:
top_level_headers = [tag for tag in header_tags top_level_outline = min(headers_outline)
if int(re.sub(r"^h", "", tag.name)) == top_level_outline] top_level_headers = [tag for tag in header_tags
if int(re.sub(r"^h", "", tag.name)) == top_level_outline]
for tag in top_level_headers: for tag in top_level_headers:
if tag.parent.name == "li": if tag.parent.name == "li":
tag.parent.unwrap()
while tag.parent.name == "ol":
tag.parent.unwrap() tag.parent.unwrap()
while tag.parent.name == "ol":
tag.parent.unwrap()
title = tag.text title = tag.text
title = re.sub(r'\s+', ' ', title).strip() title = re.sub(r'\s+', ' ', title).strip()
number = re.match(r'^(?:\.?\d+\.? ?)+', title) number = re.match(r'^(?:\.?\d+\.? ?)+', title)
is_numbered = number is not None is_numbered = number is not None
cleaned_title = self.clean_header_title(tag.text) cleaned_title = self.clean_header_title(tag.text)
is_introduction = cleaned_title.lower() == 'introduction' is_introduction = cleaned_title.lower() == 'introduction'
headers_info.append({ headers_info.append({
'title': cleaned_title, 'title': cleaned_title,
'is_numbered': is_numbered, 'is_numbered': is_numbered,
'is_introduction': is_introduction}) 'is_introduction': is_introduction})
return headers_info return headers_info
@@ -973,10 +973,11 @@ class Book:
# Add is_introduction field to json structure # Add is_introduction field to json structure
# after deleting content before toc, some chapters can be deleted # after deleting content before toc, some chapters can be deleted
same_first_titles = self.top_level_headers[0]['title'] == json_strc[0]['title'] if self.top_level_headers:
is_first_header_introduction = not self.top_level_headers[0]['should_be_numbered'] same_first_titles = self.top_level_headers[0]['title'] == json_strc[0]['title']
is_first_header_introduction = not self.top_level_headers[0]['should_be_numbered']
json_strc[0]['is_introduction'] = is_first_header_introduction and same_first_titles json_strc[0]['is_introduction'] = is_first_header_introduction and same_first_titles
self.content_dict = { self.content_dict = {
"content": json_strc, "content": json_strc,