From ca229dc6b792a4db5ebeb45303287fabbbe5f29a Mon Sep 17 00:00:00 2001 From: Kiryl Date: Thu, 14 Jul 2022 19:11:33 +0300 Subject: [PATCH] Rewrite _insert_tags cause of mutable tag --- src/epub_converter/html_epub_processor.py | 24 +++++++++++------------ 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/src/epub_converter/html_epub_processor.py b/src/epub_converter/html_epub_processor.py index 8193285..7a6e476 100644 --- a/src/epub_converter/html_epub_processor.py +++ b/src/epub_converter/html_epub_processor.py @@ -123,24 +123,22 @@ class HtmlEpubPreprocessor: """ - def _wrap_tag_with_table(chapter_tag, tag_to_be_wrapped, width="100", border="", bg_color=None): + def _wrap_tag_with_table(width="100", border="", bg_color=None): table = chapter_tag.new_tag("table") table.attrs["border"], table.attrs["align"], table.attrs["style"] \ = border, "center", f"width:{width}%;" tbody, tr, td = \ chapter_tag.new_tag("tbody"), chapter_tag.new_tag("tr"), chapter_tag.new_tag("td") td.attrs["bgcolor"] = bg_color - tag_to_be_wrapped.wrap(td) + tag_to_wrap.wrap(td) td.wrap(tr) tr.wrap(tbody) tbody.wrap(table) table.insert_after(BeautifulSoup(features="lxml").new_tag("br")) return table - def process_tag_using_table(tag_to_wrap): + def process_tag_using_table(): _wrap_tag_with_table( - chapter_tag, - tag_to_be_wrapped=tag_to_wrap, width=tag_to_wrap.attrs["width"] if tag_to_wrap.attrs.get("width") else "100", border=tag_to_wrap.attrs["border"] if tag_to_wrap.attrs.get("border") else None, bg_color=tag_to_wrap.attrs["bgcolor"] if tag_to_wrap.attrs.get("bgcolor") else None) @@ -152,7 +150,7 @@ class HtmlEpubPreprocessor: for attr in rule["attrs"]: for tag_to_wrap in chapter_tag.find_all([re.compile(tag) for tag in tags], {attr["name"]: re.compile(fr"{attr['value']}")}): - process_tag_using_table(tag_to_wrap) + process_tag_using_table() @staticmethod def _tags_to_correspond_livecarta_tag(chapter_tag, rules: list): @@ -229,7 +227,9 @@ class HtmlEpubPreprocessor: Chapter Tag with inserted tags """ - def insert(tag, tag_to_insert): + def insert(tag): + tag_to_insert = \ + chapter_tag.new_tag(rule["tag_to_insert"]) # insert all items that was in tag to subtag and remove from tag for content in reversed(tag.contents): tag_to_insert.insert(0, content.extract()) @@ -238,26 +238,24 @@ class HtmlEpubPreprocessor: for rule in rules: tags = rule["tags"] - tag_to_insert = \ - chapter_tag.new_tag(rule["tag_to_insert"]) if rule["condition"]: for condition_on_tag in ((k, v) for k, v in rule["condition"].items() if v): if condition_on_tag[0] == 'parent_tags': for tag in chapter_tag.find_all([re.compile(tag) for tag in tags]): if tag.parent.select(condition_on_tag[1]): - insert(tag, tag_to_insert) + insert(tag) elif condition_on_tag[0] == 'child_tags': for tag in chapter_tag.find_all([re.compile(tag) for tag in tags]): if not tag.select(re.sub('[():]|not', '', condition_on_tag[1])): - insert(tag, tag_to_insert) + insert(tag) elif condition_on_tag[0] == "attrs": for attr in rule["condition"]["attrs"]: for tag in chapter_tag.find_all([re.compile(tag) for tag in tags], {attr["name"]: re.compile(fr"{attr['value']}")}): - insert(tag, tag_to_insert) + insert(tag) else: for tag in chapter_tag.find_all([re.compile(tag) for tag in tags]): - insert(tag, tag_to_insert) + insert(tag) def _remove_headings_content(self, content_tag, title_of_chapter: str): """