add latex

This commit is contained in:
swrneko
2025-09-09 14:20:47 +03:00
parent 271846a8c5
commit 74a15afded
8 changed files with 82 additions and 9 deletions

View File

@@ -0,0 +1,24 @@
import re
from pylatexenc.latex2text import LatexNodes2Text
class ConvertMdToPdf:
# Конвертирует md в pdf
def convertLatexToText(self, text:str):
# Обрабатываем только математические выражения
text = re.sub(
r'\$\$(.*?)\$\$|\$(.*?)\$',
self.replace_math,
text,
flags=re.DOTALL
)
return text
def replace_math(self, match):
math_content = match.group(1) or match.group(2) # $$...$$ или $...$
try:
# Преобразуем только математическое выражение
converted = LatexNodes2Text().latex_to_text(math_content)
return converted
except:
return math_content # В случае ошибки оставляем как есть