forked from LiveCarta/BookConverter
fix error on book without right TOC
This commit is contained in:
43
src/book.py
43
src/book.py
@@ -594,7 +594,6 @@ class Book:
|
||||
|
||||
footnotes.append(content)
|
||||
|
||||
|
||||
self.footnotes = footnotes
|
||||
|
||||
def _process_images(self):
|
||||
@@ -705,28 +704,29 @@ class Book:
|
||||
headers_info = []
|
||||
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]
|
||||
top_level_outline = min(headers_outline)
|
||||
top_level_headers = [tag for tag in header_tags
|
||||
if int(re.sub(r"^h", "", tag.name)) == top_level_outline]
|
||||
if headers_outline:
|
||||
top_level_outline = min(headers_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:
|
||||
if tag.parent.name == "li":
|
||||
tag.parent.unwrap()
|
||||
while tag.parent.name == "ol":
|
||||
for tag in top_level_headers:
|
||||
if tag.parent.name == "li":
|
||||
tag.parent.unwrap()
|
||||
while tag.parent.name == "ol":
|
||||
tag.parent.unwrap()
|
||||
|
||||
title = tag.text
|
||||
title = re.sub(r'\s+', ' ', title).strip()
|
||||
number = re.match(r'^(?:\.?\d+\.? ?)+', title)
|
||||
is_numbered = number is not None
|
||||
title = tag.text
|
||||
title = re.sub(r'\s+', ' ', title).strip()
|
||||
number = re.match(r'^(?:\.?\d+\.? ?)+', title)
|
||||
is_numbered = number is not None
|
||||
|
||||
cleaned_title = self.clean_header_title(tag.text)
|
||||
is_introduction = cleaned_title.lower() == 'introduction'
|
||||
cleaned_title = self.clean_header_title(tag.text)
|
||||
is_introduction = cleaned_title.lower() == 'introduction'
|
||||
|
||||
headers_info.append({
|
||||
'title': cleaned_title,
|
||||
'is_numbered': is_numbered,
|
||||
'is_introduction': is_introduction})
|
||||
headers_info.append({
|
||||
'title': cleaned_title,
|
||||
'is_numbered': is_numbered,
|
||||
'is_introduction': is_introduction})
|
||||
|
||||
return headers_info
|
||||
|
||||
@@ -973,10 +973,11 @@ class Book:
|
||||
|
||||
# Add is_introduction field to json structure
|
||||
# after deleting content before toc, some chapters can be deleted
|
||||
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']
|
||||
if self.top_level_headers:
|
||||
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 = {
|
||||
"content": json_strc,
|
||||
|
||||
Reference in New Issue
Block a user