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

@@ -10,6 +10,7 @@ class Flashcard:
deck: str
id_updated: bool
preamble: str
file_handler: FileHandler
note_id_node: tree_sitter.Node
@@ -19,7 +20,7 @@ class Flashcard:
svg_front: bytes
svg_back: bytes
def __init__(self, front: str, back: str, deck: str | None, note_id: int, file_handler: FileHandler):
def __init__(self, front: str, back: str, deck: str | None, note_id: int, preamble: str, file_handler: FileHandler):
if deck is None:
deck = "Default"
if not note_id:
@@ -28,8 +29,9 @@ class Flashcard:
self.back = back
self.deck = deck
self.note_id = note_id
self.id_updated = False
self.preamble = preamble
self.file_handler = file_handler
self.id_updated = False
def __str__(self):
return f"Flashcard(id={self.note_id}, front={self.front})"

View File

@@ -19,31 +19,17 @@ async def export_flashcards(root_dir, typst_cmd):
api = AnkiConnectApi()
# parse flashcards
print("Parsing flashcards...")
flashcards = []
file_handlers = []
for file in glob.glob(f"{root_dir}/**/*.typ", recursive=True):
fh = FileHandler(file)
cards = parser.parse_file(fh)
file_handlers.append((fh, cards))
flashcards.extend(cards)
flashcards = parser.parse_directory(root_dir)
# async typst compilation
await compiler.compile_flashcards(flashcards)
# async anki push per deck
await api.push_flashcards(flashcards)
# write id updates to files
print("Updating ids in source...")
for fh, cards in file_handlers:
file_updated = False
for c in cards:
if c.id_updated:
fh.update_node_content(c.note_id_node, c.note_id)
file_updated = True
if file_updated:
fh.write()
try:
# async anki push per deck
await api.push_flashcards(flashcards)
finally:
# write id updates to files
parser.update_ids_in_source()
print("Done")

View File

@@ -1,3 +1,7 @@
import glob
import os.path
from functools import cache
from typing import List
import tree_sitter
@@ -32,12 +36,15 @@ class FlashcardParser:
typst_parser: tree_sitter.Parser
flashcard_query: tree_sitter.Query
file_handlers: List[tuple[FileHandler, List[Flashcard]]]
def __init__(self):
self.typst_language = get_language("typst")
self.typst_parser = get_parser("typst")
self.flashcard_query = self.typst_language.query(ts_flashcard_query)
self.file_handlers = []
def parse_file(self, file: FileHandler) -> List[Flashcard]:
def parse_file(self, file: FileHandler, preamble: str) -> List[Flashcard]:
cards = []
tree = self.typst_parser.parse(file.get_bytes(), encoding="utf8")
captures = self.flashcard_query.captures(tree.root_node)
@@ -57,8 +64,43 @@ class FlashcardParser:
file.get_node_content(back, True),
None,
int(file.get_node_content(note_id)),
preamble,
file,
)
card.set_ts_nodes(front, back, note_id)
cards.append(card)
return cards
def parse_directory(self, root_dir):
print(f"Parsing flashcards in {root_dir}...")
preambles = {}
flashcards = []
@cache
def get_preamble(path) -> str | None:
while len(path) > len(root_dir):
if preamble := preambles.get(path):
return preamble
path = os.path.dirname(path)
for file in sorted(glob.glob(f"{root_dir}/**/**.typ", include_hidden=True, recursive=True)):
if os.path.basename(file) == ".anki.typ":
with open(file, encoding="utf-8") as f:
preambles[os.path.dirname(file)] = f.read()
continue
fh = FileHandler(file)
cards = self.parse_file(fh, get_preamble(os.path.dirname(file)))
self.file_handlers.append((fh, cards))
flashcards.extend(cards)
return flashcards
def update_ids_in_source(self):
print("Updating ids in source...")
for fh, cards in self.file_handlers:
file_updated = False
for c in cards:
if c.id_updated:
fh.update_node_content(c.note_id_node, c.note_id)
file_updated = True
if file_updated:
fh.write()

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]):