From 062885ab2b67cea02e7954431932771f357c4782 Mon Sep 17 00:00:00 2001 From: LoaD Accumulator Date: Mon, 12 Jun 2023 21:23:20 +0200 Subject: [PATCH] [FIX] Allow display math mode. --- latex.py | 5 +++-- main.py | 9 ++++++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/latex.py b/latex.py index 29c126d..c4d24f1 100644 --- a/latex.py +++ b/latex.py @@ -1,7 +1,8 @@ # Converts a TeX file into a PNG/SVG file. template = r""" -\documentclass[utf8]{{standalone}} +\documentclass[utf8,preview]{{standalone}} \usepackage{{amsmath}} + \usepackage[active,tightpage]{{preview}} \usepackage[utf8]{{inputenc}} % TODO: Add user packages here. \begin{{document}} @@ -19,9 +20,9 @@ def render(user: str, source: str, png: bool = True) -> str: fmt = template.format(content=source) tmp = tempfile.NamedTemporaryFile(delete=False) - print(tmp.name) tmp.write(bytes(fmt, encoding="utf8")) tmp.close() + print(tmp.name) # Parse our templated file thru' LaTeX and dvipng. # TODO: Allow arbitrary DPI and foreground color. ret = subprocess.run(["latex", "-halt-on-error", tmp.name], cwd="/tmp", capture_output=True) diff --git a/main.py b/main.py index 9f9d530..d861ea7 100644 --- a/main.py +++ b/main.py @@ -2,11 +2,11 @@ from nio import * import asyncio import yaml -import os async def send_png(room: MatrixRoom, filename: str) -> None: import imagesize + import os basename = os.path.basename(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: import re 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) try: filename = latex.render("", tex) @@ -92,7 +95,7 @@ async def main() -> None: client.user_id = user # Bad kludge! - await client.sync(timeout=300000) + await client.sync(timeout=3000) # Register all of the callbacks client.add_event_callback(msg_cb, RoomMessageText)