epub converter: fix <pre> 2

This commit is contained in:
shirshasa
2021-07-06 15:12:04 +03:00
parent f0d75ec85c
commit 6cc4b30405

View File

@@ -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', '&nbsp; &nbsp; &nbsp; ')
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]: