diff --git a/src/epub_converter/css_preprocessing.py b/src/epub_converter/css_preprocessing.py index ff4a347..fa72c12 100644 --- a/src/epub_converter/css_preprocessing.py +++ b/src/epub_converter/css_preprocessing.py @@ -5,7 +5,7 @@ from ebooklib import epub from bs4 import BeautifulSoup from itertools import takewhile -from src.util.color_reader import str2hex +from src.util.color_reader import str2hex, rgb2color_name from src.livecarta_config import LiveCartaConfig @@ -129,8 +129,7 @@ LIVECARTA_STYLE_ATTRS_MAPPING = { 'font-variant': lambda x: x, 'text-align': lambda x: x, 'font': lambda x: '', - 'font-family': lambda x: LiveCartaConfig.FONT_CORRESPONDANCE_TABLE.get(x) or - LiveCartaConfig.FONT_CORRESPONDANCE_TABLE.get(x.capitalize()), + 'font-family': lambda x: LiveCartaConfig.FONT_CORRESPONDANCE_TABLE.get(re.sub(r"^\s+|\s+$", "", x.title())), 'font-size': convert_tag_style_values, 'color': get_text_color, 'background-color': get_bg_color, @@ -179,7 +178,7 @@ def build_inline_style_content(style: str) -> str: """Build inline style with livecarta convention""" # replace all spaces between '; & letter' to ';' 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 # remove it split_style: list = list(filter(None, style.split(';'))) # replace all spaces between ': & letter' to ':' @@ -198,7 +197,7 @@ def update_css_styles_to_livecarta_convention(css_rule: cssutils.css.CSSStyleRul css_rule.style[style_type.name] = '' return - cleaned_value = style_type.value.replace('\"', '').split(', ')[-1] + cleaned_value = style_type.value.replace('\"', '') constraints_on_value = LIVECARTA_STYLE_ATTRS.get( style_type.name) value_not_in_possible_values_list = cleaned_value not in LIVECARTA_STYLE_ATTRS[ @@ -215,7 +214,7 @@ def update_css_styles_to_livecarta_convention(css_rule: cssutils.css.CSSStyleRul def build_css_file_content(css_content: str) -> str: """Build css content with livecarta convention""" - sheet = cssutils.parseString(css_content, validate=False) + sheet = cssutils.parseString(css_content.lower(), validate=False) for css_rule in sheet: if css_rule.type == css_rule.STYLE_RULE: diff --git a/src/util/color_reader.py b/src/util/color_reader.py index 706da61..fe44758 100644 --- a/src/util/color_reader.py +++ b/src/util/color_reader.py @@ -103,13 +103,12 @@ def str2hex(s: str): return rgb_percent_to_hex((r, g, b)) if 'rgb' in s: - match = re.search(r'rgba*\(((\d+), *(\d+), *(\d+), (\d\.\d+)*)\)', s) - if match: - r, g, b = int(match.group(2)), int(match.group(3)), int(match.group(4)) - if match.group(5): - alpha = float(match.group(5)) - r, g, b = rgba2rgb(r, g, b, alpha) - return rgb_to_hex((r, g, b)) + rgba = re.findall('([0-9] *\.?[0-9]+)', s) + r, g, b = int(rgba[0]), int(rgba[1]), int(rgba[2]) + if len(rgba) == 4: + alpha = float(rgba[3]) + r, g, b = rgba2rgb(r, g, b, alpha) + return rgb_to_hex((r, g, b)) if 'hsl' in s: # hsl(hue in {0,360}, saturation [0, 100%], lightness [0, 100%])