diff --git a/src/html_presets_processor.py b/src/html_presets_processor.py index 1e5df37..c908ccb 100644 --- a/src/html_presets_processor.py +++ b/src/html_presets_processor.py @@ -18,7 +18,8 @@ class HtmlPresetsProcessor: "replacer": self._replace_tag, "attr_replacer": self._replace_attr, "unwrapper": self._unwrap_tag, - "inserter": self._insert_tag + "inserter": self._insert_tag, + "text_replacer": self._replace_text } @staticmethod @@ -142,6 +143,13 @@ class HtmlPresetsProcessor: # wrap subtag with items 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 def _process_tags(body_tag: BeautifulSoup, 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], {attr["name"]: re.compile(fr"{attr['value']}")}): action(body_tag=body_tag, tag=tag, rule=rule) - # attr replacer - elif condition_on_tag[0] == "tags": - attr = rule["attr"] - for tag in body_tag.find_all([re.compile(tag) for tag in tags], - {attr['name']: re.compile(fr"{attr['value']}")}): + elif condition_on_tag[0] == "text": + # find all tags that are in List of tags and tags that contains required text + for tag in body_tag.find_all( + lambda t: re.search(r"(?=(" + '|'.join([tag for tag in tags]) + r"))", + t.name) and re.search(re.compile(rule["condition"]["text"]), + t.text)): action(body_tag=body_tag, tag=tag, rule=rule) else: for tag in body_tag.find_all([re.compile(tag) for tag in tags]):