From 89989b66f4f054d0d22bc4792c8b9737743bf56f Mon Sep 17 00:00:00 2001 From: shirshasa Date: Tue, 21 Jul 2020 15:43:41 +0300 Subject: [PATCH] fix error on book without right TOC --- src/book.py | 43 ++++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/src/book.py b/src/book.py index ccc7b55..0707cc3 100644 --- a/src/book.py +++ b/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,