[FIX] Allow display math mode.

This commit is contained in:
LoaD Accumulator 2023-06-12 21:23:20 +02:00
parent d267bede14
commit 062885ab2b
No known key found for this signature in database
GPG key ID: 6898757653ABE3E6
2 changed files with 9 additions and 5 deletions

View file

@ -1,7 +1,8 @@
# Converts a TeX file into a PNG/SVG file. # Converts a TeX file into a PNG/SVG file.
template = r""" template = r"""
\documentclass[utf8]{{standalone}} \documentclass[utf8,preview]{{standalone}}
\usepackage{{amsmath}} \usepackage{{amsmath}}
\usepackage[active,tightpage]{{preview}}
\usepackage[utf8]{{inputenc}} \usepackage[utf8]{{inputenc}}
% TODO: Add user packages here. % TODO: Add user packages here.
\begin{{document}} \begin{{document}}
@ -19,9 +20,9 @@ def render(user: str, source: str, png: bool = True) -> str:
fmt = template.format(content=source) fmt = template.format(content=source)
tmp = tempfile.NamedTemporaryFile(delete=False) tmp = tempfile.NamedTemporaryFile(delete=False)
print(tmp.name)
tmp.write(bytes(fmt, encoding="utf8")) tmp.write(bytes(fmt, encoding="utf8"))
tmp.close() tmp.close()
print(tmp.name)
# Parse our templated file thru' LaTeX and dvipng. # Parse our templated file thru' LaTeX and dvipng.
# TODO: Allow arbitrary DPI and foreground color. # TODO: Allow arbitrary DPI and foreground color.
ret = subprocess.run(["latex", "-halt-on-error", tmp.name], cwd="/tmp", capture_output=True) ret = subprocess.run(["latex", "-halt-on-error", tmp.name], cwd="/tmp", capture_output=True)

View file

@ -2,11 +2,11 @@ from nio import *
import asyncio import asyncio
import yaml import yaml
import os
async def send_png(room: MatrixRoom, filename: str) -> None: async def send_png(room: MatrixRoom, filename: str) -> None:
import imagesize import imagesize
import os
basename = os.path.basename(filename) basename = os.path.basename(filename)
w, h = imagesize.get(filename) w, h = imagesize.get(filename)
@ -38,7 +38,10 @@ async def send_png(room: MatrixRoom, filename: str) -> None:
async def msg_cb(room: MatrixRoom, event: RoomMessageText) -> None: async def msg_cb(room: MatrixRoom, event: RoomMessageText) -> None:
import re import re
import latex import latex
for tex in re.findall('((?:\$[^\$]+\$)|(?:\$\$[^\$]+\$\$))', event.body, re.M): if event.sender == client.user_id:
return
for tex in re.findall(r'((?:\$[^\$]+\$)|(?:\$\$[^\$]+\$\$)|(?:\\\[[^\]]+\\\]))', event.body, re.M):
print("Text:", tex) print("Text:", tex)
try: try:
filename = latex.render("", tex) filename = latex.render("", tex)
@ -92,7 +95,7 @@ async def main() -> None:
client.user_id = user client.user_id = user
# Bad kludge! # Bad kludge!
await client.sync(timeout=300000) await client.sync(timeout=3000)
# Register all of the callbacks # Register all of the callbacks
client.add_event_callback(msg_cb, RoomMessageText) client.add_event_callback(msg_cb, RoomMessageText)