mirror of
https://git.freetards.xyz/array.in.a.matrix/TexLiLy.git
synced 2025-04-02 13:21:42 -04:00
39 lines
1.3 KiB
Python
39 lines
1.3 KiB
Python
from nio import *
|
|
|
|
import utils
|
|
|
|
|
|
# Our message callback. It should be passed through a router.
|
|
async def msg_cb(room: MatrixRoom, event: RoomMessageText) -> None:
|
|
import re
|
|
import latex
|
|
client = utils.get_client()
|
|
if event.sender == client.user_id:
|
|
return
|
|
print("ae", client)
|
|
|
|
for tex in re.findall(r'((?:\$[^\$]+\$)|(?:\$\$[^\$]+\$\$)|(?:\\\[[^\]]+\\\]))', event.body, re.M):
|
|
print("Text:", tex)
|
|
try:
|
|
filename = latex.render("", tex)
|
|
await utils.send_png(room, filename)
|
|
except FileNotFoundError as e:
|
|
content = {
|
|
"msgtype": "m.text",
|
|
"body": f"Couldn't parse LaTeX correctly.\n```{e.args[0]}\n```",
|
|
"formatted_body": f"Couldn't parse LaTeX correctly.<br><code><pre>{e.args[0]}</pre></code>",
|
|
}
|
|
await client.room_send(room.room_id, message_type="m.room.message", content=content)
|
|
except OSError:
|
|
content = {
|
|
"msgtype": "m.text",
|
|
"body": "???",
|
|
"formatted_body": "???"
|
|
}
|
|
|
|
await client.room_send(room.room_id, message_type="m.room.message", content=content)
|
|
|
|
# Our file callback.
|
|
async def file_cb(room: MatrixRoom, event: RoomMessageFile) -> None:
|
|
url = event.url
|
|
filename = event.body
|