fix(anki): handle typst compilation warnings

This commit is contained in:
arne314
2025-03-09 11:51:26 +01:00
parent 89de725101
commit 766ae2d5ea

View File

@@ -1,6 +1,8 @@
import asyncio import asyncio
import os import os
import random import random
import re
import sys
from pathlib import Path from pathlib import Path
from typing import List from typing import List
@@ -18,7 +20,7 @@ default_preamble = """
class TypstCompilationError(ValueError): class TypstCompilationError(ValueError):
pass regex = re.compile(r"\nerror: ")
class TypstCompiler: class TypstCompiler:
@@ -48,7 +50,11 @@ class TypstCompiler:
stdout, stderr = await proc.communicate() stdout, stderr = await proc.communicate()
os.remove(tmp_path) os.remove(tmp_path)
if stderr: if stderr:
raise TypstCompilationError(bytes.decode(stderr, encoding="utf-8")) err = bytes.decode(stderr, encoding="utf-8")
if TypstCompilationError.regex.search("\n" + err):
raise TypstCompilationError(err)
else:
print(f"Typst compilation warning:\n{err}", file=sys.stderr, flush=True)
return stdout return stdout
async def _compile_flashcard(self, card: Flashcard): async def _compile_flashcard(self, card: Flashcard):