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

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