mirror of
https://git.freetards.xyz/array.in.a.matrix/TexLiLy.git
synced 2025-04-02 13:21:42 -04:00
[ADD/WIP] Add test LaTeX support.
LaTeX support isn't complete, it's merely a test.
This commit is contained in:
parent
3e31a138ad
commit
928eeb3a9a
1 changed files with 35 additions and 0 deletions
35
latex.py
Normal file
35
latex.py
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
# Converts a TeX file into a PNG/SVG file.
|
||||||
|
template = r"""
|
||||||
|
\documentclass{{standalone}}
|
||||||
|
\usepackage{{amsmath}}
|
||||||
|
% TODO: Add user packages here.
|
||||||
|
\begin{{document}}
|
||||||
|
% The user content goes here.
|
||||||
|
{content}
|
||||||
|
|
||||||
|
\end{{document}}
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def render(user: str, source: str, png: bool = True) -> str:
|
||||||
|
import tempfile
|
||||||
|
import subprocess
|
||||||
|
|
||||||
|
fmt = template.format(content=source)
|
||||||
|
|
||||||
|
tmp = tempfile.NamedTemporaryFile(delete=False)
|
||||||
|
print(tmp.name)
|
||||||
|
tmp.write(bytes(fmt, encoding="utf8"))
|
||||||
|
tmp.close()
|
||||||
|
# Parse our templated file thru' LaTeX and dvipng.
|
||||||
|
# TODO: Allow arbitrary DPI and foreground color.
|
||||||
|
subprocess.run(["latex", tmp.name], cwd="/tmp")
|
||||||
|
if png:
|
||||||
|
print("PNG")
|
||||||
|
subprocess.run(["dvipng", "-D", "1000", tmp.name + ".dvi", "-bg", "Transparent",
|
||||||
|
"-fg", "rgb .75 .75 .75", "-o", tmp.name + ".png"], cwd="/tmp")
|
||||||
|
|
||||||
|
return tmp.name + ".png"
|
||||||
|
|
||||||
|
# TODO:
|
||||||
|
raise NotImplementedError("SVG support is not currently implemented")
|
Loading…
Add table
Reference in a new issue