From 213233cd60d48f59100832f0b424693d866e1e96 Mon Sep 17 00:00:00 2001 From: w_xiaolizu Date: Wed, 31 May 2023 16:14:40 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96isay=20markdown?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- toolbox.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/toolbox.py b/toolbox.py index ab86742..d34cee7 100644 --- a/toolbox.py +++ b/toolbox.py @@ -243,16 +243,19 @@ def report_execption(chatbot, history, a, b): history.append(b) -def text_divide_paragraph(text): +def text_divide_paragraph(input_str): """ 将文本按照段落分隔符分割开,生成带有段落标签的HTML代码。 """ - regex = r'```[\s\S]*?```(?:(?!```).)*$' - codeblock_matches = re.findall(regex, text) - for match in codeblock_matches: - text = text.replace(match, match.replace('\n\n\n\n', '\n\n')) - text = re.sub(r'\n{3,}', '\n\n', text) - return text.strip() + code_blocks = re.findall('```.*?```', input_str, re.DOTALL) + for block in code_blocks: + input_str = input_str.replace(block, f'{{{{{{{{{{{code_blocks.index(block)}}}}}}}}}}}') + # 将除了三个反引号之间的文本块以外的 "\n" 替换为 "\n\n" + input_str = re.sub(r'(?!```)(?