epub converter: update li,ol,ul,h processing

This commit is contained in:
shirshasa
2021-07-06 10:06:22 +03:00
parent 0801645de2
commit 709ffb5e2d

View File

@@ -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