From 6cc4b30405afdb494f1974a2de99d3183ac2961d Mon Sep 17 00:00:00 2001 From: shirshasa Date: Tue, 6 Jul 2021 15:12:04 +0300 Subject: [PATCH] epub converter: fix
 2

---
 src/html_epub_preprocessor.py | 24 ++++++++++++++++++------
 1 file changed, 18 insertions(+), 6 deletions(-)

diff --git a/src/html_epub_preprocessor.py b/src/html_epub_preprocessor.py
index ce262cb..fd6520a 100644
--- a/src/html_epub_preprocessor.py
+++ b/src/html_epub_preprocessor.py
@@ -389,7 +389,6 @@ def wrap_span_with_table(main_tag, old_tag):
     tbody = main_tag.new_tag("tbody")
     tr = main_tag.new_tag("tr")
     td = main_tag.new_tag("td")
-    td.attrs['style'] = 'font-family: courier new,courier,monospace;'
     td.attrs['bgcolor'] = '#f5f5f5'
     old_tag.wrap(td)
     td.wrap(tr)
@@ -400,18 +399,31 @@ def wrap_span_with_table(main_tag, old_tag):
 
 def preprocess_pre_tags(chapter_tag):
     for pre in chapter_tag.find_all("pre"):
+        new_tag = BeautifulSoup(features='lxml').new_tag("span")
+        new_tag.attrs = pre.attrs.copy()
+
         for child in pre.children:
             if isinstance(child, NavigableString):
-                child.text = escape(pre.text)
-        pre.name = 'span'
-        pre.attrs['style'] = "white-space: pre-wrap;"
-        wrap_span_with_table(chapter_tag, pre)
+                text = escape(pre.text)
+                text = text.replace('\t', '      ')
+                elements = re.split('\r\n|\n|\r', text)
+                for i in elements:
+                    new_tag.append(NavigableString(i))
+                    new_tag.append(BeautifulSoup().new_tag('br'))
+            else:
+                new_tag.append(child.extract())
+
+        new_tag.attrs['style'] = "font-family: courier new,courier,monospace; " \
+                                 "font-size: 14px; white-space: pre-wrap;"
+        pre.insert_before(new_tag)
+        pre.extract()
+        wrap_span_with_table(chapter_tag, new_tag)
 
 
 def preprocess_code_tags(chapter_tag):
     for code in chapter_tag.find_all("code"):
         code.name = 'span'
-        code.attrs['style'] = 'color:#c7254e; font-family: courier new,courier,monospace;'
+        code.attrs['style'] = 'color:#c7254e; font-size: 14px; font-family: courier new,courier,monospace;'
 
 
 def prepare_title_and_content(title, chapter_tag: BeautifulSoup, remove_title_from_chapter) -> Tuple[str, str]: