From 7b2c35b15ae428d62d5b40d855f4882a7fea779f Mon Sep 17 00:00:00 2001 From: Kiryl Date: Thu, 22 Dec 2022 18:28:40 +0300 Subject: [PATCH] change indent processing --- src/inline_style_processor.py | 38 ++++++++++++++++++++++++++--------- src/livecarta_config.py | 20 +++++++++++------- src/style_reader.py | 23 +++++++++++++-------- 3 files changed, 56 insertions(+), 25 deletions(-) diff --git a/src/inline_style_processor.py b/src/inline_style_processor.py index cc7c14d..4b9ecbd 100644 --- a/src/inline_style_processor.py +++ b/src/inline_style_processor.py @@ -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 diff --git a/src/livecarta_config.py b/src/livecarta_config.py index b1b7b58..d62f8c9 100644 --- a/src/livecarta_config.py +++ b/src/livecarta_config.py @@ -48,18 +48,28 @@ class LiveCartaConfig: If property has not empty list, it means that only certain property-value combinations can be transformed. """ LIVECARTA_STYLE_ATTRS = { - "text-indent": [], - "font-variant": ["small-caps"], - "text-align": [x for x in ["justify", "right", "center", "left"] if x != "left"], "align": [], "font": [], "font-family": [], "font-size": [], + "font-variant": ["small-caps"], "font-weight": ["bold", "600", "700", "800", "900"], # "font-style": ["italic"], # "text-decoration": ["underline", "line-through"], # , "text-decoration-line": ["underline", "line-through"], # , "text-transform": [], + "text-align": [x for x in ["justify", "right", "center", "left"] if x != "left"], + "text-indent": [], + "margin": [], + "margin-top": [], + "margin-right": [], + "margin-left": [], + "margin-bottom": [], + "padding": [], + "padding-top": [], + "padding-right": [], + "padding-left": [], + "padding-bottom": [], "vertical-align": ["super"], # "color": [], "background-color": [], @@ -76,8 +86,4 @@ class LiveCartaConfig: "border-bottom": [], "list-style-type": [], "list-style-image": [], - "margin-left": [], - "margin-top": [], - "margin": [], - } diff --git a/src/style_reader.py b/src/style_reader.py index ef6ce49..6d0f90e 100644 --- a/src/style_reader.py +++ b/src/style_reader.py @@ -16,16 +16,27 @@ class StyleReader: to suit LiveCarta style convention. """ self.LIVECARTA_STYLE_ATTRS_MAPPING = { - "text-indent": lambda x: self.convert_tag_style_values(x, is_indent=True), - "font-variant": lambda x: x, - "text-align": lambda x: x, "font": lambda x: "", "font-family": lambda x: x, "font-size": self.convert_tag_style_values, + "font-variant": lambda x: x, "text-transform": lambda x: x, + "text-align": lambda x: x, + "text-indent": lambda x: self.convert_tag_style_values(x, is_indent=True), + "margin": self.convert_tag_style_values, + "margin-top": self.convert_tag_style_values, + "margin-right": self.convert_tag_style_values, + "margin-left": lambda x: self.convert_tag_style_values(x, is_indent=True), + "margin-bottom": self.convert_tag_style_values, + "padding": self.convert_tag_style_values, + "padding-top": self.convert_tag_style_values, + "padding-right": self.convert_tag_style_values, + "padding-left": self.convert_tag_style_values, + "padding-bottom": self.convert_tag_style_values, "color": self.get_text_color, "background-color": self.get_bg_color, "background": self.get_bg_color, + "width": lambda x: self.convert_tag_style_values(x) if "%" not in x else x, "border": self.convert_tag_style_values, "border-top-width": self.convert_tag_style_values, "border-right-width": self.convert_tag_style_values, @@ -36,11 +47,7 @@ class StyleReader: "border-left": self.convert_tag_style_values, "border-bottom": self.convert_tag_style_values, "list-style-type": lambda x: x if x in LiveCartaConfig.list_types else "disc", - "list-style-image": lambda x: "disc", - "margin-left": lambda x: self.convert_tag_style_values(x, is_indent=True), - "margin-top": lambda x: self.convert_tag_style_values(x, is_indent=True), - "margin": lambda x: self.convert_tag_style_values(x, is_indent=True), - "width": lambda x: self.convert_tag_style_values(x) if "%" not in x else x + "list-style-image": lambda x: "disc" } @staticmethod