epub converter: fix bgcolor if white text

This commit is contained in:
shirshasa
2021-06-01 18:04:17 +03:00
parent 2b46a16c07
commit 044439e617

View File

@@ -91,6 +91,14 @@ LIVECARTA_STYLE_ATTRS_MAPPING = { property: mapping function }
Warning, if LIVECARTA_STYLE_ATTRS is changed, LIVECARTA_STYLE_ATTRS_MAPPING should be updated
to suit livecarta style convention.
"""
def get_bg_color(x):
color = LawCartaConfig.HTML42LIVECARTA_COLORS.get(str2color_name(x), '')
color = '' if color == 'white' else color
return color
LIVECARTA_STYLE_ATTRS_MAPPING = {
'text-indent': lambda x: LawCartaConfig.INDENT if x != '0' else '',
'font-variant': lambda x: x,
@@ -99,8 +107,8 @@ LIVECARTA_STYLE_ATTRS_MAPPING = {
'font-family': lambda x: LawCartaConfig.font_correspondence_table.get(x.capitalize()),
'font-size': convert_font_size,
'color': lambda x: LawCartaConfig.HTML42LIVECARTA_COLORS.get(str2color_name(x), ''),
'background-color': lambda x: LawCartaConfig.HTML42LIVECARTA_COLORS.get(str2color_name(x), ''),
'background': lambda x: LawCartaConfig.HTML42LIVECARTA_COLORS.get(str2color_name(x), ''),
'background-color': get_bg_color,
'background': get_bg_color,
'border-top-width': lambda x: x if x != '0' else '',
'border-right-width': lambda x: x if x != '0' else '',
'border-left-width': lambda x: x if x != '0' else '',
@@ -174,7 +182,7 @@ def clean_css(css):
def add_inline_style_to_html_soup(soup1, css_text):
livecarta_tmp_ids = []
h_regex = f'(^h[{LawCartaConfig.SUPPORTED_LEVELS + 1}-9]$)'
for i, x in enumerate(soup1.find_all(re.compile('(^p$)|(^span$)|(^li$)|(^ul$)|(^ol$)|(^td$)|' + h_regex))):
for i, x in enumerate(soup1.find_all(re.compile('(^p$)|(^span$)|(^li$)|(^ul$)|(^ol$)|(^td$)|(^th$)|' + h_regex))):
x.attrs['livecarta_id'] = i
livecarta_tmp_ids.append(i)
@@ -184,12 +192,23 @@ def add_inline_style_to_html_soup(soup1, css_text):
disable_validation=True)
soup2 = BeautifulSoup(html_with_inline_style, features='lxml')
def remove_white_if_no_bgcolor(style_):
if ('color:white' in style_) and ('background' not in style_):
style_ = style_.replace('color:white;', '')
return style_
def wrap_p_if_bg_color(t):
if (t.name == 'p') and ('background' in t.attrs.get('style')):
t.name = 'span'
t.wrap( BeautifulSoup(features='lxml').new_tag('p'))
for i in livecarta_tmp_ids:
tag = soup1.find(attrs={'livecarta_id': i})
tag_initial_name = tag.name
tag_with_style = soup2.find(attrs={'livecarta_id': i})
if tag_with_style.attrs.get('style'):
style = tag_with_style.attrs.get('style') + ';'
style = remove_white_if_no_bgcolor(style)
style = style.replace('background:', 'background-color:')
to_remove = check_style_to_be_tag(style)
new_tags = []
@@ -222,9 +241,7 @@ def add_inline_style_to_html_soup(soup1, css_text):
tag.attrs['style'] = style
del tag.attrs['livecarta_id']
if(tag.name == 'p') and ('background-color' in tag.attrs.get('style')):
tag.name = 'span'
print(tag)
wrap_p_if_bg_color(tag)
else:
del tag.attrs['livecarta_id']