epub_presets

This commit is contained in:
Kiryl
2022-09-06 13:19:26 +03:00
parent 4f4dbab94c
commit 96c7421e71

View File

@@ -8,7 +8,7 @@ from src.util.helpers import BookLogger
class HtmlEpubProcessor:
def __init__(self, preset_path: str = "presets/presets.json", logger: BookLogger = None):
def __init__(self, preset_path: str = "presets/epub_presets.json", logger: BookLogger = None):
self.preset = json.load(open(preset_path))
self.logger = logger
self.name2action = {
@@ -173,8 +173,8 @@ class HtmlEpubProcessor:
# wrap subtag with items
kwargs["tag"].append(tag_to_insert)
def _process_tags(self,
chapter_tag: BeautifulSoup,
@staticmethod
def _process_tags(chapter_tag: BeautifulSoup,
rules: List[Dict[str, Union[List[str], str, Dict[str, Union[List[Dict[str, str]], int, str]]]]],
action):
"""
@@ -199,24 +199,24 @@ class HtmlEpubProcessor:
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.select(', '.join([condition_on_tag[1] + " > " + re.sub(r"[\^$]", "", tag)
for tag in tags])):
for tag in tags])):
tag.parent.attrs.update(tag.attrs)
action(chapter_tag=chapter_tag, tag=tag, rule=rule)
elif condition_on_tag[0] == "child_tags":
for tag in chapter_tag.select(', '.join([re.sub(r"[\^$]", "", tag) + condition_on_tag[1]
for tag in tags])):
action(chapter_tag=chapter_tag, tag=tag, rule=rule)
for tag in tags])):
action(chapter_tag=chapter_tag, tag=tag, rule=rule)
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']}")}):
{attr["name"]: re.compile(fr"{attr['value']}")}):
action(chapter_tag=chapter_tag, tag=tag, rule=rule)
# attr replacer
elif condition_on_tag[0] == "tags":
attr = rule["attr"]
for tag in chapter_tag.find_all([re.compile(tag) for tag in tags],
{attr['name']: re.compile(fr"{attr['value']}")}):
action(chapter_tag=chapter_tag, tag=tag, rule=rule)
action(chapter_tag=chapter_tag, tag=tag, rule=rule)
else:
for tag in chapter_tag.find_all([re.compile(tag) for tag in tags]):
action(chapter_tag=chapter_tag, tag=tag, rule=rule)