Skip to content

Commit

Permalink
fix: anki subdecks are not used (#404)
Browse files Browse the repository at this point in the history
fix: anki subdecks are not used

Fixes #396

tests: ensure ankiconnect gets correct subdecks

fix: extract all tags from markdown documents

fix: tag strings should not contain "#"

fix: import all decks

feat: support arbitrary subdeck nesting
  • Loading branch information
MartinBernstorff authored Dec 22, 2023
2 parents b69d658 + 3ba0c90 commit 7ca3781
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,14 @@ def _delete_prompts(
self.gateway.delete_notes(list(prompt_ids))

def _grouped_cards_to_deck(
self, cards: Mapping[str, Sequence[AnkiCard]]
self, grouped_cards: Mapping[str, Sequence[AnkiCard]]
) -> genanki.Deck:
deck_name = next(iter(cards.keys()))
deck_name = next(iter(grouped_cards.keys()))
deck = genanki.Deck(
name=deck_name, deck_id=hash_cleaned_str(deck_name)
)

for card in cards[deck_name]:
for card in grouped_cards[deck_name]:
deck.add_note(card.to_genanki_note()) # type: ignore

return deck
Expand All @@ -97,11 +97,12 @@ def _create_package(
cards_grouped_by_deck = Seq(cards).groupby(
lambda card: card.deck
)
decks = (
Seq([cards_grouped_by_deck])
.map(self._grouped_cards_to_deck)
.to_list()
)
decks = [
self._grouped_cards_to_deck(
{group: cards_grouped_by_deck[group]}
)
for group in cards_grouped_by_deck
]

return genanki.Package(deck_or_decks=decks)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ def to_genanki_note(self) -> genanki.Note:

@property
def deck(self) -> str:
deck_prefix = "#anki/deck/"
deck_prefix = "anki/deck/"
deck_in_tags = (
tag.replace(deck_prefix, "")
tag.replace(deck_prefix, "").replace("/", "::")
for tag in self.tags
if tag.startswith(deck_prefix)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ class FakeAnkiQA(AnkiQA):


def test_ankiqa_deck_inference():
card = FakeAnkiQA(tags=["#anki/deck/Subdeck"])
card = FakeAnkiQA(tags=["anki/deck/Subdeck"])
assert card.deck == "FakeBaseDeck::Subdeck"
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ def test_ankiconnect_push_prompts():
QAWithoutDoc(
question="FakeQuestion",
answer="FakeAnswer",
add_tags=["FakeTag"],
add_tags=[
"anki/deck/FakeSubdeck/FakeSubSubdeck"
],
),
ClozeWithoutDoc(
text="FakeText", add_tags=["FakeTag"]
Expand All @@ -74,6 +76,17 @@ def test_ankiconnect_push_prompts():
)

expected_commands = [(ImportPackage, 1), (UpdateModel, 2)]
import_package_command = next(
c
for c in gateway.executed_commands
if isinstance(c, ImportPackage)
)
assert (
import_package_command.package.decks[0].name # type: ignore
== "FakeDeck::FakeSubdeck::FakeSubSubdeck"
)
assert len(import_package_command.package.decks) == 2 # type: ignore

for command in expected_commands:
assert (
len(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ class Document:

@property
def tags(self) -> Sequence[str]:
return list(re.findall(r"#anki\/tag\/\S+", self.content))
tag_strings: list[str] = list(
re.findall(r"#[\w\/]+", self.content)
)
return [
tag_string.replace("#", "") for tag_string in tag_strings
]

@property
def title(self) -> str:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,18 @@ def test_markdown_document_ingester(tmpdir: Path):
ingester = MarkdownDocumentIngester(directory=Path(tmpdir))

with (Path(tmpdir) / "test.md").open("w") as f:
f.write("# Hello World\n#anki/tag/test_tag")
f.write(
"""# Hello World\n
#anki/tag/test_tag #anki/tag/test_tag2 <!-- #comment_tag -->
"""
)

documents = ingester.get_documents()
assert len(documents) == 1
document = documents[0]
assert document.title == "test"
assert document.tags == ["#anki/tag/test_tag"]
assert document.tags == [
"anki/tag/test_tag",
"anki/tag/test_tag2",
"comment_tag",
]
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ def test_cloze_prompt_extractor(tmpdir: Path):
extractor[0].text
== r"What is the meaning of life? {{c734::42}}"
)
assert extractor[0].tags == ["#anki/tag/test_tag"]
assert extractor[0].tags == ["anki/tag/test_tag"]
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ def test_qa_prompt_extractor(tmpdir: Path):
prompt = extractor[0]
assert prompt.question == "What is the meaning of life?"
assert prompt.answer == "42"
assert prompt.tags == ["#anki/tag/test_tag"]
assert prompt.tags == ["anki/tag/test_tag"]
assert prompt.uid == 9385242780

0 comments on commit 7ca3781

Please sign in to comment.