feat(anki): custom preamble per directory

This commit is contained in:
arne314
2024-12-24 13:22:09 +01:00
parent b0d8031a8e
commit 1f2bc8e486
6 changed files with 61 additions and 132 deletions

View File

@@ -26,17 +26,14 @@ class TypstCompiler:
typst_root_dir: str
max_processes: int
def __init__(self, typst_root_dir: str, typst_cmd: str, preamble: str = None):
if preamble is None:
preamble = default_preamble
def __init__(self, typst_root_dir: str, typst_cmd: str):
self.typst_cmd = typst_cmd
self.typst_root_dir = typst_root_dir
self.preamble = preamble
self.max_processes = round(os.cpu_count() * 1.5)
async def _compile(self, src: str, directory: str) -> bytes:
tmp_path = f"{directory}/tmp_{random.randint(1, 1000000000)}.typ"
with open(tmp_path, "w") as f:
with open(tmp_path, "w", encoding="utf-8") as f:
f.write(src)
proc = await asyncio.create_subprocess_shell(
f"{self.typst_cmd} compile {tmp_path} - --root {self.typst_root_dir} --format svg",
@@ -50,8 +47,9 @@ class TypstCompiler:
return stdout
async def _compile_flashcard(self, card: Flashcard):
front = await self._compile(self.preamble + "\n" + card.as_typst(True), card.file_handler.directory_path)
back = await self._compile(self.preamble + "\n" + card.as_typst(False), card.file_handler.directory_path)
preamble = default_preamble if card.preamble is None else card.preamble
front = await self._compile(preamble + "\n" + card.as_typst(True), card.file_handler.directory_path)
back = await self._compile(preamble + "\n" + card.as_typst(False), card.file_handler.directory_path)
card.set_svgs(front, back)
async def compile_flashcards(self, cards: List[Flashcard]):