diff --git a/src/epub_converter/html_epub_processor.py b/src/epub_converter/html_epub_processor.py index 40640c1..fd29817 100644 --- a/src/epub_converter/html_epub_processor.py +++ b/src/epub_converter/html_epub_processor.py @@ -99,11 +99,13 @@ class HtmlEpubProcessor: return text title_of_chapter: str = title_of_chapter.lower() - title_in_text: List[Tag] = chapter_tag.find_all(lambda tag: title_of_chapter == text_preparing(tag) or \ - (title_of_chapter in text_preparing(tag) and - re.findall(r"^h[1-3]$", tag.name or chapter_tag.name))) + title_in_text: List[Tag] = chapter_tag.find_all(lambda tag: (title_of_chapter in text_preparing(tag) and + len(text_preparing(tag)) != 0 and + re.findall(r"^h[1-5]$", tag.name or chapter_tag.name))) - text_in_title: List[Tag] = chapter_tag.find_all(lambda tag: (text_preparing(tag) in title_of_chapter)) + text_in_title: List[Tag] = chapter_tag.find_all(lambda tag: (text_preparing(tag) in title_of_chapter) and + len(text_preparing(tag)) != 0 and + re.findall(r"^h[1-5]$", tag.name or chapter_tag.name)) if title_in_text: self.html_preprocessor._add_span_to_save_ids_for_links( title_in_text[-1], chapter_tag) diff --git a/src/html_presets_processor.py b/src/html_presets_processor.py index eeba3ca..cfffe7b 100644 --- a/src/html_presets_processor.py +++ b/src/html_presets_processor.py @@ -27,8 +27,14 @@ class HtmlPresetsProcessor: kwargs["rule"]["tag_to_wrap"])) @staticmethod - def _decompose_tag(**kwargs): - kwargs["tag"].parent.attrs.update(kwargs["tag"].attrs) + def set_attrs_to_parent(tag, parent_tag): + for key in tag.attrs: + if key not in parent_tag.attrs: + parent_tag.attrs[key] = tag.attrs[key] + + def _decompose_tag(self, **kwargs): + if kwargs["tag"].parent: + self.set_attrs_to_parent(kwargs["tag"], kwargs["tag"].parent) kwargs["tag"].decompose() @staticmethod @@ -111,9 +117,9 @@ class HtmlPresetsProcessor: elif attr: del kwargs["tag"][attr] - @staticmethod - def _unwrap_tag(**kwargs): - kwargs["tag"].parent.attrs.update(kwargs["tag"].attrs) + def _unwrap_tag(self, **kwargs): + if kwargs["tag"].parent: + self.set_attrs_to_parent(kwargs["tag"], kwargs["tag"].parent) kwargs["tag"].unwrap() @staticmethod