mirror of
https://github.com/Ascyii/typstar.git
synced 2026-01-01 05:24:24 -05:00
feat(anki): custom preamble per directory
This commit is contained in:
@@ -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]):
|
||||
|
||||
Reference in New Issue
Block a user