forked from LiveCarta/BookConverter
Formatting
This commit is contained in:
@@ -14,21 +14,23 @@ from src.livecarta_config import LiveCartaConfig
|
||||
cssutils.log.setLevel(CRITICAL)
|
||||
|
||||
|
||||
sizes_pr = [-100, -1, 0.5, 0.56, 0.63, 0.69, 0.75, 0.81, 0.88, 0.94, 1.0, 1.06, 1.13, 1.19, 1.25, 1.31, 1.38, 1.44, 1.5, 1.56,
|
||||
1.63, 1.69, 1.75, 1.81, 1.88, 1.94, 2.0, 2.06, 2.13, 2.19, 2.25, 2.31, 2.38, 2.44, 2.5, 2.56, 2.63, 2.69,
|
||||
2.75, 2.81, 2.88, 2.94, 3.0, 4.0, 5.0]
|
||||
sizes_pr = [-100, -1, 0.5, 0.56, 0.63, 0.69, 0.75, 0.81, 0.88, 0.94, 1.0,
|
||||
1.06, 1.13, 1.19, 1.25, 1.31, 1.38, 1.44, 1.5, 1.56, 1.63, 1.69,
|
||||
1.75, 1.81, 1.88, 1.94, 2.0, 2.06, 2.13, 2.19, 2.25, 2.31, 2.38,
|
||||
2.44, 2.5, 2.56, 2.63, 2.69, 2.75, 2.81, 2.88, 2.94, 3.0, 4.0, 5.0]
|
||||
|
||||
sizes_px = ['0px', '10px', '10px', '11px', '12px', '13px', '14px', '15px', '16px', '17px', '18px', '19px', '20px', '21px',
|
||||
'22px', '23px', '24px', '25px', '26px', '27px', '28px', '29px', '30px', '31px', '32px', '33px', '34px',
|
||||
'35px', '36px', '37px', '38px', '39px', '40px', '41px', '42px', '43px', '44px', '45px', '46px', '47px',
|
||||
'48px', '49px', '50px', '64px', '72px']
|
||||
sizes_px = ['0px', '10px', '10px', '11px', '12px', '13px', '14px', '15px', '16px',
|
||||
'17px', '18px', '19px', '20px', '21px', '22px', '23px', '24px', '25px',
|
||||
'26px', '27px', '28px', '29px', '30px', '31px', '32px', '33px', '34px',
|
||||
'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_tag_values(value: str) -> str:
|
||||
def convert_tag_style_values(value: str) -> str:
|
||||
"""
|
||||
Function
|
||||
- converts values of tags from em/%/pt to px
|
||||
@@ -42,8 +44,8 @@ def convert_tag_values(value: str) -> str:
|
||||
value: str
|
||||
|
||||
"""
|
||||
def find_closest_size(value):
|
||||
possible_sizes = list(takewhile(lambda x: value > x, sizes_pr))
|
||||
def find_closest_size(size_value):
|
||||
possible_sizes = list(takewhile(lambda x: size_value > x, sizes_pr))
|
||||
last_possible_size_index = sizes_pr.index(possible_sizes[-1])
|
||||
return sizes_px[last_possible_size_index]
|
||||
|
||||
@@ -122,12 +124,13 @@ Warning, if LIVECARTA_STYLE_ATTRS is changed, LIVECARTA_STYLE_ATTRS_MAPPING shou
|
||||
to suit livecarta style convention.
|
||||
"""
|
||||
LIVECARTA_STYLE_ATTRS_MAPPING = {
|
||||
'text-indent': convert_tag_values,
|
||||
'text-indent': convert_tag_style_values,
|
||||
'font-variant': lambda x: x,
|
||||
'text-align': lambda x: x,
|
||||
'font': lambda x: '',
|
||||
'font-family': lambda x: LiveCartaConfig.font_correspondence_table.get(x) or LiveCartaConfig.font_correspondence_table.get(x.capitalize()),
|
||||
'font-size': convert_tag_values,
|
||||
'font-family': lambda x: LiveCartaConfig.font_correspondence_table.get(x) or
|
||||
LiveCartaConfig.font_correspondence_table.get(x.capitalize()),
|
||||
'font-size': convert_tag_style_values,
|
||||
'color': get_text_color,
|
||||
'background-color': get_bg_color,
|
||||
'background': get_bg_color,
|
||||
@@ -140,9 +143,9 @@ LIVECARTA_STYLE_ATTRS_MAPPING = {
|
||||
'border-bottom': 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',
|
||||
'margin-left': convert_tag_values,
|
||||
'margin-top': convert_tag_values,
|
||||
'margin': convert_tag_values,
|
||||
'margin-left': convert_tag_style_values,
|
||||
'margin-top': convert_tag_style_values,
|
||||
'margin': convert_tag_style_values,
|
||||
}
|
||||
|
||||
"""
|
||||
@@ -269,10 +272,10 @@ class TagStyleConverter:
|
||||
item = item.split(':')
|
||||
if item[0] in ['text-indent', 'margin-left', 'margin']:
|
||||
if len(item[1].split(' ')) == 3:
|
||||
item[1] = convert_tag_values(item[1].split(
|
||||
item[1] = convert_tag_style_values(item[1].split(
|
||||
' ')[-2]) # split returns middle value
|
||||
else:
|
||||
item[1] = convert_tag_values(item[1].split(
|
||||
item[1] = convert_tag_style_values(item[1].split(
|
||||
' ')[-1]) # split returns last value
|
||||
clean_style += item[0] + ': ' + item[1] + '; '
|
||||
|
||||
@@ -343,7 +346,8 @@ class TagStyleConverter:
|
||||
|
||||
split_inline_style: dict = remove_extra_spaces(inline_style)
|
||||
|
||||
# repetition check - if the tag had already had inline style that isn't in the css styles, add this to style parsed from css
|
||||
# repetition check - if the tag had already had inline style
|
||||
# that isn't in the css styles, add this to style parsed from css
|
||||
repeat_styles = list(set(split_ultimate_style.keys())
|
||||
& set(split_inline_style.keys()))
|
||||
|
||||
@@ -409,7 +413,8 @@ class TagStyleConverter:
|
||||
if has_p_style_attrs:
|
||||
p_style += item + ';'
|
||||
initial_style = initial_style.replace(item + ';', '')
|
||||
# here check that this style i exactly the same. Not 'align' when we have 'text-align', or 'border' when we have 'border-top'
|
||||
# here check that this style i exactly the same.
|
||||
# Not 'align' when we have 'text-align', or 'border' when we have 'border-top'
|
||||
styles_to_be_saved_in_span = [((attr + ':') in initial_style) & (
|
||||
'-' + attr not in initial_style) for attr in styles_cant_be_in_p]
|
||||
if any(styles_to_be_saved_in_span):
|
||||
@@ -549,4 +554,4 @@ if __name__ == '__main__':
|
||||
'pr01s05.xhtml').get_body_content().decode()
|
||||
html_soup = BeautifulSoup(html_, features='lxml')
|
||||
|
||||
print(convert_html_soup_with_css_style(html_soup, css_cleaned))
|
||||
print(convert_html_soup_with_css_style(html_soup, css_cleaned))
|
||||
|
||||
Reference in New Issue
Block a user