import markdown import re import os import math from latex2mathml.converter import convert as tex2mathml from functools import wraps, lru_cache from shared_utils.config_loader import get_conf as get_conf pj = os.path.join default_user_name = 'default_user' def text_divide_paragraph(text): """ 将文本按照段落分隔符分割开,生成带有段落标签的HTML代码。 """ pre = '
' suf = '
' if text.startswith(pre) and text.endswith(suf): return text if '```' in text: # careful input return text elif '' in text: # careful input return text else: # whatever input lines = text.split("\n") for i, line in enumerate(lines): lines[i] = lines[i].replace(" ", " ") text = "
".join(lines) return pre + text + suf @lru_cache(maxsize=128) # 使用 lru缓存 加快转换速度 def markdown_convertion(txt): """ 将Markdown格式的文本转换为HTML格式。如果包含数学公式,则先将公式转换为HTML格式。 """ pre = '
' suf = '
' if txt.startswith(pre) and txt.endswith(suf): # print('警告,输入了已经经过转化的字符串,二次转化可能出问题') return txt # 已经被转化过,不需要再次转化 markdown_extension_configs = { 'mdx_math': { 'enable_dollar_delimiter': True, 'use_gitlab_delimiters': False, }, } find_equation_pattern = r'\n', '') return content def is_equation(txt): """ 判定是否为公式 | 测试1 写出洛伦兹定律,使用tex格式公式 测试2 给出柯西不等式,使用latex格式 测试3 写出麦克斯韦方程组 """ if '```' in txt and '```reference' not in txt: return False if '$' not in txt and '\\[' not in txt: return False mathpatterns = { r'(?