From 709ffb5e2d0d4f28e9accc380c91dcfc7bba12f4 Mon Sep 17 00:00:00 2001 From: shirshasa Date: Tue, 6 Jul 2021 10:06:22 +0300 Subject: [PATCH] epub converter: update li,ol,ul,h processing --- src/css_reader.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/css_reader.py b/src/css_reader.py index 911594e..7ea5a37 100644 --- a/src/css_reader.py +++ b/src/css_reader.py @@ -278,10 +278,42 @@ class TagStyleConverter: t.attrs['style'] = old_style t.wrap(p_tag) + @staticmethod + def add_span_to_save_style_attrs_in_li(t): + styles_cant_be_in_li = [attr for attr in LIVECARTA_STYLE_ATTRS if attr not in ['text-align']] + + if t.name == 'li' and t.attrs.get('style'): + check = [attr in t.attrs.get('style') for attr in styles_cant_be_in_li] + if any(check): + t.name = 'span' + li_tag = BeautifulSoup(features='lxml').new_tag('li') + old_style = t.attrs['style'] + + possible_li_attrs_regexp = re.compile(r'(text-align:(\w+);)') + has_li_style_attrs = re.search(possible_li_attrs_regexp, old_style) + if has_li_style_attrs and has_li_style_attrs.group(1): + new_style = has_li_style_attrs.group(1) + old_style = old_style.replace(has_li_style_attrs.group(1), '') + li_tag.attrs['style'] = new_style + t.attrs['style'] = old_style + t.wrap(li_tag) + + @staticmethod + def add_span_to_save_style_attrs(t): + h_regex = f'(^h[1-9]$)' + no_style_in_livecarta_regexp = re.compile(r'(^ul$)|(^ol$)|' + h_regex) + + if re.search(no_style_in_livecarta_regexp, t.name) and t.attrs.get('style'): + new_tag = BeautifulSoup(features='lxml').new_tag(t.name) + t.name = 'span' + t.wrap(new_tag) + def convert_initial_tag(self): del self.tag.attrs['livecarta_id'] self.tag = self.change_attrs_with_corresponding_tags() self.wrap_p_to_save_style_attrs(self.tag) + self.add_span_to_save_style_attrs_in_li(self.tag) + self.add_span_to_save_style_attrs(self.tag) return self.tag