epub converter: add new type of blocks 2

This commit is contained in:
shirshasa
2021-08-23 16:24:38 +03:00
parent 9fb7a7eda2
commit 367bb16a23

View File

@@ -257,16 +257,26 @@ def unwrap_structural_tags(body_tag):
if not tag_.parent.attrs.get('class'): if not tag_.parent.attrs.get('class'):
tag_.parent.attrs['class'] = tag_class tag_.parent.attrs['class'] = tag_class
def _preserve_class_in_section_tag(tag_): def _preserve_class_in_section_tag(tag_) -> bool:
# to save css style inherited from class, copy class to child <p> # to save css style inherited from class, copy class to child <p>
# this is for Wiley books with boxes # 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]
child_p_tag = tag_.find_all("p") if 'feature' not in tag_class:
if len(child_p_tag) != 1: return True
return child_p_tags = tag_.find_all("p")
child_p_tag = child_p_tag[0] if len(child_p_tags) == 1:
if not child_p_tag.attrs.get('class'): child_p_tag = child_p_tags[0]
child_p_tag.attrs['class'] = tag_class if not child_p_tag.attrs.get('class'):
child_p_tag.attrs['class'] = tag_class
return True
elif len(child_p_tags) > 1:
tag_.name = 'p'
return False
else:
return True
def _add_table_to_abc_books(tag_, border, bg_color): 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) wrap_block_tag_with_table(body_tag, old_tag=tag_, width='100', border=border, bg_color=bg_color)
@@ -301,11 +311,13 @@ def unwrap_structural_tags(body_tag):
div.unwrap() div.unwrap()
for s in body_tag.find_all("section"): for s in body_tag.find_all("section"):
could_be_unwrapped = True
if s.attrs.get('class'): if s.attrs.get('class'):
_preserve_class_in_aside_tag(s) _preserve_class_in_aside_tag(s)
_preserve_class_in_section_tag(s) could_be_unwrapped = _preserve_class_in_section_tag(s)
_add_span_to_save_ids_for_links(s) _add_span_to_save_ids_for_links(s)
s.unwrap() if could_be_unwrapped:
s.unwrap()
for s in body_tag.find_all("article"): for s in body_tag.find_all("article"):
_add_span_to_save_ids_for_links(s) _add_span_to_save_ids_for_links(s)