我正在构建一个富文本格式化程序,可以完成五件事。
multiline formatting
single line
我已经能够成功地将内容传递给formatter函数,该函数包括打包到一条消息中的所有示例。我面临的问题是如何编写一个仍然是DRY的函数,并允许应用上面显示的任意数量的格式化选项。如果解决方案看起来很明显我很抱歉我对编程很陌生,而且我似乎无法想出一个没有多次重复的解决方案。
import re
class Publisher():
def rich_text_formatter(self, content):
# Quote_format
#find the desired string inside of the post content
string = re.findall(r">([^;]*)\r\n", content)
#if the desired string existed.
if string:
#concatenate html tags around the extracted string while turning list into a string for concatention.
formatted_text = "<div class='quote-container'><div class='quote_bar'></div><span class='quote_format'>" + ', '.join(string) + " </span></div> "
# insert formatted text back inside the post content replacing the unformatted string.
final = re.sub(r">([^;]*)\r\n", formatted_text, content)
# Multi_line_format
string = re.findall(r"\s```([^;]*)```\s", final)
if string:
formatted_text = " <br><pre class='multi_line_code_format'> " + ', '.join(string) + " </pre> "
final = re.sub(r"\s```([^;]*)```\s", formatted_text, final)
# Bold_text_format
string = re.findall(r"\s\*([^;]*)\*\s", final)
if string:
formatted_text = " <span class='bold_text_format'> " + ', '.join(string) + " </span> "
final = re.sub(r"\s\*([^;]*)\*\s", formatted_text, final)
# Italic_text_format
string = re.findall(r"\s_([^;]*)_\s", final)
if string:
formatted_text = " <span class='italic_text_format'> " + ', '.join(string) + " </span> "
final = re.sub(r"\s_([^;]*)_\s", formatted_text, final)
# Single_line_format
string = re.findall(r"\s`([^;]*)`\s", final)
if string:
formatted_text = " <span class='single_line_format'> " + ', '.join(string) + " </span> "
final = re.sub(r"\s`([^;]*)`\s", formatted_text, final)
return final
publisher = Publisher()
content = 'hey this is preformatted text ```multiline``` >this is a quote\r\n' + ' `single line code` *bolded text* _italic_ '
print publisher.rich_text_formatter(content)