Add bg-color to code tags

This commit is contained in:
Kiryl
2022-04-29 12:02:28 +03:00
parent c10190662b
commit 00447aea54

View File

@@ -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 <pre> tags"""
"""Function preprocessing <pre> 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 <code>, <kdb>, <var>"""
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 <code>, <kdb>, <var> 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