优化isay 代码,考虑代码块内有```和有制表符的情况, 优化界面显示

This commit is contained in:
w_xiaolizu
2023-06-15 12:48:09 +08:00
parent c8c4d37616
commit d4befe6964
5 changed files with 45 additions and 10 deletions

View File

@ -259,19 +259,36 @@ def report_execption(chatbot, history, a, b):
def text_divide_paragraph(input_str):
"""
将文本按照段落分隔符分割开生成带有段落标签的HTML代码。
"""
if input_str:
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"
# 提取所有的代码块
code_blocks = re.findall(r'```[\s\S]*?```', input_str)
input_str = re.sub(r'(?!```)(?<!\n)\n(?!(\n|^)( {0,3}[\*\+\-]|[0-9]+\.))', '\n\n', input_str)
# 还原未处理的文本块
# 将提取到的代码块用占位符替换
for i, block in enumerate(code_blocks):
input_str = input_str.replace(f'{{{{{{{{{{{i}}}}}}}}}}}', block)
input_str = input_str.replace(block, f'{{{{CODE_BLOCK_{i}}}}}')
# 判断输入文本是否有反引号
if code_blocks:
# 将非代码块部分的单个换行符替换为双换行符,并处理四个空格的行
sections = re.split(r'({{{{\w+}}}})', input_str)
for idx, section in enumerate(sections):
if 'CODE_BLOCK' in section or section.startswith(' '):
continue
sections[idx] = re.sub(r'(?!```)(?<!\n)\n(?!(\n|^)( {0,3}[\*\+\-]|[0-9]+\.))', '\n\n', section)
input_str = ''.join(sections)
# 将占位符替换回原代码块
for i, block in enumerate(code_blocks):
input_str = input_str.replace(f'{{{{CODE_BLOCK_{i}}}}}', block.replace('\n', '\n'))
else:
# 对于没有反引号的字符串,针对四个空格之前的换行符进行处理
lines = input_str.split('\n')
for idx, line in enumerate(lines[:-1]):
if not line.strip():
continue
if not (lines[idx + 1].startswith(' ') or lines[idx + 1].startswith('\t')):
pass
input_str = '\n'.join(lines)
return input_str