epub converter: update ul processing

This commit is contained in:
shirshasa
2021-07-08 15:10:36 +03:00
parent 00b808eaad
commit 8863154a05

View File

@@ -23,6 +23,10 @@ sizes_px = ['10px', '10px', '11px', '12px', '13px', '14px', '15px', '16px', '17p
'35px', '36px', '37px', '38px', '39px', '40px', '41px', '42px', '43px', '44px', '45px', '46px', '47px',
'48px', '49px', '50px', '64px', '72px']
list_types = ['circle', 'disc', 'armenian','decimal',
'decimal-leading-zero', 'georgian', 'lower-alpha','lower-latin',
'lower-roman', 'upper-alpha', 'upper-latin', 'upper-roman', 'none' ]
def convert_font_size(value):
if 'pt' in value:
@@ -82,7 +86,9 @@ LIVECARTA_STYLE_ATTRS = {
'border-right-width': [],
'border-left-width': [],
'border-bottom-width': [],
'border': []
'border': [],
'list-style-type': [],
'list-style-image': []
}
"""
@@ -120,6 +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'
}
"""
@@ -280,7 +288,7 @@ class TagStyleConverter:
@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']]
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'):
check = [attr in t.attrs.get('style') for attr in styles_cant_be_in_li]
@@ -298,10 +306,30 @@ class TagStyleConverter:
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']]
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'
li_tag = BeautifulSoup(features='lxml').new_tag('ul')
old_style = t.attrs['style']
possible_li_attrs_regexp = 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(t):
h_regex = f'(^h[1-9]$)'
no_style_in_livecarta_regexp = re.compile(r'(^ul$)|(^ol$)|' + h_regex)
no_style_in_livecarta_regexp = re.compile(r'(^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)
@@ -313,6 +341,7 @@ class TagStyleConverter:
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(self.tag)
return self.tag