html_presets_processor.py changed structure of presets

This commit is contained in:
Kiryl
2022-10-13 11:08:57 +03:00
parent 93979e8d59
commit 43fc0c650d

View File

@@ -24,7 +24,7 @@ class HtmlPresetsProcessor:
@staticmethod
def _wrap_tag(**kwargs):
kwargs["tag"].wrap(kwargs["body_tag"].new_tag(
kwargs["rule"]["tag_to_wrap"]))
kwargs["rule"]["tag_to_wrap"]["name"]))
@staticmethod
def set_attrs_to_parent(tag, parent_tag):
@@ -98,24 +98,31 @@ class HtmlPresetsProcessor:
@staticmethod
def _replace_tag(**kwargs):
tag_to_replace: str = kwargs["rule"]["tag_to_replace"]
tag_to_replace: str = kwargs["rule"]["tag_to_replace"]["name"]
kwargs["tag"].name = tag_to_replace
if kwargs["rule"]["tag_to_replace"].get("attrs"):
dict_attributes = {attr["name"]: attr["value"]
for attr in kwargs["rule"]["tag_to_replace"]["attrs"]}
kwargs["tag"].attrs = dict_attributes
@staticmethod
def _replace_attr(**kwargs):
attr, attr_value =\
kwargs["rule"]["attr"]["name"], kwargs["rule"]["attr"]["value"]
attr = kwargs["rule"]["condition"]["attrs"][0]
attr_name, attr_value =\
attr["name"], attr["value"]
attr_to_replace, attr_value_to_replace =\
kwargs["rule"]["attr_to_replace"]["name"], kwargs["rule"]["attr_to_replace"]["value"]
if attr_to_replace:
kwargs["tag"][attr_to_replace] = kwargs["tag"][attr]
kwargs["tag"][attr_to_replace] = kwargs["tag"][attr_name] \
if kwargs["tag"].get(attr_name)\
else ""
if attr_value_to_replace:
kwargs["tag"].attrs[attr_to_replace] = attr_value_to_replace
del kwargs["tag"][attr]
del kwargs["tag"][attr_name]
elif attr_value_to_replace:
kwargs["tag"].attrs[attr] = attr_value_to_replace
elif attr:
del kwargs["tag"][attr]
kwargs["tag"].attrs[attr_name] = attr_value_to_replace
elif attr_name:
del kwargs["tag"][attr_name]
def _unwrap_tag(self, **kwargs):
if kwargs["tag"].parent:
@@ -124,8 +131,11 @@ class HtmlPresetsProcessor:
@staticmethod
def _insert_tag(**kwargs):
dict_attributes = {attr["name"]: attr["value"]
for attr in kwargs["rule"]["tag_to_insert"]["attrs"]}
tag_to_insert = \
kwargs["body_tag"].new_tag(kwargs["rule"]["tag_to_insert"])
kwargs["body_tag"].new_tag(
kwargs["rule"]["tag_to_insert"]["name"], attrs=dict_attributes)
# insert all items that was in tag to subtag and remove from tag
for content in reversed(kwargs["tag"].contents):
tag_to_insert.insert(0, content.extract())