优化isay 代码,考虑代码块内有```和有制表符的情况, 优化界面显示
This commit is contained in:
37
toolbox.py
37
toolbox.py
@ -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
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user