Optimize indent working

This commit is contained in:
Kibzik
2023-03-15 12:57:17 +03:00
parent 8e8c23d071
commit 5eba3d549c
2 changed files with 19 additions and 31 deletions

View File

@@ -180,7 +180,6 @@ class HtmlPresetsProcessor:
def _unwrap_tag(self, **kwargs): def _unwrap_tag(self, **kwargs):
if kwargs["found_tag"].parent: if kwargs["found_tag"].parent:
self.set_attrs_to_parent(kwargs["found_tag"], kwargs["found_tag"].parent) self.set_attrs_to_parent(kwargs["found_tag"], kwargs["found_tag"].parent)
print(kwargs["found_tag"])
kwargs["found_tag"].unwrap() kwargs["found_tag"].unwrap()
@staticmethod @staticmethod

View File

@@ -84,50 +84,39 @@ class InlineStyleProcessor:
to value and divisible by m to value and divisible by m
""" """
# Find the quotient # Find the quotient
q = int(value / m) q = round(value / m)
return m * q
n1 = m * q
n2 = (m * (q + 1))
new_value = n1 if (abs(value - n1) < abs(value - n2)) else n2
return new_value
processed_style = ";".join(split_style) + ';' processed_style = ";".join(split_style) + ';'
margin_left_regexp = re.compile( margin_left_regexp = re.compile(
r"((margin-left|margin): *-*(\d*\.*\d+)\w+;*)") r"(margin(-left)?:\s*-?(\d+(\.\d+)?)(\w*)\s*;)")
text_indent_regexp = re.compile( text_indent_regexp = re.compile(
r"(text-indent: *-*(\d*\.*\d+)\w+;*)") r"(text-indent:\s*-?(\d+(\.\d+)?)(\w*)\s*;)")
has_margin = margin_left_regexp.search(processed_style)
has_text_indent = text_indent_regexp.search(processed_style)
has_margin = re.search(margin_left_regexp, processed_style)
has_text_indent = re.search(text_indent_regexp, processed_style)
if has_margin: if has_margin:
num_m = abs(int("0" + "".join( num_m = abs(float(has_margin.group(3)))
filter(str.isdigit, str(has_margin.group(3))))))
if has_text_indent: if has_text_indent:
num_ti = abs(int("0" + "".join( num_ti = abs(float(has_text_indent.group(2)))
filter(str.isdigit, str(has_text_indent.group(2))))))
indent_value = str(closest_number(abs(num_m - num_ti))) indent_value = str(closest_number(abs(num_m - num_ti)))
processed_style = processed_style.replace(has_text_indent.group(1), "text-indent: " +
indent_value + "px; ")
processed_style = processed_style.replace( processed_style = processed_style.replace(
has_margin.group(1), "") has_text_indent.group(0), f"text-indent: {indent_value}px;")
return processed_style else:
indent_value = str(closest_number(abs(num_m))) indent_value = str(closest_number(abs(num_m)))
processed_style = processed_style.replace(has_margin.group(1), "text-indent: " + processed_style += f"text-indent: {indent_value}px;"
indent_value + "px; ")
return processed_style processed_style = margin_left_regexp.sub("", processed_style)
elif has_text_indent: elif has_text_indent:
num_ti = abs(int("0" + "".join( num_ti = abs(float(has_text_indent.group(2)))
filter(str.isdigit, str(has_text_indent.group(2))))))
indent_value = str(closest_number(num_ti)) indent_value = str(closest_number(num_ti))
processed_style = processed_style.replace(has_text_indent.group(1), "text-indent: " + processed_style = text_indent_regexp.sub(f"text-indent: {indent_value}px;", processed_style)
indent_value + "px; ")
return processed_style return processed_style.strip(";")
return processed_style
def process_inline_style(self) -> str: def process_inline_style(self) -> str:
""" """