forked from LiveCarta/BookConverter
Add attr replacer & svg -> img
This commit is contained in:
@@ -11,6 +11,7 @@ class HtmlEpubPreprocessor:
|
||||
self.name2function = {
|
||||
"table_wrapper": self._wrap_tags_with_table,
|
||||
"replacer": self._tags_to_correspond_livecarta_tag,
|
||||
"attr_replacer": self._replace_attrs_in_tags,
|
||||
"unwrapper": self._unwrap_tags,
|
||||
"inserter": self._insert_tags_into_correspond_tags
|
||||
}
|
||||
@@ -190,6 +191,30 @@ class HtmlEpubPreprocessor:
|
||||
# todo can cause appearance of \n <p><p>...</p></p> -> <p>\n</p> <p>...</p> <p>\n</p> (section)
|
||||
tag.name = tag_to_replace
|
||||
|
||||
@staticmethod
|
||||
def _replace_attrs_in_tags(chapter_tag: BeautifulSoup, rules: list):
|
||||
"""
|
||||
Function to replace all tags to correspond LiveCarta tags
|
||||
Parameters
|
||||
----------
|
||||
chapter_tag: BeautifulSoup
|
||||
Tag & contents of the chapter tag
|
||||
|
||||
Returns
|
||||
-------
|
||||
None
|
||||
Chapter Tag with all tags replaced with LiveCarta tags
|
||||
|
||||
"""
|
||||
for rule in rules:
|
||||
attr = rule["attr"]
|
||||
tags = rule["condition"]["tags"]
|
||||
attr_to_replace = rule["attr_to_replace"]
|
||||
for tag in chapter_tag.find_all([re.compile(tag) for tag in tags],
|
||||
{attr: re.compile(r".*")}):
|
||||
tag[attr_to_replace] = tag[attr]
|
||||
del tag[attr]
|
||||
|
||||
def _unwrap_tags(self, chapter_tag: BeautifulSoup, rules: dict):
|
||||
"""
|
||||
Function unwrap tags and moves id to span
|
||||
@@ -353,7 +378,7 @@ class HtmlEpubPreprocessor:
|
||||
and (tag.attrs.get("class") not in ["link-anchor", "footnote-element"]):
|
||||
del tag.attrs["class"]
|
||||
|
||||
def prepare_content(self, title_str: str, content_tag: BeautifulSoup, remove_title_from_chapter: bool) -> str:
|
||||
def prepare_content(self, title_str: str, content_tag: BeautifulSoup, remove_title_from_chapter: bool) -> Tag:
|
||||
"""
|
||||
Function finalise processing/cleaning content
|
||||
Parameters
|
||||
@@ -378,7 +403,7 @@ class HtmlEpubPreprocessor:
|
||||
|
||||
Returns
|
||||
-------
|
||||
content_tag: str
|
||||
content_tag: Tag
|
||||
prepared content
|
||||
|
||||
"""
|
||||
@@ -397,4 +422,4 @@ class HtmlEpubPreprocessor:
|
||||
self._process_tables(content_tag)
|
||||
# 9. remove classes that weren't created by converter
|
||||
self._class_removing(content_tag)
|
||||
return str(content_tag)
|
||||
return content_tag
|
||||
|
||||
Reference in New Issue
Block a user