From 01c2c8b120b381349f3fc92097181ada0af5d64c Mon Sep 17 00:00:00 2001 From: Kiryl Date: Mon, 27 Jun 2022 19:12:02 +0300 Subject: [PATCH] Add ability to replace tags based on parents/children --- src/epub_converter/html_epub_preprocessor.py | 25 +++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/src/epub_converter/html_epub_preprocessor.py b/src/epub_converter/html_epub_preprocessor.py index fbc45a2..042b7b0 100644 --- a/src/epub_converter/html_epub_preprocessor.py +++ b/src/epub_converter/html_epub_preprocessor.py @@ -142,12 +142,25 @@ def _wrap_tags_with_table(chapter_tag): def _tags_to_correspond_livecarta_tag(chapter_tag): """Function to replace all tags to correspond livecarta tags""" - for reg_key, to_replace_value in LiveCartaConfig.REPLACE_REGEX_WITH_LIVECARTA_CORRESPOND_TAGS.items(): - for key in reg_key: - tags = chapter_tag.find_all(re.compile(key)) - for tag in tags: - # todo can cause appearance of \n

...

->

\n

...

\n

(section) - tag.name = to_replace_value + for reg_keys, to_replace_value in LiveCartaConfig.REPLACE_TAG_WITH_LIVECARTA_CORRESPOND_TAGS.items(): + for key in reg_keys: + if isinstance(key, tuple): + replace = key[0] + parent, child = key[1], key[2] + for parent_tag in chapter_tag.select(parent): + if replace == "parent": + parent_tag.name = to_replace_value + elif replace == "child": + for child_tag in parent_tag.select(child): + child_tag.name = to_replace_value + if not child_tag.attrs.get("style"): + child_tag.attrs["style"] =\ + "font-size: 14px; font-family: courier new,courier,monospace;" + else: + tags = chapter_tag.find_all(re.compile(key)) + for tag in tags: + # todo can cause appearance of \n

...

->

\n

...

\n

(section) + tag.name = to_replace_value def _unwrap_tags(chapter_tag):