aboutsummaryrefslogtreecommitdiffstats
path: root/bot/exts/fun/latex/_renderer.py
blob: fb72b94c2aa6877f9a1fe187a30c0edb3a7690ce (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import sys
from pathlib import Path
from typing import BinaryIO

import matplotlib.pyplot as plt

# configure fonts and colors for matplotlib
plt.rcParams.update(
    {
        "font.size": 16,
        "mathtext.fontset": "cm",  # Computer Modern font set
        "mathtext.rm": "serif",
        "figure.facecolor": "36393F",  # matches Discord's dark mode background color
        "text.color": "white",
    }
)


def render(text: str, file_handle: BinaryIO) -> None:
    """
    Saves rendered image in `file_handle`.

    In case the input is invalid latex, it prints the error to `stderr`.
    """
    fig = plt.figure()
    fig.text(0, 1, text, horizontalalignment="left", verticalalignment="top")
    try:
        plt.savefig(file_handle, bbox_inches="tight", dpi=600)
    except ValueError as err:
        # get rid of traceback, keeping just the latex error
        sys.exit(err)


def main() -> None:
    """
    Renders a latex query and saves the output in a specified file.

    Expects two command line arguments: the query and the path to the output file.
    """
    query = sys.argv[1]
    out_file_path = Path(sys.argv[2])
    with open(out_file_path, "wb") as out_file:
        render(query, out_file)


if __name__ == "__main__":
    main()