forked from LiveCarta/BookConverter
Make todos & refactor code
This commit is contained in:
@@ -11,7 +11,8 @@ from src.livecarta_config import LiveCartaConfig
|
||||
|
||||
def save_image_locally(img_file_path, img_content, book_id):
|
||||
folder_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
new_path = pathlib.Path(os.path.join(folder_path, f'../json/img_{book_id}/'))
|
||||
new_path = pathlib.Path(os.path.join(
|
||||
folder_path, f'../json/img_{book_id}/'))
|
||||
new_path.mkdir(exist_ok=True)
|
||||
|
||||
new_img_path = new_path / os.path.basename(img_file_path)
|
||||
@@ -23,7 +24,8 @@ def save_image_locally(img_file_path, img_content, book_id):
|
||||
|
||||
|
||||
def save_image_to_aws(access: Access, img_file_path, img_content: bytes, book_id):
|
||||
link = access.send_image(img_file_path, doc_id=book_id, img_content=img_content)
|
||||
link = access.send_image(
|
||||
img_file_path, doc_id=book_id, img_content=img_content)
|
||||
return link
|
||||
|
||||
|
||||
@@ -37,7 +39,8 @@ def update_src_links_in_images(body_tag: Tag,
|
||||
for img in img_tags:
|
||||
path_to_img_from_html = img.attrs.get('src')
|
||||
html_folder = os.path.dirname(path_to_html)
|
||||
path_to_img_from_root = os.path.normpath(os.path.join(html_folder, path_to_img_from_html)).replace('\\', '/')
|
||||
path_to_img_from_root = os.path.normpath(os.path.join(
|
||||
html_folder, path_to_img_from_html)).replace('\\', '/')
|
||||
|
||||
assert path_to_img_from_root in href2img_content, \
|
||||
f'Image {path_to_img_from_html} in file {path_to_html} was not added to manifest.'
|
||||
@@ -47,10 +50,12 @@ def update_src_links_in_images(body_tag: Tag,
|
||||
if path_to_img_from_root in path2aws_path:
|
||||
new_folder = path2aws_path[path_to_img_from_root]
|
||||
else:
|
||||
new_folder = save_image_to_aws(access, path_to_img_from_root, img_content, 'book_id')
|
||||
new_folder = save_image_to_aws(
|
||||
access, path_to_img_from_root, img_content, 'book_id')
|
||||
path2aws_path[path_to_img_from_root] = new_folder
|
||||
else:
|
||||
new_folder = save_image_locally(path_to_img_from_root, img_content, 'book_id')
|
||||
new_folder = save_image_locally(
|
||||
path_to_img_from_root, img_content, 'book_id')
|
||||
|
||||
img.attrs['src'] = str(new_folder)
|
||||
if img.attrs.get('width'):
|
||||
@@ -71,7 +76,8 @@ def preprocess_table(body_tag: BeautifulSoup):
|
||||
style = td.get('style')
|
||||
width = ''
|
||||
if style:
|
||||
width_match = re.search(r"[^-]width: ?(\d+\.?\d*)(p[tx])", style)
|
||||
width_match = re.search(
|
||||
r"[^-]width: ?(\d+\.?\d*)(p[tx])", style)
|
||||
if width_match:
|
||||
size = width_match.group(1)
|
||||
units = width_match.group(2)
|
||||
@@ -96,10 +102,10 @@ def process_lists(body_tag):
|
||||
"""
|
||||
li_tags = body_tag.find_all("li")
|
||||
|
||||
for il_tag in li_tags:
|
||||
if il_tag.p:
|
||||
il_tag.attrs.update(il_tag.p.attrs)
|
||||
il_tag.p.unwrap()
|
||||
for li_tag in li_tags:
|
||||
if li_tag.p:
|
||||
li_tag.attrs.update(li_tag.p.attrs)
|
||||
li_tag.p.unwrap()
|
||||
|
||||
|
||||
def insert_span_with_attrs_before_tag(main_tag, tag, id_, class_):
|
||||
@@ -111,11 +117,12 @@ def insert_span_with_attrs_before_tag(main_tag, tag, id_, class_):
|
||||
|
||||
|
||||
def clean_headings_content(content: Tag, title: str):
|
||||
def _add_span_to_save_ids_for_links(tag_to_be_removed, body_tag):
|
||||
def add_span_to_save_ids_for_links(tag_to_be_removed, body_tag):
|
||||
if tag_to_be_removed.attrs.get('id'):
|
||||
insert_span_with_attrs_before_tag(body_tag,
|
||||
tag_to_be_removed,
|
||||
id_=tag_to_be_removed.attrs.get('id'),
|
||||
id_=tag_to_be_removed.attrs.get(
|
||||
'id'),
|
||||
class_=tag_to_be_removed.attrs.get('class'))
|
||||
|
||||
for sub_tag in tag_to_be_removed.find_all():
|
||||
@@ -136,10 +143,10 @@ def clean_headings_content(content: Tag, title: str):
|
||||
text = re.sub(r' +', ' ', text).strip()
|
||||
text = text.lower()
|
||||
if title == text:
|
||||
_add_span_to_save_ids_for_links(child, content)
|
||||
add_span_to_save_ids_for_links(child, content)
|
||||
child.extract()
|
||||
elif (title in text) and (child.name in ['h1', 'h2', 'h3']):
|
||||
_add_span_to_save_ids_for_links(child, content)
|
||||
add_span_to_save_ids_for_links(child, content)
|
||||
child.extract()
|
||||
break
|
||||
|
||||
@@ -187,9 +194,12 @@ def preprocess_footnotes(source_html_tag: Tag, href2soup_html: dict = None, note
|
||||
|
||||
"""
|
||||
footnotes = []
|
||||
noterefs_tags = source_html_tag.find_all(attrs={noteref_attr_name: 'noteref'})
|
||||
bad_noterefs_tags = set([tag for tag in noterefs_tags if not tag.attrs.get('href')])
|
||||
noterefs_tags = [tag for tag in noterefs_tags if tag not in bad_noterefs_tags]
|
||||
noterefs_tags = source_html_tag.find_all(
|
||||
attrs={noteref_attr_name: 'noteref'})
|
||||
bad_noterefs_tags = set(
|
||||
[tag for tag in noterefs_tags if not tag.attrs.get('href')])
|
||||
noterefs_tags = [
|
||||
tag for tag in noterefs_tags if tag not in bad_noterefs_tags]
|
||||
new_noterefs_tags = []
|
||||
new_footnotes_tags = []
|
||||
[tag.decompose() for tag in bad_noterefs_tags]
|
||||
@@ -204,7 +214,8 @@ def preprocess_footnotes(source_html_tag: Tag, href2soup_html: dict = None, note
|
||||
if len(tags) == 0:
|
||||
anchored_tags = list(target_html_tag.find_all(id=element_id))
|
||||
if len(anchored_tags):
|
||||
print(f'Warning. Href for tag is detected as footnote:\n{noteref_tag}')
|
||||
print(
|
||||
f'Warning. Href for tag is detected as footnote:\n{noteref_tag}')
|
||||
return anchored_tags
|
||||
else:
|
||||
assert 0, f'Error, No element with id: {href} found.'
|
||||
@@ -219,7 +230,8 @@ def preprocess_footnotes(source_html_tag: Tag, href2soup_html: dict = None, note
|
||||
else:
|
||||
target_html_tag = href2soup_html.get(file)
|
||||
if not target_html_tag:
|
||||
print(f'Error while footnotes processing. For {noteref_tag} invalid path: {file}.')
|
||||
print(
|
||||
f'Error while footnotes processing. For {noteref_tag} invalid path: {file}.')
|
||||
continue
|
||||
|
||||
possible_footnote = 'note|footnote|endnote|rearenote'
|
||||
@@ -230,11 +242,13 @@ def preprocess_footnotes(source_html_tag: Tag, href2soup_html: dict = None, note
|
||||
footnote_tag = expected_footnote_tags[0]
|
||||
if footnote_tag.parent.attrs.get('role') and footnote_tag.parent.attrs.get('role') == 'doc-endnote':
|
||||
footnote_tag = footnote_tag.parent
|
||||
new_noterefs_tags.append(replace_with_livecarta_anchor_tag(noteref_tag, i))
|
||||
new_noterefs_tags.append(
|
||||
replace_with_livecarta_anchor_tag(noteref_tag, i))
|
||||
content = footnote_tag.text
|
||||
# footnote_tag.decompose()
|
||||
footnotes.append(content)
|
||||
footnote_tag = footnote_tag.find(attrs={'role': 'doc-backlink'}) or footnote_tag
|
||||
footnote_tag = footnote_tag.find(
|
||||
attrs={'role': 'doc-backlink'}) or footnote_tag
|
||||
new_footnotes_tags.append(footnote_tag)
|
||||
|
||||
return footnotes, new_noterefs_tags, new_footnotes_tags
|
||||
@@ -262,7 +276,8 @@ def unwrap_structural_tags(body_tag):
|
||||
def _preserve_class_in_aside_tag(tag_):
|
||||
# to save css style inherited from class, copy class to aside tag (which is parent to tag_)
|
||||
# this is for Wiley books with boxes
|
||||
tag_class = tag_.attrs['class'] if not isinstance(tag_.attrs['class'], list) else tag_.attrs['class'][0]
|
||||
tag_class = tag_.attrs['class'] if not isinstance(
|
||||
tag_.attrs['class'], list) else tag_.attrs['class'][0]
|
||||
if tag_.parent.name == 'aside':
|
||||
if not tag_.parent.attrs.get('class'):
|
||||
tag_.parent.attrs['class'] = tag_class
|
||||
@@ -272,7 +287,8 @@ def unwrap_structural_tags(body_tag):
|
||||
# this is for Wiley books with boxes
|
||||
# returns True, if <section> could be unwrapped
|
||||
|
||||
tag_class = tag_.attrs['class'] if not isinstance(tag_.attrs['class'], list) else tag_.attrs['class'][0]
|
||||
tag_class = tag_.attrs['class'] if not isinstance(
|
||||
tag_.attrs['class'], list) else tag_.attrs['class'][0]
|
||||
if 'feature' not in tag_class:
|
||||
return True
|
||||
child_p_tags = tag_.find_all("p")
|
||||
@@ -288,51 +304,56 @@ def unwrap_structural_tags(body_tag):
|
||||
else:
|
||||
return True
|
||||
|
||||
def add_table_to_abc_books(tag_, border, bg_color):
|
||||
wrap_block_tag_with_table(body_tag, old_tag=tag_, width='100', border=border, bg_color=bg_color)
|
||||
|
||||
def add_span_to_save_ids_for_links(tag_to_be_removed):
|
||||
if tag_to_be_removed.attrs.get('id'):
|
||||
insert_span_with_attrs_before_tag(main_tag=body_tag, tag=tag_to_be_removed,
|
||||
id_=tag_to_be_removed.attrs['id'],
|
||||
class_=tag_to_be_removed.attrs.get('class'))
|
||||
|
||||
structural_tags_names = [
|
||||
'div', 'section', 'article', 'main', 'body', 'html', 'aside', 'canvas', 'data',
|
||||
'figure', 'footer', 'iframe', 'span', 'p'
|
||||
]
|
||||
def replace_div_tag_with_table():
|
||||
for div in body_tag.find_all("div"):
|
||||
if div.attrs.get('class'):
|
||||
div_class = div.attrs['class'] if not isinstance(
|
||||
div.attrs['class'], list) else div.attrs['class'][0]
|
||||
if div_class in ['C409', 'C409a']:
|
||||
wrap_block_tag_with_table(
|
||||
body_tag, old_tag=div, width='100', border='solid 3px', bg_color='#e7e7e9')
|
||||
|
||||
elif div_class in ['C441', 'C816']:
|
||||
wrap_block_tag_with_table(
|
||||
body_tag, old_tag=div, width='100', border='solid #6e6e70 1px', bg_color='#e7e7e8')
|
||||
|
||||
if div.attrs.get('style'):
|
||||
if 'background-color' in div.attrs['style']:
|
||||
end_index = div.attrs['style'].find(
|
||||
'background-color') + len('background-color')
|
||||
start_index_of_color = end_index + 2
|
||||
bg_color = div.attrs['style'][start_index_of_color:start_index_of_color + 7]
|
||||
wrap_block_tag_with_table(
|
||||
body_tag, old_tag=div, width='100', border='', bg_color=bg_color)
|
||||
elif div.attrs.get('style') == '':
|
||||
del div.attrs['style']
|
||||
|
||||
structural_tags_names = [
|
||||
'div', 'section', 'article', 'main', 'body', 'html', 'aside', 'canvas', 'data',
|
||||
'figure', 'footer', 'iframe', 'span', 'p'
|
||||
]
|
||||
|
||||
if div.contents:
|
||||
is_not_struct_tag = [
|
||||
child.name not in structural_tags_names for child in div.contents]
|
||||
if all(is_not_struct_tag):
|
||||
div.name = 'p'
|
||||
continue
|
||||
add_span_to_save_ids_for_links(div)
|
||||
div.unwrap()
|
||||
# comments removal
|
||||
for tag in body_tag.find_all():
|
||||
for element in tag(text=lambda text: isinstance(text, Comment)):
|
||||
element.extract()
|
||||
|
||||
for div in body_tag.find_all("div"):
|
||||
if div.attrs.get('class'):
|
||||
div_class = div.attrs['class'] if not isinstance(div.attrs['class'], list) else div.attrs['class'][0]
|
||||
if div_class in ['C409', 'C409a']:
|
||||
add_table_to_abc_books(div, border='solid 3px', bg_color='#e7e7e9')
|
||||
|
||||
elif div_class in ['C441', 'C816']:
|
||||
add_table_to_abc_books(div, border='solid #6e6e70 1px', bg_color='#e7e7e8')
|
||||
|
||||
if div.attrs.get('style'):
|
||||
if 'background-color' in div.attrs['style']:
|
||||
end_index = div.attrs['style'].find('background-color') + len('background-color')
|
||||
start_index_of_color = end_index + 2
|
||||
bg_color = div.attrs['style'][start_index_of_color:start_index_of_color+7]
|
||||
add_table_to_abc_books(div, border='', bg_color=bg_color)
|
||||
|
||||
if div.attrs.get('style') == '':
|
||||
del div.attrs['style']
|
||||
if div.contents:
|
||||
is_not_struct_tag = [child.name not in structural_tags_names for child in div.contents]
|
||||
if all(is_not_struct_tag):
|
||||
div.name = 'p'
|
||||
continue
|
||||
|
||||
add_span_to_save_ids_for_links(div)
|
||||
div.unwrap()
|
||||
replace_div_tag_with_table()
|
||||
|
||||
for s in body_tag.find_all("section"):
|
||||
could_be_unwrapped = True
|
||||
@@ -348,7 +369,8 @@ def unwrap_structural_tags(body_tag):
|
||||
|
||||
for s in body_tag.find_all("figure"):
|
||||
s.name = 'p'
|
||||
s.attrs['style'] = "text-align: center;" # to center image inside this tag
|
||||
# to center image inside this tag
|
||||
s.attrs['style'] = "text-align: center;"
|
||||
|
||||
for s in body_tag.find_all("figcaption"):
|
||||
add_span_to_save_ids_for_links(s)
|
||||
@@ -383,7 +405,8 @@ def unwrap_structural_tags(body_tag):
|
||||
x.parent.unwrap() # todo warning! could reflect on formatting/internal links in some cases
|
||||
|
||||
parents_marks_are_body = [x.parent == body_tag for x in marks]
|
||||
assert all(parents_marks_are_body), 'Anchor for chapter is deeper than 2 level. Chapters can not be parsed.'
|
||||
assert all(
|
||||
parents_marks_are_body), 'Anchor for chapter is deeper than 2 level. Chapters can not be parsed.'
|
||||
|
||||
heading_tag_to_p_tag(body_tag)
|
||||
|
||||
@@ -411,7 +434,8 @@ def get_tags_between_chapter_marks(first_id, href, html_soup):
|
||||
:param html_soup: soup object of current file
|
||||
:return: list [Tag, NavigableString]; chapter's tags
|
||||
"""
|
||||
marked_tags = html_soup.find(attrs={'id': first_id, 'class': 'converter-chapter-mark'})
|
||||
marked_tags = html_soup.find(
|
||||
attrs={'id': first_id, 'class': 'converter-chapter-mark'})
|
||||
if marked_tags:
|
||||
next_tag = marked_tags.next_sibling
|
||||
tags = []
|
||||
@@ -484,16 +508,20 @@ def preprocess_block_tags(chapter_tag):
|
||||
if block.attrs.get('class') in ['feature1', 'feature2', 'feature3', 'feature4']:
|
||||
clean_wiley_block(block)
|
||||
|
||||
color = '#DDDDDD' if block.attrs.get('class') == 'feature1' else None
|
||||
color = '#EEEEEE' if block.attrs.get('class') == 'feature2' else color
|
||||
color = '#DDDDDD' if block.attrs.get(
|
||||
'class') == 'feature1' else None
|
||||
color = '#EEEEEE' if block.attrs.get(
|
||||
'class') == 'feature2' else color
|
||||
wrap_block_tag_with_table(chapter_tag, block, bg_color=color)
|
||||
block.insert_after(BeautifulSoup(features='lxml').new_tag("br"))
|
||||
block.unwrap()
|
||||
|
||||
for future_block in chapter_tag.find_all("p", attrs={"class": re.compile("feature[1234]")}):
|
||||
clean_wiley_block(future_block)
|
||||
color = '#DDDDDD' if future_block.attrs.get('class') == 'feature1' else None
|
||||
color = '#EEEEEE' if future_block.attrs.get('class') == 'feature2' else color
|
||||
color = '#DDDDDD' if future_block.attrs.get(
|
||||
'class') == 'feature1' else None
|
||||
color = '#EEEEEE' if future_block.attrs.get(
|
||||
'class') == 'feature2' else color
|
||||
wrap_block_tag_with_table(chapter_tag, future_block, bg_color=color)
|
||||
|
||||
|
||||
@@ -512,7 +540,8 @@ def preprocess_pre_tags(chapter_tag):
|
||||
new_tag = BeautifulSoup(features='lxml').new_tag("span")
|
||||
new_tag.attrs = pre.attrs.copy()
|
||||
spans = pre.find_all("span")
|
||||
to_add_br = len(spans) > 1 # if in <pre> there are multiple <span>, we need to add <br> after each content
|
||||
# if in <pre> there are multiple <span>, we need to add <br> after each content
|
||||
to_add_br = len(spans) > 1
|
||||
|
||||
for child in pre.children:
|
||||
if isinstance(child, NavigableString):
|
||||
@@ -520,7 +549,8 @@ def preprocess_pre_tags(chapter_tag):
|
||||
sub_strings = re.split('\r\n|\n|\r', cleaned_text)
|
||||
for string in sub_strings:
|
||||
new_tag.append(NavigableString(string))
|
||||
new_tag.append(BeautifulSoup(features='lxml').new_tag('br'))
|
||||
new_tag.append(BeautifulSoup(
|
||||
features='lxml').new_tag('br'))
|
||||
else:
|
||||
for sub_child in child.children:
|
||||
if isinstance(sub_child, NavigableString):
|
||||
@@ -531,7 +561,8 @@ def preprocess_pre_tags(chapter_tag):
|
||||
cleaned_tag = child.extract()
|
||||
new_tag.append(cleaned_tag)
|
||||
if to_add_br:
|
||||
new_tag.append(BeautifulSoup(features='lxml').new_tag('br'))
|
||||
new_tag.append(BeautifulSoup(
|
||||
features='lxml').new_tag('br'))
|
||||
|
||||
new_tag.attrs['style'] = "font-family: courier new,courier,monospace; " \
|
||||
"font-size: 14px; white-space: nowrap;"
|
||||
@@ -551,40 +582,41 @@ def preprocess_code_tags(chapter_tag):
|
||||
code.attrs['style'] = 'color:#c7254e; font-size: 14px; font-family: courier new,courier,monospace;'
|
||||
|
||||
|
||||
def prepare_title_and_content(title, chapter_tag: BeautifulSoup, remove_title_from_chapter) -> Tuple[str, str]:
|
||||
def prepare_title(title_of_chapter: str) -> str:
|
||||
"""
|
||||
Final processing/cleaning function.
|
||||
|
||||
:param title: title of the chapter
|
||||
:param chapter_tag: soup object
|
||||
:param remove_title_from_chapter: bool
|
||||
:return: tuple[str, str]
|
||||
"""
|
||||
title_str = BeautifulSoup(title, features='lxml').string
|
||||
title_str = BeautifulSoup(title_of_chapter, features='lxml').string
|
||||
title_str = re.sub(r'([\n\t\xa0])', ' ', title_str)
|
||||
title_str = re.sub(r' +', ' ', title_str).rstrip()
|
||||
title_str = clean_title_from_numbering(title_str)
|
||||
return title_str
|
||||
|
||||
|
||||
def prepare_content(title_str: str, content_tag: BeautifulSoup, remove_title_from_chapter: bool) -> str:
|
||||
"""
|
||||
Final processing/cleaning function.
|
||||
"""
|
||||
# 0. cleaning \n
|
||||
to_remove = []
|
||||
for child in chapter_tag.contents:
|
||||
for child in content_tag.contents:
|
||||
if isinstance(child, NavigableString):
|
||||
s = re.sub(r'([\n\t])', '', child.string)
|
||||
if s == '':
|
||||
to_remove.append(child)
|
||||
|
||||
[x.extract() for x in to_remove]
|
||||
# 1. heading removal
|
||||
if remove_title_from_chapter:
|
||||
clean_headings_content(chapter_tag, title_str)
|
||||
process_lists(chapter_tag)
|
||||
preprocess_table(chapter_tag)
|
||||
preprocess_code_tags(chapter_tag)
|
||||
preprocess_pre_tags(chapter_tag)
|
||||
preprocess_block_tags(chapter_tag)
|
||||
clean_headings_content(content_tag, title_str)
|
||||
process_lists(content_tag)
|
||||
preprocess_table(content_tag)
|
||||
preprocess_code_tags(content_tag)
|
||||
preprocess_pre_tags(content_tag)
|
||||
preprocess_block_tags(content_tag)
|
||||
|
||||
# 2. class removal
|
||||
for tag in chapter_tag.find_all(recursive=True):
|
||||
for tag in content_tag.find_all(recursive=True):
|
||||
if hasattr(tag, 'attrs') and tag.attrs.get('class') and (tag.attrs.get('class') not in ['link-anchor',
|
||||
'footnote-element']):
|
||||
del tag.attrs['class']
|
||||
# content_str = re.sub(r'([\n\t\xa0])', ' ', str(content_tag))
|
||||
title_str = clean_title_from_numbering(title_str)
|
||||
return title_str, str(chapter_tag)
|
||||
return str(content_tag)
|
||||
Reference in New Issue
Block a user