From e07141fe1078923efe18e69b1c6c8b56533a67af Mon Sep 17 00:00:00 2001 From: Greenscreener Date: Sat, 11 Feb 2023 18:26:12 +0100 Subject: [PATCH] Don't leave hanging katexserver. --- formatitko.py | 10 +++++----- katex.py | 6 ++++++ 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/formatitko.py b/formatitko.py index 5d50bd8..17d072d 100755 --- a/formatitko.py +++ b/formatitko.py @@ -48,14 +48,14 @@ doc = doc.walk(transform, context) # setting out to TeX doc.content = [Group(*doc.content, metadata={"language":language})] -# Initialize KaTeX client (this runs the node app and connects to a unix socket) -katexClient = KatexClient() # Initialize the image processor (this just keeps some basic state) imageProcessor = ImageProcessor(args.img_public_dir, args.img_web_path, *args.img_lookup_dirs) -# Generate HTML and TeX out of the transformed document -open(args.output_html, "w").write(html(doc, katexClient, imageProcessor)) -open(args.output_tex, "w").write(tex(doc, imageProcessor)) +# Initialize KaTeX client (this runs the node app and connects to a unix socket) +with KatexClient() as katexClient: + # Generate HTML and TeX out of the transformed document + open(args.output_html, "w").write(html(doc, katexClient, imageProcessor)) + open(args.output_tex, "w").write(tex(doc, imageProcessor)) if args.debug: print(show(doc)) diff --git a/katex.py b/katex.py index c0aadea..fc9ace4 100644 --- a/katex.py +++ b/katex.py @@ -56,3 +56,9 @@ class KatexClient: def endgroup(self): self._client.sendall("endgroup\n".encode("utf-8")) + + def __enter__(self): + return self + + def __exit__(self, type, value, tb): + self._server_process.terminate()