Rewrite _insert_tags cause of mutable tag

This commit is contained in:
Kiryl
2022-07-14 19:11:33 +03:00
parent 8624a0a776
commit ca229dc6b7

View File

@@ -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 = chapter_tag.new_tag("table")
table.attrs["border"], table.attrs["align"], table.attrs["style"] \ table.attrs["border"], table.attrs["align"], table.attrs["style"] \
= border, "center", f"width:{width}%;" = border, "center", f"width:{width}%;"
tbody, tr, td = \ tbody, tr, td = \
chapter_tag.new_tag("tbody"), chapter_tag.new_tag("tr"), chapter_tag.new_tag("td") chapter_tag.new_tag("tbody"), chapter_tag.new_tag("tr"), chapter_tag.new_tag("td")
td.attrs["bgcolor"] = bg_color td.attrs["bgcolor"] = bg_color
tag_to_be_wrapped.wrap(td) tag_to_wrap.wrap(td)
td.wrap(tr) td.wrap(tr)
tr.wrap(tbody) tr.wrap(tbody)
tbody.wrap(table) tbody.wrap(table)
table.insert_after(BeautifulSoup(features="lxml").new_tag("br")) table.insert_after(BeautifulSoup(features="lxml").new_tag("br"))
return table return table
def process_tag_using_table(tag_to_wrap): def process_tag_using_table():
_wrap_tag_with_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", 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, 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) 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 attr in rule["attrs"]:
for tag_to_wrap in chapter_tag.find_all([re.compile(tag) for tag in tags], for tag_to_wrap in chapter_tag.find_all([re.compile(tag) for tag in tags],
{attr["name"]: re.compile(fr"{attr['value']}")}): {attr["name"]: re.compile(fr"{attr['value']}")}):
process_tag_using_table(tag_to_wrap) process_tag_using_table()
@staticmethod @staticmethod
def _tags_to_correspond_livecarta_tag(chapter_tag, rules: list): def _tags_to_correspond_livecarta_tag(chapter_tag, rules: list):
@@ -229,7 +227,9 @@ class HtmlEpubPreprocessor:
Chapter Tag with inserted tags 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 # insert all items that was in tag to subtag and remove from tag
for content in reversed(tag.contents): for content in reversed(tag.contents):
tag_to_insert.insert(0, content.extract()) tag_to_insert.insert(0, content.extract())
@@ -238,26 +238,24 @@ class HtmlEpubPreprocessor:
for rule in rules: for rule in rules:
tags = rule["tags"] tags = rule["tags"]
tag_to_insert = \
chapter_tag.new_tag(rule["tag_to_insert"])
if rule["condition"]: if rule["condition"]:
for condition_on_tag in ((k, v) for k, v in rule["condition"].items() if v): for condition_on_tag in ((k, v) for k, v in rule["condition"].items() if v):
if condition_on_tag[0] == 'parent_tags': if condition_on_tag[0] == 'parent_tags':
for tag in chapter_tag.find_all([re.compile(tag) for tag in tags]): for tag in chapter_tag.find_all([re.compile(tag) for tag in tags]):
if tag.parent.select(condition_on_tag[1]): if tag.parent.select(condition_on_tag[1]):
insert(tag, tag_to_insert) insert(tag)
elif condition_on_tag[0] == 'child_tags': elif condition_on_tag[0] == 'child_tags':
for tag in chapter_tag.find_all([re.compile(tag) for tag in 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])): if not tag.select(re.sub('[():]|not', '', condition_on_tag[1])):
insert(tag, tag_to_insert) insert(tag)
elif condition_on_tag[0] == "attrs": elif condition_on_tag[0] == "attrs":
for attr in rule["condition"]["attrs"]: for attr in rule["condition"]["attrs"]:
for tag in chapter_tag.find_all([re.compile(tag) for tag in tags], for tag in chapter_tag.find_all([re.compile(tag) for tag in tags],
{attr["name"]: re.compile(fr"{attr['value']}")}): {attr["name"]: re.compile(fr"{attr['value']}")}):
insert(tag, tag_to_insert) insert(tag)
else: else:
for tag in chapter_tag.find_all([re.compile(tag) for tag in tags]): 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): def _remove_headings_content(self, content_tag, title_of_chapter: str):
""" """