forked from LiveCarta/BookConverter
change indent processing
This commit is contained in:
@@ -65,7 +65,7 @@ class InlineStyleProcessor:
|
|||||||
def indents_processing(split_style: List[str]) -> str:
|
def indents_processing(split_style: List[str]) -> str:
|
||||||
"""
|
"""
|
||||||
Function process indents from left using
|
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
|
Parameters
|
||||||
----------
|
----------
|
||||||
split_style: List[str]
|
split_style: List[str]
|
||||||
@@ -77,37 +77,55 @@ class InlineStyleProcessor:
|
|||||||
processed style with counted indent
|
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)+';'
|
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|margin): *-*(\d*\.*\d+)\w+;*)")
|
||||||
text_indent_regexp = re.compile(
|
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_margin = re.search(margin_left_regexp, processed_style)
|
||||||
has_text_indent = re.search(text_indent_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(int("0" + "".join(
|
||||||
filter(str.isdigit, str(has_margin.group(4))))))
|
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(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: " +
|
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(
|
processed_style = processed_style.replace(
|
||||||
has_margin.group(1), "")
|
has_margin.group(1), "")
|
||||||
return processed_style
|
return processed_style
|
||||||
|
|
||||||
|
indent_value = str(closest_number(abs(num_m)))
|
||||||
processed_style = processed_style.replace(has_margin.group(1), "text-indent: " +
|
processed_style = processed_style.replace(has_margin.group(1), "text-indent: " +
|
||||||
str(abs(num_m)) + "px; ")
|
indent_value + "px; ")
|
||||||
return processed_style
|
return processed_style
|
||||||
|
|
||||||
elif has_text_indent:
|
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: " +
|
processed_style = processed_style.replace(has_text_indent.group(1), "text-indent: " +
|
||||||
str(abs(int("0" + "".join(
|
indent_value + "px; ")
|
||||||
filter(str.isdigit, str(has_text_indent.group(3)))))))
|
|
||||||
+ "px; ")
|
|
||||||
return processed_style
|
return processed_style
|
||||||
return processed_style
|
return processed_style
|
||||||
|
|
||||||
|
|||||||
@@ -48,18 +48,28 @@ class LiveCartaConfig:
|
|||||||
If property has not empty list, it means that only certain property-value combinations can be transformed.
|
If property has not empty list, it means that only certain property-value combinations can be transformed.
|
||||||
"""
|
"""
|
||||||
LIVECARTA_STYLE_ATTRS = {
|
LIVECARTA_STYLE_ATTRS = {
|
||||||
"text-indent": [],
|
|
||||||
"font-variant": ["small-caps"],
|
|
||||||
"text-align": [x for x in ["justify", "right", "center", "left"] if x != "left"],
|
|
||||||
"align": [],
|
"align": [],
|
||||||
"font": [],
|
"font": [],
|
||||||
"font-family": [],
|
"font-family": [],
|
||||||
"font-size": [],
|
"font-size": [],
|
||||||
|
"font-variant": ["small-caps"],
|
||||||
"font-weight": ["bold", "600", "700", "800", "900"], # <strong>
|
"font-weight": ["bold", "600", "700", "800", "900"], # <strong>
|
||||||
"font-style": ["italic"], # <i>
|
"font-style": ["italic"], # <i>
|
||||||
"text-decoration": ["underline", "line-through"], # <u> , <s>
|
"text-decoration": ["underline", "line-through"], # <u> , <s>
|
||||||
"text-decoration-line": ["underline", "line-through"], # <u> , <s>
|
"text-decoration-line": ["underline", "line-through"], # <u> , <s>
|
||||||
"text-transform": [],
|
"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"], # <sup>
|
"vertical-align": ["super"], # <sup>
|
||||||
"color": [],
|
"color": [],
|
||||||
"background-color": [],
|
"background-color": [],
|
||||||
@@ -76,8 +86,4 @@ class LiveCartaConfig:
|
|||||||
"border-bottom": [],
|
"border-bottom": [],
|
||||||
"list-style-type": [],
|
"list-style-type": [],
|
||||||
"list-style-image": [],
|
"list-style-image": [],
|
||||||
"margin-left": [],
|
|
||||||
"margin-top": [],
|
|
||||||
"margin": [],
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,16 +16,27 @@ class StyleReader:
|
|||||||
to suit LiveCarta style convention.
|
to suit LiveCarta style convention.
|
||||||
"""
|
"""
|
||||||
self.LIVECARTA_STYLE_ATTRS_MAPPING = {
|
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": lambda x: "",
|
||||||
"font-family": lambda x: x,
|
"font-family": lambda x: x,
|
||||||
"font-size": self.convert_tag_style_values,
|
"font-size": self.convert_tag_style_values,
|
||||||
|
"font-variant": lambda x: x,
|
||||||
"text-transform": 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,
|
"color": self.get_text_color,
|
||||||
"background-color": self.get_bg_color,
|
"background-color": self.get_bg_color,
|
||||||
"background": 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": self.convert_tag_style_values,
|
||||||
"border-top-width": self.convert_tag_style_values,
|
"border-top-width": self.convert_tag_style_values,
|
||||||
"border-right-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-left": self.convert_tag_style_values,
|
||||||
"border-bottom": 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-type": lambda x: x if x in LiveCartaConfig.list_types else "disc",
|
||||||
"list-style-image": lambda x: "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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
|||||||
Reference in New Issue
Block a user