Skip to content

Commit

Permalink
feat: use functionalpy
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinBernstorff committed Oct 27, 2023
1 parent 72d106a commit b7352f1
Show file tree
Hide file tree
Showing 12 changed files with 22 additions and 26 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ file = "README.md"
content-type = "text/markdown"

dependencies = [
"functionalpy==0.6.0",
"misaka==2.1.1",
"genanki==0.13.0",
"typer==0.9.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def __init__(
self,
fields: list[str],
source_prompt: Prompt,
url_generator: Callable[[Path, Optional[int]], str] = get_obsidian_url,
url_generator: Callable[[Path, int | None], str] = get_obsidian_url,
html_compiler: Callable[[str], str] = compile_field,
):
self.markdown_fields = fields
Expand Down Expand Up @@ -164,8 +164,7 @@ def process_match(m) -> str: # noqa

current_stage = re.sub(regex, process_match, current_stage)

for r in results:
yield r
yield from results

# Anki seems to hate alt tags :(
self.html_fields[i] = re.sub(r'alt="[^"]*?"', "", current_stage)
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import re
from collections.abc import Callable
from pathlib import Path
from typing import List, Optional

import genanki
from personal_mnemonic_medium.exporters.anki.card_types.base import AnkiCard
Expand All @@ -21,7 +20,7 @@ def __init__(
self,
fields: list[str],
source_prompt: Prompt,
url_generator: Callable[[Path, Optional[int]], str] = get_obsidian_url,
url_generator: Callable[[Path, int | None], str] = get_obsidian_url,
html_compiler: Callable[[str], str] = compile_field,
):
super().__init__(
Expand Down
3 changes: 1 addition & 2 deletions src/personal_mnemonic_medium/exporters/anki/card_types/qa.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from collections.abc import Callable
from pathlib import Path
from typing import List, Optional

import genanki
from personal_mnemonic_medium.exporters.anki.card_types.base import AnkiCard
Expand All @@ -20,7 +19,7 @@ def __init__(
self,
fields: list[str],
source_prompt: Prompt,
url_generator: Callable[[Path, Optional[int]], str] = get_obsidian_url,
url_generator: Callable[[Path, int | None], str] = get_obsidian_url,
html_compiler: Callable[[str], str] = compile_field,
):
super().__init__(
Expand Down
2 changes: 1 addition & 1 deletion src/personal_mnemonic_medium/exporters/anki/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import urllib.request
from pathlib import Path
from time import sleep
from typing import Any, Dict, List
from typing import Any

from genanki import Model, Note
from wasabi import Printer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import Optional


def get_obsidian_url(source_path: Path, line_nr: Optional[int] = None) -> str:
def get_obsidian_url(source_path: Path, line_nr: int | None = None) -> str:
"""Get the obsidian URI for the source document."""
vault: str = urllib.parse.quote(source_path.parent.name) # type: ignore
file: str = urllib.parse.quote(source_path.name) # type: ignore
Expand Down
2 changes: 1 addition & 1 deletion src/personal_mnemonic_medium/note_factories/markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def get_and_append_new_uuid(self, file_path: Path) -> str:
def get_note_id(self, file_string: str) -> str:
return re.findall(r"<!-- {BearID:.+", file_string)[0]

def get_note_from_file(self, file_path: Path) -> Optional[Document]:
def get_note_from_file(self, file_path: Path) -> Document | None:
with file_path.open(encoding="utf8") as f:
file_contents = f.read()

Expand Down
4 changes: 2 additions & 2 deletions src/personal_mnemonic_medium/note_factories/note.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ def __init__(
):
self.title = title
self.uuid = uuid
self.content = self._replace_alias_wiki_links(content)
self.content = self.replace_alias_wiki_links(content)
self.source_path = source_path

import_time_formatted = datetime.datetime.now().strftime("%Y-%m-%d")

self.tags = self.get_tags(self.content, import_time=import_time_formatted)

@staticmethod
def _replace_alias_wiki_links(text: str) -> str:
def replace_alias_wiki_links(text: str) -> str:
tokens_in_link = r"[\w|\s|\d|\(|\)\-]"
regex_pattern = rf"\[\[{tokens_in_link}+\|{tokens_in_link}+\]\]"
pattern_matches = re.findall(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def _has_cloze(string: str) -> bool:
@staticmethod
def _replace_cloze_id_with_unique(
string: str,
selected_cloze: Optional[str] = None,
selected_cloze: str | None = None,
) -> str:
"""Each cloze deletion in a note is numbered sequentially.
Expand Down
6 changes: 2 additions & 4 deletions src/personal_mnemonic_medium/prompt_extractors/prompt.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from typing import List, Optional

from personal_mnemonic_medium.note_factories.note import Document


Expand All @@ -8,8 +6,8 @@ def __init__(
self,
note_uuid: str,
source_note: Document,
tags: Optional[list[str]] = None,
line_nr: Optional[int] = None,
tags: list[str] | None = None,
line_nr: int | None = None,
):
self.tags = tags
self.note_uuid = note_uuid
Expand Down
14 changes: 7 additions & 7 deletions tests/exporters/anki/test_card_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ def __init__(
self,
document_factory: DocumentFactory = MarkdownNoteFactory(), # noqa: B008
prompt_extractors: Sequence[PromptExtractor] = [
QAPromptExtractor(),
ClozePromptExtractor(),
QAPromptExtractor(), # noqa: B008
ClozePromptExtractor(), # noqa: B008
],
card_exporter: CardExporter = AnkiPackageGenerator(), # noqa: B008
) -> None:
Expand Down Expand Up @@ -133,21 +133,21 @@ def test_get_bear_id():

def test_alias_wiki_link_substitution():
alias = "Here I am [[alias|wiki link]], and another [[alias2|wiki link2]]"
output = Document._replace_alias_wiki_links(alias)
output = Document.replace_alias_wiki_links(alias)
assert output == "Here I am [[wiki link]], and another [[wiki link2]]"

no_alias = "Here I am [[wiki link]] and another [[wiki link2]]"
output = Document._replace_alias_wiki_links(no_alias)
output = Document.replace_alias_wiki_links(no_alias)
assert output == "Here I am [[wiki link]] and another [[wiki link2]]"

test_3 = "How was ice climbing [[Franz Josef]] with [[Vibeke Christiansen|Vibeke]]?"
output = Document._replace_alias_wiki_links(test_3)
output = Document.replace_alias_wiki_links(test_3)
assert output == "How was ice climbing [[Franz Josef]] with [[Vibeke]]?"

alias = "[[Isolation (database design)|Isolation]]"
output = Document._replace_alias_wiki_links(alias)
output = Document.replace_alias_wiki_links(alias)
assert output == "[[Isolation]]"

alias = "[[test-test|test-]]"
output = Document._replace_alias_wiki_links(alias)
output = Document.replace_alias_wiki_links(alias)
assert output == "[[test-]]"
4 changes: 2 additions & 2 deletions tests/exporters/anki/test_package_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ def test_cards_to_decks():

deck, media = AnkiPackageGenerator().cards_to_deck(cards=genanki_notes)

assert type(deck) == genanki.Deck
assert type(media) == set
assert isinstance(deck, genanki.Deck)
assert isinstance(media, set)


def test_package_generators():
Expand Down

0 comments on commit b7352f1

Please sign in to comment.