From 3216ba2b93ee5a2526f7d36f993f7f5db9ad459d Mon Sep 17 00:00:00 2001 From: Kiryl Date: Fri, 28 Jan 2022 11:33:12 +0300 Subject: [PATCH] Make a comparison(inline-css) by style name --- src/epub_converter/css_reader.py | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/src/epub_converter/css_reader.py b/src/epub_converter/css_reader.py index 37fdc4b..313b3a5 100644 --- a/src/epub_converter/css_reader.py +++ b/src/epub_converter/css_reader.py @@ -266,8 +266,9 @@ class TagStyleConverter: return style_ @staticmethod - def process_indents_to_px(split_style: list) -> str: + def process_indents_to_px(split_style: dict) -> str: """ Function cleans using convert_indents() style string and returns new clean_style """ + split_style = [k + ":" + v for k, v in split_style.items()] clean_style = '' for item in split_style: item = item.split(':') @@ -307,15 +308,12 @@ class TagStyleConverter: return clean_style def preprocess_style(self): - def remove_extra_spaces(style: str) -> List: + def remove_extra_spaces(style: str) -> dict: """ Function to remove extra spaces in style to process clean_style """ # replace all spaces between '; & letter' to ';' style = re.sub(r"; *", ";", style) split_style: List = style.split(';') - # result = {} - # for list_item in split_style: - # key, val = list_item.split(":") - # result[key] = val + # when we split style by ; and we have at the end ; that's why we have '' in list while '' in split_style: split_style.remove('') @@ -323,7 +321,11 @@ class TagStyleConverter: # replace all spaces between ': & letter' to ':' split_style = [el.replace( re.search(r'(:\s*)', el).group(1), ':') for el in split_style] - return split_style + dict = {} + for list_item in split_style: + key, val = list_item.split(":") + dict[key] = val + return dict ultimate_style = self.tag_with_ultimate_style.attrs.get('style') + ';' ultimate_style = self.remove_white_if_no_bgcolor( @@ -332,18 +334,21 @@ class TagStyleConverter: 'background:', 'background-color:') ultimate_style = ultimate_style.replace( 'list-style-image', 'list-style-type') - split_ultimate_style: List = remove_extra_spaces(ultimate_style) + + split_ultimate_style: dict = remove_extra_spaces(ultimate_style) + ultimate_style: str = self.process_indents_to_px(split_ultimate_style) if self.tag_with_inline_style.attrs.get('style'): inline_style = self.tag_with_inline_style.attrs['style'] - split_inline_style: List = remove_extra_spaces(inline_style) + split_inline_style: dict = remove_extra_spaces(inline_style) # repetition check - if the tag had already had inline style that isn't in the css styles, add this to style parsed from css - repeat_styles = list(set(split_ultimate_style) - & set(split_inline_style)) - for item in repeat_styles: - split_inline_style.remove(item) + repeat_styles = list(set(split_ultimate_style.keys()) + & set(split_inline_style.keys())) + + # remove styles(css) that are in css and inline + [split_inline_style.pop(item) for item in repeat_styles] if split_inline_style: # if split_inline_style is not empty - start convert and add to ultimate style @@ -352,7 +357,6 @@ class TagStyleConverter: split_inline_style) ultimate_style += inline_style - ultimate_style: str = self.process_indents_to_px(split_ultimate_style) return ultimate_style def change_attrs_with_corresponding_tags(self):