TexLiLy/lilypond.py
2023-06-14 19:36:13 +02:00

27 lines
744 B
Python

template = r"""
\header {{
tagline = ""
}}
{content}
"""
def render(user: str, source: str, png: bool = True) -> str:
import tempfile
import subprocess
import utils
import os
fmt = template.format(content=source)
tmp = tempfile.NamedTemporaryFile(delete=False)
tmp.write(bytes(fmt, encoding="utf8"))
tmp.close()
if png:
ret = subprocess.run(["lilypond", "-d", "preview", "--png", "-dresolution=500", tmp.name], cwd="/tmp", capture_output=True)
if ret.returncode != 0:
raise FileNotFoundError(ret.stderr.decode())
return tmp.name + ".preview.png"
subprocess.run(["lilypond", "-d", "preview", "--svg", tmp.name], cwd="/tmp")
return tmp.name + "preview.svg"