mirror of
https://git.freetards.xyz/array.in.a.matrix/TexLiLy.git
synced 2025-04-02 13:21:42 -04:00
Merge branch 'dev' into dev
This commit is contained in:
commit
f0d8dd1c7b
1 changed files with 35 additions and 23 deletions
52
main.py
52
main.py
|
@ -4,33 +4,45 @@ import asyncio
|
|||
import yaml
|
||||
import os
|
||||
|
||||
|
||||
async def send_png(room: MatrixRoom, filename: str) -> None:
|
||||
import imagesize
|
||||
basename = os.path.basename(filename)
|
||||
w, h = imagesize.get(filename)
|
||||
|
||||
file = open(filename, "r+b")
|
||||
size = os.path.getsize(filename)
|
||||
|
||||
filename = os.path.basename(filename)
|
||||
resp, keys = await client.upload(file, content_type="image/png", filename=basename)
|
||||
|
||||
if not isinstance(resp, UploadResponse):
|
||||
raise OSError("Couldn't upload file.")
|
||||
|
||||
content = {
|
||||
"body": basename,
|
||||
"info": {
|
||||
"size": size,
|
||||
"mimetype": "image/png",
|
||||
"w": w, "h": h
|
||||
},
|
||||
"msgtype": "m.image",
|
||||
"url": resp.content_uri,
|
||||
}
|
||||
file.close()
|
||||
|
||||
await client.room_send(room.room_id, message_type="m.room.message", content=content)
|
||||
|
||||
|
||||
# Our message callback. It should be passed through a router.
|
||||
async def msg_cb(room: MatrixRoom, event: RoomMessageText) -> None:
|
||||
if event.body.startswith("$"):
|
||||
import imagesize
|
||||
import latex
|
||||
|
||||
try:
|
||||
filename = latex.render("", event.body)
|
||||
w, h = imagesize.get(filename)
|
||||
|
||||
file = open(filename, "r+b")
|
||||
size = os.path.getsize(filename)
|
||||
|
||||
filename = os.path.basename(filename)
|
||||
resp, keys = await client.upload(file, content_type="image/png", filename=filename)
|
||||
content = {
|
||||
"body": filename,
|
||||
"info": {
|
||||
"size": size,
|
||||
"w": w, "h": h,
|
||||
"mimetype": "image/png"
|
||||
},
|
||||
"msgtype": "m.image",
|
||||
"url": resp.content_uri,
|
||||
}
|
||||
await client.room_send(room.room_id, message_type="m.room.message", content=content)
|
||||
except FileNotFoundError:
|
||||
await send_png(room, filename)
|
||||
except (FileNotFoundError, OSError):
|
||||
content = {
|
||||
"msgtype": "m.text",
|
||||
"body": "Couldn't parse LaTeX correctly."
|
||||
|
|
Loading…
Add table
Reference in a new issue