epub converter: fix list-type adding

This commit is contained in:
shirshasa
2021-07-12 08:13:21 +03:00
parent a078a7a42a
commit 21d2c09de4

View File

@@ -87,8 +87,8 @@ LIVECARTA_STYLE_ATTRS = {
'border-left-width': [],
'border-bottom-width': [],
'border': [],
# 'list-style-type': [],
# 'list-style-image': []
'list-style-type': [],
'list-style-image': []
}
"""
@@ -126,8 +126,8 @@ LIVECARTA_STYLE_ATTRS_MAPPING = {
'border-right-width': lambda x: x if x != '0' else '',
'border-left-width': lambda x: x if x != '0' else '',
'border-bottom-width': lambda x: x if x != '0' else '',
# 'list-style-type': lambda x: x if x in list_types else 'disc',
# 'list-style-image': lambda x: 'disc'
'list-style-type': lambda x: x if x in list_types else 'disc',
'list-style-image': lambda x: 'disc'
}
"""
@@ -226,6 +226,7 @@ class TagStyleConverter:
style = self.tag_with_style.attrs.get('style') + ';'
style = self.remove_white_if_no_bgcolor(style, self.tag_with_style)
style = style.replace('background:', 'background-color:')
style = style.replace('list-style-image', 'list-style-type')
return style
def change_attrs_with_corresponding_tags(self):
@@ -283,34 +284,41 @@ class TagStyleConverter:
p_tag.attrs['style'] = new_style
li_attrs_regexp = re.compile(r'(list-style-type:(\w+);)')
has_li_style_attr = re.search(li_attrs_regexp, old_style)
old_style = old_style if not has_li_style_attr else old_style.replace(has_li_style_attr.group(1), '')
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', 'list-style-type']]
if t.name == 'li' and t.attrs.get('style'):
styles_cant_be_in_li = [attr for attr in LIVECARTA_STYLE_ATTRS if
attr not in ['text-align', 'list-style-type']]
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']
new_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
for possible_li_attrs_regexp in [re.compile(r'(text-align:(\w+);)'),
re.compile(r'(list-style-type:(\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_in_ul(t):
styles_cant_be_in_li = [attr for attr in LIVECARTA_STYLE_ATTRS if attr not in ['list-style-type']]
def add_span_to_save_style_attrs_in_ul_ol(t):
if t.name in ['ul', 'ol'] and t.attrs.get('style'):
styles_cant_be_in_li = [attr for attr in LIVECARTA_STYLE_ATTRS if attr not in ['list-style-type']]
if t.name == 'ul' 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'
@@ -321,27 +329,30 @@ class TagStyleConverter:
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), '')
old_style = old_style.replace(new_style, '')
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'(^ol$)(^ul$)|' + h_regex)
no_style_in_livecarta_regexp = re.compile('(^h[1-9]$)')
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)
style = t.attrs['style']
li_attrs_regexp = re.compile(r'(list-style-type:(\w+);)')
has_li_style_attr = re.search(li_attrs_regexp, style)
t.attrs['style'] = style if not has_li_style_attr else style.replace(has_li_style_attr.group(1), '')
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_in_ul(self.tag)
self.add_span_to_save_style_attrs_in_ul_ol(self.tag)
self.add_span_to_save_style_attrs(self.tag)
return self.tag