From 00447aea542a920629f74e2ada2fd5e3a8db54e5 Mon Sep 17 00:00:00 2001 From: Kiryl Date: Fri, 29 Apr 2022 12:02:28 +0300 Subject: [PATCH] Add bg-color to code tags --- src/epub_converter/html_epub_preprocessor.py | 43 ++++++++++++++------ 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/src/epub_converter/html_epub_preprocessor.py b/src/epub_converter/html_epub_preprocessor.py index 7d71c14..d06241a 100644 --- a/src/epub_converter/html_epub_preprocessor.py +++ b/src/epub_converter/html_epub_preprocessor.py @@ -526,7 +526,7 @@ def preprocess_block_tags(chapter_tag): wrap_block_tag_with_table(chapter_tag, future_block, bg_color=color) -def prepare_formatted(text): +def prepare_formatted(text: str) -> str: """Function replaces special symbols with their Unicode representation""" text = text.replace("<", "\x3C") text = text.replace(">", "\x3E") @@ -554,7 +554,19 @@ def wrap_preformatted_span_with_table(main_tag, old_tag): def preprocess_pre_tags(chapter_tag): - """Function preprocessing
 tags"""
+    """Function preprocessing 
 tags
+    Parameters
+    ----------
+    chapter_tag: BeautifulSoup
+
+    Steps
+    ----------
+    1. cleaning \n
+    2. heading removal
+    3. processing tags
+    4. class removal
+    """
+
     for pre in chapter_tag.find_all("pre"):
         new_tag = BeautifulSoup(features='lxml').new_tag("span")
         new_tag.attrs = pre.attrs.copy()
@@ -592,19 +604,23 @@ def preprocess_pre_tags(chapter_tag):
         table.insert_after(p_for_br)
 
 
-def preprocess_code_tags(chapter_tag):
-    """Function that emulates style of , , """
-    for parent_tag in chapter_tag.find_all(re.compile("pre|p")):
-        for code in parent_tag.find_all(re.compile("code|kbd|var")):
-            # if code.name == "code":
-            #     parent_tag.name = "pre"
-            code.name = "span"
-            if parent_tag.name == "pre":
-                continue
-            # if tags aren't in pre
+def preprocess_code_tags(chapter_tag: Tag):
+    """Function that
+     - transform , ,  tags into span
+     - add code style to this tags
+    """
+
+    for code in chapter_tag.find_all(re.compile("code|kbd|var")):
+        code.name = "span"
+        if code.parent.name == "pre":
+            continue
+
+        # if tags aren't in pre
+        if not code.attrs.get('style'):
             code.attrs['style'] = 'font-size: 14px; font-family: courier new,courier,monospace;'
 
 
+
 def prepare_title(title_of_chapter: str) -> str:
     """Function finalise processing/cleaning title"""
     title_str = BeautifulSoup(title_of_chapter, features='lxml').string
@@ -633,7 +649,8 @@ def prepare_content(title_str: str, content_tag: BeautifulSoup, remove_title_fro
 
     Returns
     -------
-    prepared content: str
+    content_tag: str
+        prepared content
     """
 
     # 0. cleaning \n