forked from LiveCarta/BookConverter
Add abil. to replace value of attributes in presets
This commit is contained in:
@@ -14,7 +14,8 @@ from src.docx_converter.docx_solver import DocxBook
|
|||||||
from src.epub_converter.epub_solver import EpubBook
|
from src.epub_converter.epub_solver import EpubBook
|
||||||
|
|
||||||
|
|
||||||
def configure_file_logger(name: str, filename: str = "logs/converter.log", filemode: str ="w+", logging_level: int = logging.INFO) -> logging.Logger:
|
def configure_file_logger(name: str, filename: str = "logs/converter.log",
|
||||||
|
filemode: str = "w+", logging_level: int = logging.INFO) -> logging.Logger:
|
||||||
logger = logging.getLogger(name)
|
logger = logging.getLogger(name)
|
||||||
|
|
||||||
folder_path = os.path.dirname(os.path.abspath(__file__))
|
folder_path = os.path.dirname(os.path.abspath(__file__))
|
||||||
|
|||||||
@@ -81,11 +81,17 @@
|
|||||||
"preset_name": "attr_replacer",
|
"preset_name": "attr_replacer",
|
||||||
"rules": [
|
"rules": [
|
||||||
{
|
{
|
||||||
"attr": "xlink:href",
|
"attr": {
|
||||||
|
"name": "xlink:href",
|
||||||
|
"value": ".*"
|
||||||
|
},
|
||||||
"condition": {
|
"condition": {
|
||||||
"tags": ["^img$"]
|
"tags": ["^img$"]
|
||||||
},
|
},
|
||||||
"attr_to_replace": "src"
|
"attr_to_replace": {
|
||||||
|
"name": "src",
|
||||||
|
"value": null
|
||||||
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -147,10 +147,17 @@ class HtmlEpubProcessor:
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _replace_attr(**kwargs):
|
def _replace_attr(**kwargs):
|
||||||
attr = kwargs["rule"]["attr"]
|
attr, attr_value =\
|
||||||
attr_to_replace = kwargs["rule"]["attr_to_replace"]
|
kwargs["rule"]["attr"]["name"], kwargs["rule"]["attr"]["value"]
|
||||||
kwargs["tag"][attr_to_replace] = kwargs["tag"][attr]
|
attr_to_replace, attr_value_to_replace =\
|
||||||
del kwargs["tag"][attr]
|
kwargs["rule"]["attr_to_replace"]["name"], kwargs["rule"]["attr_to_replace"]["value"]
|
||||||
|
if attr_to_replace:
|
||||||
|
kwargs["tag"][attr_to_replace] = kwargs["tag"][attr]
|
||||||
|
if attr_value_to_replace:
|
||||||
|
kwargs["tag"].attrs[attr_to_replace] = attr_value_to_replace
|
||||||
|
del kwargs["tag"][attr]
|
||||||
|
elif attr_value_to_replace:
|
||||||
|
kwargs["tag"].attrs[attr] = attr_value_to_replace
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _unwrap_tag(**kwargs):
|
def _unwrap_tag(**kwargs):
|
||||||
@@ -208,7 +215,7 @@ class HtmlEpubProcessor:
|
|||||||
elif condition_on_tag[0] == "tags":
|
elif condition_on_tag[0] == "tags":
|
||||||
attr = rule["attr"]
|
attr = rule["attr"]
|
||||||
for tag in chapter_tag.find_all([re.compile(tag) for tag in tags],
|
for tag in chapter_tag.find_all([re.compile(tag) for tag in tags],
|
||||||
{attr: re.compile(r".*")}):
|
{attr['name']: re.compile(fr"{attr['value']}")}):
|
||||||
action(chapter_tag=chapter_tag, tag=tag, rule=rule)
|
action(chapter_tag=chapter_tag, tag=tag, rule=rule)
|
||||||
else:
|
else:
|
||||||
for tag in chapter_tag.find_all([re.compile(tag) for tag in tags]):
|
for tag in chapter_tag.find_all([re.compile(tag) for tag in tags]):
|
||||||
@@ -316,7 +323,7 @@ class HtmlEpubProcessor:
|
|||||||
----------
|
----------
|
||||||
title_str: str
|
title_str: str
|
||||||
|
|
||||||
chapter_tag: Tag, soup object
|
chapter_tag: BeautifulSoup, soup object
|
||||||
|
|
||||||
remove_title_from_chapter: bool
|
remove_title_from_chapter: bool
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user