forked from LiveCarta/BookConverter
Remove font=family processing
This commit is contained in:
@@ -113,13 +113,6 @@ class HTMLDocxPreprocessor:
|
|||||||
elif color and color in LiveCartaConfig.COLORS_MAP:
|
elif color and color in LiveCartaConfig.COLORS_MAP:
|
||||||
font.attrs["style"] = f'color: {color};'
|
font.attrs["style"] = f'color: {color};'
|
||||||
|
|
||||||
if face is not None:
|
|
||||||
face = re.sub(r",[\w,\- ]*$", "", face)
|
|
||||||
if face != LiveCartaConfig.DEFAULT_FONT_NAME and LiveCartaConfig.FONT_CORRESPONDANCE_TABLE.get(face):
|
|
||||||
font.attrs["face"] = LiveCartaConfig.FONT_CORRESPONDANCE_TABLE[face]
|
|
||||||
else:
|
|
||||||
font.attrs["face"] = LiveCartaConfig.DEFAULT_FONT_NAME
|
|
||||||
|
|
||||||
if len(font.attrs) == 0:
|
if len(font.attrs) == 0:
|
||||||
font.unwrap()
|
font.unwrap()
|
||||||
|
|
||||||
|
|||||||
@@ -78,7 +78,7 @@ def convert_indents_tag_values(size_value: str) -> str:
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
Dictionary LIVECARTA_STYLE_ATTRS = { css property: value }
|
Dictionary LIVECARTA_STYLE_ATTRS = { css property: value }
|
||||||
Style properties that can be used to fit livecarta css style convention.
|
Style properties that can be used to fit LiveCarta css style convention.
|
||||||
If property has empty list, it means that any value can be converted.
|
If property has empty list, it means that any value can be converted.
|
||||||
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.
|
||||||
"""
|
"""
|
||||||
@@ -88,8 +88,7 @@ LIVECARTA_STYLE_ATTRS = {
|
|||||||
"text-align": [x for x in LiveCartaConfig.ALIGN_STYLES if x != LiveCartaConfig.DEFAULT_ALIGN_STYLE],
|
"text-align": [x for x in LiveCartaConfig.ALIGN_STYLES if x != LiveCartaConfig.DEFAULT_ALIGN_STYLE],
|
||||||
"align": [],
|
"align": [],
|
||||||
"font": [],
|
"font": [],
|
||||||
"font-family": [x for x in LiveCartaConfig.FONT_CORRESPONDANCE_TABLE.keys()
|
"font-family": [],
|
||||||
if x != LiveCartaConfig.DEFAULT_FONT_NAME],
|
|
||||||
"font-size": [],
|
"font-size": [],
|
||||||
"font-weight": ["bold", "600", "700", "800", "900"], # <strong>
|
"font-weight": ["bold", "600", "700", "800", "900"], # <strong>
|
||||||
"font-style": ["italic"], # <i>
|
"font-style": ["italic"], # <i>
|
||||||
@@ -118,15 +117,14 @@ LIVECARTA_STYLE_ATTRS = {
|
|||||||
Dictionary LIVECARTA_STYLE_ATTRS_MAPPING = { property: mapping function }
|
Dictionary LIVECARTA_STYLE_ATTRS_MAPPING = { property: mapping function }
|
||||||
|
|
||||||
Warning, if LIVECARTA_STYLE_ATTRS is changed, LIVECARTA_STYLE_ATTRS_MAPPING should be updated
|
Warning, if LIVECARTA_STYLE_ATTRS is changed, LIVECARTA_STYLE_ATTRS_MAPPING should be updated
|
||||||
to suit livecarta style convention.
|
to suit LiveCarta style convention.
|
||||||
"""
|
"""
|
||||||
LIVECARTA_STYLE_ATTRS_MAPPING = {
|
LIVECARTA_STYLE_ATTRS_MAPPING = {
|
||||||
"text-indent": convert_indents_tag_values,
|
"text-indent": convert_indents_tag_values,
|
||||||
"font-variant": lambda x: x,
|
"font-variant": lambda x: x,
|
||||||
"text-align": lambda x: x,
|
"text-align": lambda x: x,
|
||||||
"font": lambda x: "",
|
"font": lambda x: "",
|
||||||
"font-family": lambda x: LiveCartaConfig.FONT_CORRESPONDANCE_TABLE.get(re.sub(r"^\s+|\s+$", "", x.title()))
|
"font-family": lambda x: x,
|
||||||
or LiveCartaConfig.FONT_CORRESPONDANCE_TABLE.get(re.sub(r"^\s+|\s+$", "", x)),
|
|
||||||
"font-size": convert_tag_style_values,
|
"font-size": convert_tag_style_values,
|
||||||
"color": get_text_color,
|
"color": get_text_color,
|
||||||
"background-color": get_bg_color,
|
"background-color": get_bg_color,
|
||||||
@@ -146,6 +144,15 @@ LIVECARTA_STYLE_ATTRS_MAPPING = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def style_conditions(style_value, style_name):
|
||||||
|
cleaned_value = style_value.replace("\"", "")
|
||||||
|
constraints_on_value = LIVECARTA_STYLE_ATTRS.get(
|
||||||
|
style_name)
|
||||||
|
value_not_in_possible_values_list = cleaned_value not in LIVECARTA_STYLE_ATTRS[
|
||||||
|
style_name]
|
||||||
|
return cleaned_value, constraints_on_value, value_not_in_possible_values_list
|
||||||
|
|
||||||
|
|
||||||
def update_inline_styles_to_livecarta_convention(split_style: list):
|
def update_inline_styles_to_livecarta_convention(split_style: list):
|
||||||
for i, style in enumerate(split_style):
|
for i, style in enumerate(split_style):
|
||||||
style_name, style_value = style.split(":")
|
style_name, style_value = style.split(":")
|
||||||
@@ -154,11 +161,8 @@ def update_inline_styles_to_livecarta_convention(split_style: list):
|
|||||||
split_style[i] = ""
|
split_style[i] = ""
|
||||||
return split_style
|
return split_style
|
||||||
|
|
||||||
cleaned_value = style_value.replace("\"", "").split()[-1]
|
cleaned_value, constraints_on_value, value_not_in_possible_values_list =\
|
||||||
constraints_on_value = LIVECARTA_STYLE_ATTRS.get(
|
style_conditions(style_value, style_name)
|
||||||
style_name)
|
|
||||||
value_not_in_possible_values_list = cleaned_value not in LIVECARTA_STYLE_ATTRS[
|
|
||||||
style_name]
|
|
||||||
if constraints_on_value and value_not_in_possible_values_list:
|
if constraints_on_value and value_not_in_possible_values_list:
|
||||||
# there are constraints + value not in LIVECARTA_STYLE_ATTRS, remove from css file
|
# there are constraints + value not in LIVECARTA_STYLE_ATTRS, remove from css file
|
||||||
split_style[i] = ""
|
split_style[i] = ""
|
||||||
@@ -172,7 +176,7 @@ def update_inline_styles_to_livecarta_convention(split_style: list):
|
|||||||
|
|
||||||
|
|
||||||
def build_inline_style_content(style: str) -> str:
|
def build_inline_style_content(style: str) -> str:
|
||||||
"""Build inline style with livecarta convention"""
|
"""Build inline style with LiveCarta convention"""
|
||||||
# replace all spaces between "; & letter" to ";"
|
# replace all spaces between "; & letter" to ";"
|
||||||
style = re.sub(r"; *", ";", style)
|
style = re.sub(r"; *", ";", style)
|
||||||
# when we split style by ";", last element of the list is "" - None
|
# when we split style by ";", last element of the list is "" - None
|
||||||
@@ -189,16 +193,15 @@ def build_inline_style_content(style: str) -> str:
|
|||||||
|
|
||||||
def update_css_styles_to_livecarta_convention(css_rule: cssutils.css.CSSStyleRule,
|
def update_css_styles_to_livecarta_convention(css_rule: cssutils.css.CSSStyleRule,
|
||||||
style_type: cssutils.css.property.Property):
|
style_type: cssutils.css.property.Property):
|
||||||
|
if style_type.name == "font-family":
|
||||||
|
pass
|
||||||
if style_type.name not in LIVECARTA_STYLE_ATTRS:
|
if style_type.name not in LIVECARTA_STYLE_ATTRS:
|
||||||
# property not in LIVECARTA_STYLE_ATTRS, remove from css file
|
# property not in LIVECARTA_STYLE_ATTRS, remove from css file
|
||||||
css_rule.style[style_type.name] = ""
|
css_rule.style[style_type.name] = ""
|
||||||
return
|
return
|
||||||
|
|
||||||
cleaned_value = style_type.value.replace("\"", "")
|
cleaned_value, constraints_on_value, value_not_in_possible_values_list =\
|
||||||
constraints_on_value = LIVECARTA_STYLE_ATTRS.get(
|
style_conditions(style_type.value, style_type.name)
|
||||||
style_type.name)
|
|
||||||
value_not_in_possible_values_list = cleaned_value not in LIVECARTA_STYLE_ATTRS[
|
|
||||||
style_type.name]
|
|
||||||
if constraints_on_value and value_not_in_possible_values_list:
|
if constraints_on_value and value_not_in_possible_values_list:
|
||||||
# there are constraints + value not in LIVECARTA_STYLE_ATTRS, remove from css file
|
# there are constraints + value not in LIVECARTA_STYLE_ATTRS, remove from css file
|
||||||
css_rule.style[style_type.name] = ""
|
css_rule.style[style_type.name] = ""
|
||||||
@@ -210,7 +213,7 @@ def update_css_styles_to_livecarta_convention(css_rule: cssutils.css.CSSStyleRul
|
|||||||
|
|
||||||
|
|
||||||
def build_css_file_content(css_content: str) -> str:
|
def build_css_file_content(css_content: str) -> str:
|
||||||
"""Build css content with livecarta convention"""
|
"""Build css content with LiveCarta convention"""
|
||||||
sheet = cssutils.parseString(css_content, validate=False)
|
sheet = cssutils.parseString(css_content, validate=False)
|
||||||
|
|
||||||
for css_rule in sheet:
|
for css_rule in sheet:
|
||||||
|
|||||||
@@ -23,20 +23,6 @@ class LiveCartaConfig:
|
|||||||
FONT_CONVERT_RATIO = LIVECARTA_DEFAULT_FONT_SIZE /\
|
FONT_CONVERT_RATIO = LIVECARTA_DEFAULT_FONT_SIZE /\
|
||||||
WORD_DEFAULT_FONT_SIZE
|
WORD_DEFAULT_FONT_SIZE
|
||||||
|
|
||||||
FONT_CORRESPONDANCE_TABLE = {
|
|
||||||
"Arial": "arial,helvetica,sans-serif",
|
|
||||||
"Comic Sans MS": "comic sans ms,cursive",
|
|
||||||
"Courier New": "courier new,courier,monospace",
|
|
||||||
"Georgia": "georgia,serif",
|
|
||||||
"Lucida Sans Unicode": "lucida sans unicode,lucida grande,sans-serif",
|
|
||||||
"Tahoma": "tahoma,geneva,sans-serif",
|
|
||||||
"Times New Roman": "times new roman,times,serif",
|
|
||||||
"Trebuchet MS": "trebuchet ms,helvetica,sans-serif",
|
|
||||||
"Verdana": "verdana,geneva,sans-serif",
|
|
||||||
"monospace": "courier new,courier,monospace",
|
|
||||||
"sans-serif": "arial,helvetica,sans-serif"
|
|
||||||
}
|
|
||||||
|
|
||||||
COLORS_MAP = {
|
COLORS_MAP = {
|
||||||
"#ffff00": "yellow",
|
"#ffff00": "yellow",
|
||||||
"#00ff00": "darkYellow",
|
"#00ff00": "darkYellow",
|
||||||
@@ -116,8 +102,8 @@ class LiveCartaConfig:
|
|||||||
}
|
}
|
||||||
|
|
||||||
WRAP_TAGS_WITH_TABLE = {
|
WRAP_TAGS_WITH_TABLE = {
|
||||||
("div",) :["width", "border", "bgcolor"],
|
("div",): ["width", "border", "bgcolor"],
|
||||||
("section", "blockquote",) : ("class", r"feature[1234]"),
|
("section", "blockquote",): ("class", r"feature[1234]"),
|
||||||
}
|
}
|
||||||
|
|
||||||
"""('what to replace', 'parent tag', 'child tag')"""
|
"""('what to replace', 'parent tag', 'child tag')"""
|
||||||
|
|||||||
Reference in New Issue
Block a user