change indent processing

This commit is contained in:
Kiryl
2022-12-22 18:28:40 +03:00
parent 38d8024292
commit 7b2c35b15a
3 changed files with 56 additions and 25 deletions

View File

@@ -65,7 +65,7 @@ class InlineStyleProcessor:
def indents_processing(split_style: List[str]) -> str:
"""
Function process indents from left using
formula_of_indent: indent = abs(margin - text_indent)
formula_of_indent: indent = closest_number(abs(margin - text_indent))
Parameters
----------
split_style: List[str]
@@ -77,37 +77,55 @@ class InlineStyleProcessor:
processed style with counted indent
"""
def closest_number(value: int, m: int = 30) -> int:
"""
Function to find the number closest
to value and divisible by m
"""
# Find the quotient
q = int(value / m)
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)+';'
margin_left_regexp = re.compile(
r"((margin-left|margin): *-*((\d*)\.*\d+)\w+;*)")
r"((margin-left|margin): *-*(\d*\.*\d+)\w+;*)")
text_indent_regexp = re.compile(
r"(text-indent: *-*((\d*)\.*\d+)\w+;*)")
r"(text-indent: *-*(\d*\.*\d+)\w+;*)")
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(4))))))
filter(str.isdigit, str(has_margin.group(3))))))
if has_text_indent:
num_ti = abs(int("0" + "".join(
filter(str.isdigit, str(has_text_indent.group(3))))))
filter(str.isdigit, str(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: " +
str(abs(num_m - num_ti)) + "px; ")
indent_value + "px; ")
processed_style = processed_style.replace(
has_margin.group(1), "")
return processed_style
indent_value = str(closest_number(abs(num_m)))
processed_style = processed_style.replace(has_margin.group(1), "text-indent: " +
str(abs(num_m)) + "px; ")
indent_value + "px; ")
return processed_style
elif has_text_indent:
num_ti = abs(int("0" + "".join(
filter(str.isdigit, str(has_text_indent.group(2))))))
indent_value = str(closest_number(num_ti))
processed_style = processed_style.replace(has_text_indent.group(1), "text-indent: " +
str(abs(int("0" + "".join(
filter(str.isdigit, str(has_text_indent.group(3)))))))
+ "px; ")
indent_value + "px; ")
return processed_style
return processed_style