forked from LiveCarta/BookConverter
Wrote documentation for every func/class in .py
This commit is contained in:
@@ -11,9 +11,9 @@ from itertools import takewhile
|
||||
from src.util.color_reader import str2hex
|
||||
from src.livecarta_config import LiveCartaConfig
|
||||
|
||||
|
||||
cssutils.log.setLevel(CRITICAL)
|
||||
|
||||
|
||||
sizes_pr = [-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]
|
||||
@@ -29,6 +29,7 @@ list_types = ['circle', 'disc', 'armenian', 'decimal',
|
||||
|
||||
|
||||
def convert_font_size(value):
|
||||
""" Function converts font-size in mapping """
|
||||
if 'pt' in value:
|
||||
if int(value.replace('pt', '')) == LiveCartaConfig.LIVECARTA_DEFAULT_FONT_SIZE:
|
||||
return ''
|
||||
@@ -58,6 +59,7 @@ def convert_font_size(value):
|
||||
|
||||
|
||||
def convert_indents(value):
|
||||
""" Function converts text-indent and margin-left values to px """
|
||||
# 30px = 3.2% = 1.25em = 23pt
|
||||
text_indent_regexp = re.compile(r'(-*\w+%)|((-*\w*).*em)|(-*\w+pt)')
|
||||
has_style_attrs = re.search(text_indent_regexp, value)
|
||||
@@ -115,13 +117,6 @@ LIVECARTA_STYLE_ATTRS = {
|
||||
'margin-left': []
|
||||
}
|
||||
|
||||
"""
|
||||
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 = str2hex(x)
|
||||
@@ -135,6 +130,12 @@ def get_text_color(x):
|
||||
return color
|
||||
|
||||
|
||||
"""
|
||||
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.
|
||||
"""
|
||||
LIVECARTA_STYLE_ATTRS_MAPPING = {
|
||||
'text-indent': convert_indents,
|
||||
'font-variant': lambda x: x,
|
||||
@@ -178,8 +179,10 @@ LIVECARTA_STYLE_ATTRS_SHOULD_BE_TAG = {
|
||||
|
||||
|
||||
def check_style_to_be_tag(style) -> List[tuple]:
|
||||
""" Some css style properties converts to tags.
|
||||
Search for them and prepare list of properties to be removed from style string"""
|
||||
"""
|
||||
Some css style properties converts to tags.
|
||||
Search for them and prepare list of properties to be removed from style string
|
||||
"""
|
||||
to_remove = []
|
||||
for k in LIVECARTA_STYLE_ATTRS_SHOULD_BE_TAG:
|
||||
if f'{k[0]}:{k[1]}' in style:
|
||||
@@ -208,6 +211,7 @@ def update_css_style_types_to_livecarta_convention(css_rule, style_type):
|
||||
|
||||
|
||||
def build_css_content(css_content):
|
||||
""" Build css content with livecarta convention """
|
||||
sheet = cssutils.parseString(css_content, validate=False)
|
||||
|
||||
for css_rule in sheet:
|
||||
@@ -231,6 +235,7 @@ class TagStyleConverter:
|
||||
|
||||
@staticmethod
|
||||
def remove_white_if_no_bgcolor(style_, tag):
|
||||
""" Function remove white color if there is no text bg color """
|
||||
if 'background' in style_:
|
||||
return style_
|
||||
|
||||
@@ -260,8 +265,7 @@ class TagStyleConverter:
|
||||
|
||||
@staticmethod
|
||||
def process_indents_to_px(split_style: list) -> str:
|
||||
# clean with convert_indents() style string and make new clean_style
|
||||
|
||||
""" Function cleans using convert_indents() style string and returns new clean_style """
|
||||
clean_style = ''
|
||||
for item in split_style:
|
||||
item = item.split(':')
|
||||
@@ -276,7 +280,7 @@ class TagStyleConverter:
|
||||
|
||||
has_margin_left = re.search(margin_left_regexp, clean_style)
|
||||
has_text_indent = re.search(text_indent_regexp, clean_style)
|
||||
#formula_of_indent: indent = abs(margin_left - text_indent)
|
||||
# formula_of_indent: indent = abs(margin_left - text_indent)
|
||||
if has_margin_left:
|
||||
num_ml = abs(int("".join(
|
||||
filter(str.isdigit, str(has_margin_left.group(2))))))
|
||||
@@ -302,6 +306,7 @@ class TagStyleConverter:
|
||||
|
||||
def preprocess_style(self):
|
||||
def remove_extra_spaces(style: str) -> List:
|
||||
""" Function to remove extra spaces in style to process clean_style """
|
||||
# replace all spaces between '; & letter' to ';'
|
||||
style = re.sub(r"; *", ";", style)
|
||||
split_style = style.split(';')
|
||||
@@ -381,7 +386,7 @@ class TagStyleConverter:
|
||||
|
||||
@staticmethod
|
||||
def wrap_span_in_p_to_save_style_attrs(tag):
|
||||
'''Function designed to save style attrs that cannot be in p -> span'''
|
||||
""" Function designed to save style attrs that cannot be in p -> span """
|
||||
if tag.name == 'p' and tag.attrs.get('style'):
|
||||
styles_cant_be_in_p = [attr for attr in LIVECARTA_STYLE_ATTRS
|
||||
if attr not in ['text-align', 'text-indent', 'border-bottom', 'border-top']]
|
||||
@@ -414,6 +419,7 @@ class TagStyleConverter:
|
||||
|
||||
@staticmethod
|
||||
def wrap_span_in_li_to_save_style_attrs(tag):
|
||||
""" Function designed to save style attrs that cannot be in li -> span """
|
||||
if tag.name == 'li' and tag.attrs.get('style'):
|
||||
styles_cant_be_in_li = [attr for attr in LIVECARTA_STYLE_ATTRS if
|
||||
attr not in ['text-align', 'list-style-type']]
|
||||
@@ -441,6 +447,7 @@ class TagStyleConverter:
|
||||
|
||||
@staticmethod
|
||||
def wrap_span_in_ul_ol_to_save_style_attrs(tag):
|
||||
""" Function designed to save style attrs that cannot be in ul/ol -> span """
|
||||
if tag.name in ['ul', 'ol'] and tag.attrs.get('style'):
|
||||
styles_cant_be_in_ul_ol = [
|
||||
attr for attr in LIVECARTA_STYLE_ATTRS if attr not in ['list-style-type']]
|
||||
@@ -465,6 +472,7 @@ class TagStyleConverter:
|
||||
|
||||
@staticmethod
|
||||
def wrap_span_in_h_to_save_style_attrs(tag):
|
||||
""" Function designed to save style attrs that cannot be in h -> span """
|
||||
h_regexp = re.compile('(^h[1-9]$)')
|
||||
|
||||
if re.search(h_regexp, tag.name) and tag.attrs.get('style'):
|
||||
@@ -487,6 +495,7 @@ class TagStyleConverter:
|
||||
|
||||
|
||||
def convert_html_soup_with_css_style(html_soup: BeautifulSoup, css_text: str):
|
||||
""" Function adds styles from .css to inline style """
|
||||
css_text = css_text.replace(
|
||||
'@namespace epub "http://www.idpf.org/2007/ops";', '')
|
||||
livecarta_tmp_ids = []
|
||||
|
||||
Reference in New Issue
Block a user