forked from LiveCarta/BookConverter
Add function - insert certain tags in parent tags
This commit is contained in:
@@ -210,28 +210,6 @@ def _remove_headings_content(content_tag, title_of_chapter: str):
|
|||||||
break
|
break
|
||||||
|
|
||||||
|
|
||||||
# todo remove
|
|
||||||
def _process_lists(chapter_tag: BeautifulSoup):
|
|
||||||
"""
|
|
||||||
Function
|
|
||||||
- process tags <li>.
|
|
||||||
- unwrap <p> tags.
|
|
||||||
Parameters
|
|
||||||
----------
|
|
||||||
chapter_tag: Tag, soup object
|
|
||||||
|
|
||||||
Returns
|
|
||||||
-------
|
|
||||||
None
|
|
||||||
|
|
||||||
"""
|
|
||||||
li_tags = chapter_tag.find_all("li")
|
|
||||||
for li_tag in li_tags:
|
|
||||||
if li_tag.p:
|
|
||||||
li_tag.attrs.update(li_tag.p.attrs)
|
|
||||||
li_tag.p.unwrap()
|
|
||||||
|
|
||||||
|
|
||||||
def _preprocess_table(chapter_tag: BeautifulSoup):
|
def _preprocess_table(chapter_tag: BeautifulSoup):
|
||||||
"""Function to preprocess tables and tags(td|th|tr): style"""
|
"""Function to preprocess tables and tags(td|th|tr): style"""
|
||||||
tables = chapter_tag.find_all("table")
|
tables = chapter_tag.find_all("table")
|
||||||
@@ -257,53 +235,20 @@ def _preprocess_table(chapter_tag: BeautifulSoup):
|
|||||||
table.attrs["border"] = "1"
|
table.attrs["border"] = "1"
|
||||||
|
|
||||||
|
|
||||||
def _preprocess_code_tags(chapter_tag: BeautifulSoup):
|
def _insert_tags_in_parents(chapter_tag):
|
||||||
"""
|
parent_tag2condition = {parent[0]: parent[1] for parent in LiveCartaConfig.INSERT_TAG_IN_PARENT_TAG.keys()}
|
||||||
Function
|
for parent_tag_name, condition in parent_tag2condition.items():
|
||||||
- transform <code>, <kdb>, <var> tags into span
|
for parent_tag in chapter_tag.select(parent_tag_name):
|
||||||
- add code style to this tags (if there is no)
|
if parent_tag.select(condition):
|
||||||
Parameters
|
|
||||||
----------
|
|
||||||
chapter_tag: Tag, soup object
|
|
||||||
|
|
||||||
Returns
|
|
||||||
-------
|
|
||||||
None
|
|
||||||
|
|
||||||
"""
|
|
||||||
for code in chapter_tag.find_all(re.compile("code|kbd|var")):
|
|
||||||
if not code.parent.name == "pre":
|
|
||||||
code.name = "span"
|
|
||||||
if not code.attrs.get("style"):
|
|
||||||
code.attrs["style"] = "font-size: 14px; font-family: courier new,courier,monospace;"
|
|
||||||
continue
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def _preprocess_pre_tags(chapter_tag: BeautifulSoup):
|
|
||||||
"""
|
|
||||||
Function preprocessing <pre> tags
|
|
||||||
Wrap string of the tag with <code> if its necessary
|
|
||||||
Parameters
|
|
||||||
----------
|
|
||||||
chapter_tag: Tag, soup object
|
|
||||||
|
|
||||||
Returns
|
|
||||||
----------
|
|
||||||
None
|
|
||||||
Modified chapter tag
|
|
||||||
|
|
||||||
"""
|
|
||||||
for pre in chapter_tag.find_all("pre"):
|
|
||||||
if pre.find_all("code|kbd|var"):
|
|
||||||
continue
|
continue
|
||||||
else:
|
else:
|
||||||
code = chapter_tag.new_tag("code")
|
tag_to_insert = chapter_tag.new_tag(
|
||||||
|
LiveCartaConfig.INSERT_TAG_IN_PARENT_TAG[(parent_tag_name, condition)])
|
||||||
# insert all items that was in pre to code and remove from pre
|
# insert all items that was in pre to code and remove from pre
|
||||||
for content in reversed(pre.contents):
|
for content in reversed(parent_tag.contents):
|
||||||
code.insert(0, content.extract())
|
tag_to_insert.insert(0, content.extract())
|
||||||
# wrap code with items
|
# wrap code with items
|
||||||
pre.append(code)
|
parent_tag.append(tag_to_insert)
|
||||||
|
|
||||||
|
|
||||||
def _class_removing(chapter_tag):
|
def _class_removing(chapter_tag):
|
||||||
@@ -353,10 +298,8 @@ def prepare_content(title_str: str, content_tag: BeautifulSoup, remove_title_fro
|
|||||||
_remove_headings_content(content_tag, title_str)
|
_remove_headings_content(content_tag, title_str)
|
||||||
|
|
||||||
# 4. processing tags (<li>, <table>, <code>, <pre>, <div>, <block>)
|
# 4. processing tags (<li>, <table>, <code>, <pre>, <div>, <block>)
|
||||||
_process_lists(content_tag) # todo regex
|
|
||||||
_preprocess_table(content_tag)
|
_preprocess_table(content_tag)
|
||||||
_preprocess_code_tags(content_tag) # todo regex
|
_insert_tags_in_parents(content_tag)
|
||||||
_preprocess_pre_tags(content_tag) # todo regex
|
|
||||||
|
|
||||||
# 5. remove classes that weren't created by converter
|
# 5. remove classes that weren't created by converter
|
||||||
_class_removing(content_tag)
|
_class_removing(content_tag)
|
||||||
|
|||||||
Reference in New Issue
Block a user