diff --git a/src/html_epub_preprocessor.py b/src/html_epub_preprocessor.py
index c06a92c..c25e38b 100644
--- a/src/html_epub_preprocessor.py
+++ b/src/html_epub_preprocessor.py
@@ -501,15 +501,20 @@ def preprocess_pre_tags(chapter_tag):
for child in pre.children:
if isinstance(child, NavigableString):
- text = pre.text
- text = _prepare_formatted(text)
- elements = re.split('\r\n|\n|\r', text)
- for i in elements:
- new_tag.append(NavigableString(i))
+ cleaned_text = _prepare_formatted(pre.text)
+ sub_strings = re.split('\r\n|\n|\r', cleaned_text)
+ for string in sub_strings:
+ new_tag.append(NavigableString(string))
new_tag.append(BeautifulSoup(features='lxml').new_tag('br'))
else:
- child.string = _prepare_formatted(child.text)
- new_tag.append(child.extract())
+ for sub_child in child.children:
+ if isinstance(sub_child, NavigableString):
+ cleaned_text2 = _prepare_formatted(str(sub_child))
+ sub_child.replace_with(NavigableString(cleaned_text2))
+ else:
+ sub_child.string = _prepare_formatted(sub_child.text)
+ cleaned_tag = child.extract()
+ new_tag.append(cleaned_tag)
if to_add_br:
new_tag.append(BeautifulSoup(features='lxml').new_tag('br'))