forked from LiveCarta/BookConverter
epub_presets
This commit is contained in:
@@ -8,7 +8,7 @@ from src.util.helpers import BookLogger
|
|||||||
|
|
||||||
|
|
||||||
class HtmlEpubProcessor:
|
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.preset = json.load(open(preset_path))
|
||||||
self.logger = logger
|
self.logger = logger
|
||||||
self.name2action = {
|
self.name2action = {
|
||||||
@@ -173,8 +173,8 @@ class HtmlEpubProcessor:
|
|||||||
# wrap subtag with items
|
# wrap subtag with items
|
||||||
kwargs["tag"].append(tag_to_insert)
|
kwargs["tag"].append(tag_to_insert)
|
||||||
|
|
||||||
def _process_tags(self,
|
@staticmethod
|
||||||
chapter_tag: BeautifulSoup,
|
def _process_tags(chapter_tag: BeautifulSoup,
|
||||||
rules: List[Dict[str, Union[List[str], str, Dict[str, Union[List[Dict[str, str]], int, str]]]]],
|
rules: List[Dict[str, Union[List[str], str, Dict[str, Union[List[Dict[str, str]], int, str]]]]],
|
||||||
action):
|
action):
|
||||||
"""
|
"""
|
||||||
@@ -199,24 +199,24 @@ class HtmlEpubProcessor:
|
|||||||
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.select(', '.join([condition_on_tag[1] + " > " + re.sub(r"[\^$]", "", tag)
|
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)
|
tag.parent.attrs.update(tag.attrs)
|
||||||
action(chapter_tag=chapter_tag, tag=tag, rule=rule)
|
action(chapter_tag=chapter_tag, tag=tag, rule=rule)
|
||||||
elif condition_on_tag[0] == "child_tags":
|
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 chapter_tag.select(', '.join([re.sub(r"[\^$]", "", tag) + condition_on_tag[1]
|
||||||
for tag in tags])):
|
for tag in tags])):
|
||||||
action(chapter_tag=chapter_tag, tag=tag, rule=rule)
|
action(chapter_tag=chapter_tag, tag=tag, rule=rule)
|
||||||
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']}")}):
|
||||||
action(chapter_tag=chapter_tag, tag=tag, rule=rule)
|
action(chapter_tag=chapter_tag, tag=tag, rule=rule)
|
||||||
# attr replacer
|
# attr replacer
|
||||||
elif condition_on_tag[0] == "tags":
|
elif condition_on_tag[0] == "tags":
|
||||||
attr = rule["attr"]
|
attr = rule["attr"]
|
||||||
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']}")}):
|
||||||
action(chapter_tag=chapter_tag, tag=tag, rule=rule)
|
action(chapter_tag=chapter_tag, tag=tag, rule=rule)
|
||||||
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]):
|
||||||
action(chapter_tag=chapter_tag, tag=tag, rule=rule)
|
action(chapter_tag=chapter_tag, tag=tag, rule=rule)
|
||||||
|
|||||||
Reference in New Issue
Block a user