add text_replacer in tags

This commit is contained in:
Kiryl
2022-10-13 11:09:18 +03:00
parent 43fc0c650d
commit 1363940fa2

View File

@@ -18,7 +18,8 @@ class HtmlPresetsProcessor:
"replacer": self._replace_tag, "replacer": self._replace_tag,
"attr_replacer": self._replace_attr, "attr_replacer": self._replace_attr,
"unwrapper": self._unwrap_tag, "unwrapper": self._unwrap_tag,
"inserter": self._insert_tag "inserter": self._insert_tag,
"text_replacer": self._replace_text
} }
@staticmethod @staticmethod
@@ -142,6 +143,13 @@ class HtmlPresetsProcessor:
# wrap subtag with items # wrap subtag with items
kwargs["tag"].append(tag_to_insert) kwargs["tag"].append(tag_to_insert)
@staticmethod
def _replace_text(**kwargs):
if re.search(re.compile(kwargs["rule"]["condition"]["text"]), kwargs["tag"].string):
new_text = re.sub(re.compile(
kwargs["rule"]["condition"]["text"]), kwargs["rule"]["text_to_replace"], kwargs["tag"].string)
kwargs["tag"].string.replace_with(new_text)
@staticmethod @staticmethod
def _process_tags(body_tag: BeautifulSoup, def _process_tags(body_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]]]]],
@@ -181,11 +189,12 @@ class HtmlPresetsProcessor:
for tag in body_tag.find_all([re.compile(tag) for tag in tags], for tag in body_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(body_tag=body_tag, tag=tag, rule=rule) action(body_tag=body_tag, tag=tag, rule=rule)
# attr replacer elif condition_on_tag[0] == "text":
elif condition_on_tag[0] == "tags": # find all tags that are in List of tags and tags that contains required text
attr = rule["attr"] for tag in body_tag.find_all(
for tag in body_tag.find_all([re.compile(tag) for tag in tags], lambda t: re.search(r"(?=(" + '|'.join([tag for tag in tags]) + r"))",
{attr['name']: re.compile(fr"{attr['value']}")}): t.name) and re.search(re.compile(rule["condition"]["text"]),
t.text)):
action(body_tag=body_tag, tag=tag, rule=rule) action(body_tag=body_tag, tag=tag, rule=rule)
else: else:
for tag in body_tag.find_all([re.compile(tag) for tag in tags]): for tag in body_tag.find_all([re.compile(tag) for tag in tags]):