forked from LiveCarta/BookConverter
LAW-6621 | Add ability to replace/remove property of attr
This commit is contained in:
@@ -17,6 +17,7 @@ class HtmlPresetsProcessor:
|
|||||||
"decomposer": self._decompose_tag,
|
"decomposer": self._decompose_tag,
|
||||||
"replacer": self._replace_tag,
|
"replacer": self._replace_tag,
|
||||||
"attr_remover": self._remove_attr,
|
"attr_remover": self._remove_attr,
|
||||||
|
"attr_property_replacer": self._replace_attr_property,
|
||||||
"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,
|
||||||
@@ -170,23 +171,73 @@ class HtmlPresetsProcessor:
|
|||||||
new_value = re.sub(pattern, "", attr_string)
|
new_value = re.sub(pattern, "", attr_string)
|
||||||
kwargs["found_tag"][attr_name] = new_value
|
kwargs["found_tag"][attr_name] = new_value
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _replace_attr_property(**kwargs):
|
||||||
|
"""
|
||||||
|
Replaces a property or value in an HTML tag attribute.
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
**kwargs (dict): A dictionary containing the following keys:
|
||||||
|
- "rule" (dict): A dictionary containing the rule to apply.
|
||||||
|
- "found_tag" (dict): A dictionary containing the attributes of the tag to modify.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
None.
|
||||||
|
"""
|
||||||
|
rule = kwargs["rule"]
|
||||||
|
found_tag = kwargs["found_tag"]
|
||||||
|
attr = rule["condition"]["attrs"][0]
|
||||||
|
attr_name = attr.get("name", None)
|
||||||
|
attr_value = attr.get("value", None)
|
||||||
|
property_name, property_value = attr_value.split(":")
|
||||||
|
property_name_to_replace = rule["property_to_replace"].get("name", None)
|
||||||
|
property_value_to_replace = rule["property_to_replace"].get("value", None)
|
||||||
|
|
||||||
|
if attr_name is None or found_tag.get(attr_name) is None:
|
||||||
|
return
|
||||||
|
|
||||||
|
pattern = r'\b' + re.escape(property_name) + r'\s*:\s*[^;]*'
|
||||||
|
|
||||||
|
if property_name_to_replace:
|
||||||
|
# Replace property name
|
||||||
|
if property_value_to_replace:
|
||||||
|
# Also replace property value
|
||||||
|
found_tag[attr_name] = re.sub(pattern, f"{property_name_to_replace}:{property_value_to_replace}",
|
||||||
|
found_tag[attr_name])
|
||||||
|
else:
|
||||||
|
# Only replace property name
|
||||||
|
found_tag[attr_name] = re.sub(pattern, f"{property_name_to_replace}:{property_value}",
|
||||||
|
found_tag[attr_name])
|
||||||
|
elif property_value_to_replace:
|
||||||
|
# Replace only property value
|
||||||
|
found_tag[attr_name] = re.sub(pattern, f"{property_name}:{property_value_to_replace}",
|
||||||
|
found_tag[attr_name])
|
||||||
|
else:
|
||||||
|
# Remove this property from tag
|
||||||
|
found_tag[attr_name] = re.sub(pattern, "", found_tag[attr_name])
|
||||||
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _replace_attr(**kwargs):
|
def _replace_attr(**kwargs):
|
||||||
attr = kwargs["rule"]["condition"]["attrs"][0]
|
attr = kwargs["rule"]["condition"]["attrs"][0]
|
||||||
attr_name, attr_value =\
|
attr_name, attr_value =\
|
||||||
attr["name"], attr["value"]
|
attr["name"], attr["value"]
|
||||||
attr_to_replace, attr_value_to_replace =\
|
attr_name_to_replace, attr_value_to_replace =\
|
||||||
kwargs["rule"]["attr_to_replace"]["name"], kwargs["rule"]["attr_to_replace"]["value"]
|
kwargs["rule"]["attr_to_replace"]["name"], kwargs["rule"]["attr_to_replace"]["value"]
|
||||||
if attr_to_replace:
|
if attr_name_to_replace:
|
||||||
kwargs["found_tag"][attr_to_replace] = kwargs["found_tag"][attr_name] \
|
# replace attr name
|
||||||
|
kwargs["found_tag"][attr_name_to_replace] = kwargs["found_tag"][attr_name] \
|
||||||
if kwargs["found_tag"].get(attr_name)\
|
if kwargs["found_tag"].get(attr_name)\
|
||||||
else ""
|
else ""
|
||||||
if attr_value_to_replace:
|
if attr_value_to_replace:
|
||||||
kwargs["found_tag"].attrs[attr_to_replace] = attr_value_to_replace
|
# also replace attr value
|
||||||
|
kwargs["found_tag"].attrs[attr_name_to_replace] = attr_value_to_replace
|
||||||
del kwargs["found_tag"][attr_name]
|
del kwargs["found_tag"][attr_name]
|
||||||
elif attr_value_to_replace:
|
elif attr_value_to_replace:
|
||||||
|
# replace ONLY attr value
|
||||||
kwargs["found_tag"].attrs[attr_name] = attr_value_to_replace
|
kwargs["found_tag"].attrs[attr_name] = attr_value_to_replace
|
||||||
elif attr_name:
|
elif attr_name:
|
||||||
|
# remove this attr from tag
|
||||||
del kwargs["found_tag"][attr_name]
|
del kwargs["found_tag"][attr_name]
|
||||||
|
|
||||||
def _unwrap_tag(self, **kwargs):
|
def _unwrap_tag(self, **kwargs):
|
||||||
|
|||||||
Reference in New Issue
Block a user