Skip to content

Commit

Permalink
refactor: move things out of globals (#224)
Browse files Browse the repository at this point in the history
Auto-created
  • Loading branch information
MartinBernstorff authored Nov 19, 2023
1 parent 3e3e4e1 commit 05a83b6
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 119 deletions.
1 change: 1 addition & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// TODO: https://github.com/MartinBernstorff/personal-mnemonic-medium/issues/223 dev: prompt if wanting to enable automerge in tasks
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import re
import textwrap
from collections.abc import Callable
from pathlib import Path

import genanki

from personal_mnemonic_medium.data_access.exporters.anki.anki_css import (
CARD_MODEL_CSS,
)
from personal_mnemonic_medium.data_access.exporters.anki.card_types.base import (
AnkiCard,
)
from personal_mnemonic_medium.data_access.exporters.anki.globals import (
CONFIG,
)
from personal_mnemonic_medium.domain.markdown_to_html import (
compile_field,
)
Expand Down Expand Up @@ -41,12 +42,35 @@ def __init__(

@property
def genanki_model(self) -> genanki.Model:
model_name = "Ankdown Cloze with UUID"
CARD_MATHJAX_CONTENT = textwrap.dedent(
"""\
"""
)
# TODO: refactor: remove mathjax if tests keep passing

return genanki.Model(
model_id=simple_hash(CONFIG["card_model_name_cloze"]), # type: ignore
name=CONFIG["card_model_name_cloze"],
fields=CONFIG["card_model_fields_cloze"],
templates=CONFIG["card_model_template_cloze"],
css=CONFIG["card_model_css"], # type: ignore
model_id=simple_hash(model_name),
name=model_name,
fields=[
{"name": "Text"},
{"name": "Extra"},
{"name": "Tags"},
{"name": "UUID"},
],
templates=[
{
"name": "Ankdown Cloze Card with UUID",
"qfmt": r"{{{{cloze:Text}}}}\n<div class='extra'>{{{{Extra}}}}</div>\n{}".format(
CARD_MATHJAX_CONTENT
),
"afmt": r"{{{{cloze:Text}}}}\n<div class='extra'>{{{{Extra}}}}</div>\n{}".format(
CARD_MATHJAX_CONTENT
),
}
],
css=CARD_MODEL_CSS,
model_type=1, # This is the model_type number for genanki, takes 0 for QA or 1 for cloze
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
from pathlib import Path

import genanki
from personal_mnemonic_medium.data_access.exporters.anki.anki_css import (
CARD_MODEL_CSS,
)
from personal_mnemonic_medium.data_access.exporters.anki.card_types.base import (
AnkiCard,
)
Expand All @@ -15,9 +18,6 @@
Prompt,
)
from personal_mnemonic_medium.utils.hasher import simple_hash
from personal_mnemonic_medium.data_access.exporters.anki.globals import ( # noqa
CONFIG,
)


class AnkiQA(AnkiCard):
Expand All @@ -39,13 +39,56 @@ def __init__(

@property
def genanki_model(self) -> genanki.Model:
global CONFIG # noqa
model_fields = [
{"name": "Question"},
{"name": "Answer"},
{"name": "Extra"},
{"name": "UUID"},
]

QUESTION_STR = r"{{ Question }}"
TTS_QUESTION_STR = r"{{ tts en_US voices=Apple_Samantha speed=1.05:Question }}"

ANSWER_STR = r"{{ Answer }}"
TTS_ANSWER_STR = (
r"{{ tts en_US voices=Apple_Samantha speed=1.05:Answer }}"
)

EXTRA_STR = r"{{ Extra }}"

model_template = [
{
"name": "Ankdown QA Card with UUID",
"qfmt": f"""
<div class="front">
{QUESTION_STR}{TTS_QUESTION_STR}
</div>
<div class="extra">
{EXTRA_STR}
</div>
""",
"afmt": f"""
<div class="back">
<div class="question">
{QUESTION_STR}
</div>
<div class="answer">
{ANSWER_STR}{TTS_ANSWER_STR}
</div>
<div class="extra">
{EXTRA_STR}
</div>
</div>
""",
}
]

return genanki.Model(
model_id=simple_hash(CONFIG["card_model_name_qa"]), # type: ignore
name=CONFIG["card_model_name_qa"], # type: ignore
fields=CONFIG["card_model_fields_qa"], # type: ignore
templates=CONFIG["card_model_template_qa"], # type: ignore
css=CONFIG["card_model_css"], # type: ignore
model_id=simple_hash("Ankdown QA with UUID"),
name=("Ankdown QA with UUID"),
fields=model_fields,
templates=model_template,
css=CARD_MODEL_CSS,
model_type=0,
)
# TODO: https://github.com/MartinBernstorff/personal-mnemonic-medium/issues/203 Refactor the CONFIG to be a dataclass
Expand Down
84 changes: 1 addition & 83 deletions personal_mnemonic_medium/data_access/exporters/anki/globals.py
Original file line number Diff line number Diff line change
@@ -1,97 +1,15 @@
# Anki 2.1 has mathjax built in, but ankidroid and other clients don't.
import textwrap
from typing import Any

from personal_mnemonic_medium.data_access.exporters.anki.anki_css import (
CARD_MODEL_CSS,
)
# TODO: https://github.com/MartinBernstorff/personal-mnemonic-medium/issues/222 refactor: remove globals.py

ANKICONNECT_URL = (
"http://host.docker.internal:8765"
) # On host machine, port is 8765

CARD_MATHJAX_CONTENT = textwrap.dedent(
"""\
"""
)

VERSION = "0.1"

QUESTION_STR = r"{{ Question }}"
ANSWER_STR = r"{{ Answer }}"
EXTRA_STR = r"{{ Extra }}"
TTS_QUESTION_STR = (
r"{{ tts en_US voices=Apple_Samantha speed=1.05:Question }}"
)
TTS_ANSWER_STR = (
r"{{ tts en_US voices=Apple_Samantha speed=1.05:Answer }}"
)

QA_MODEL_TEMPLATE = [
{
"name": "Ankdown QA Card with UUID",
"qfmt": f"""
<div class="front">
{QUESTION_STR}{TTS_QUESTION_STR}
</div>
<div class="extra">
{EXTRA_STR}
</div>
""",
"afmt": f"""
<div class="back">
<div class="question">
{QUESTION_STR}
</div>
<div class="answer">
{ANSWER_STR}{TTS_ANSWER_STR}
</div>
<div class="extra">
{EXTRA_STR}
</div>
</div>
""",
}
]

CLOZE_MODEL_TEMPLATE = [
{
"name": "Ankdown Cloze Card with UUID",
"qfmt": r"{{{{cloze:Text}}}}\n<div class='extra'>{{{{Extra}}}}</div>\n{}".format(
CARD_MATHJAX_CONTENT
),
"afmt": r"{{{{cloze:Text}}}}\n<div class='extra'>{{{{Extra}}}}</div>\n{}".format(
CARD_MATHJAX_CONTENT
),
}
]

CONFIG = {
"pkg_arg": "AnkdownPkg.apkg",
"recur_dir": ".",
"dollar": False,
"updated_only": False,
"version_log": ".mdvlog",
"card_model_name_cloze": "Ankdown Cloze with UUID",
"card_model_name_qa": "Ankdown QA with UUID",
"card_model_css": CARD_MODEL_CSS,
"card_model_fields_cloze": [
{"name": "Text"},
{"name": "Extra"},
{"name": "Tags"},
{"name": "UUID"},
],
"card_model_fields_qa": [
{"name": "Question"},
{"name": "Answer"},
{"name": "Extra"},
{"name": "UUID"},
],
"card_model_template_qa": QA_MODEL_TEMPLATE,
"card_model_template_cloze": CLOZE_MODEL_TEMPLATE,
}

VERSION_LOG: dict[Any, Any] = {}
Q_TYPE_TAG = {
"G": "med/type/1_GP",
Expand Down
22 changes: 3 additions & 19 deletions personal_mnemonic_medium/domain/markdown_to_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,16 @@

import misaka

from personal_mnemonic_medium.data_access.exporters.anki.globals import (
CONFIG,
)


def field_to_html(field: Any) -> str:
# Math processing
"""
Need to extract the math in brackets so that it doesn't get markdowned.
If math is separated with dollar sign it is converted to brackets.
"""
if CONFIG["dollar"]:
for sep, (op, cl) in [
("$$", (r"\\[", r"\\]")),
("$", (r"\\(", r"\\)")),
]:
escaped_sep = sep.replace(r"$", r"\$")
# ignore escaped dollar signs when splitting the field
field = re.split(rf"(?<!\\){escaped_sep}", field)
# add op(en) and cl(osing) brackets to every second element of the list
field[1::2] = [op + e + cl for e in field[1::2]]
field = "".join(field)
else:
for bracket in ["(", ")", "[", "]"]:
field = field.replace(rf"\{bracket}", rf"\\{bracket}")
# backslashes, man.
for bracket in ["(", ")", "[", "]"]:
field = field.replace(rf"\{bracket}", rf"\\{bracket}")
# backslashes, man.

for token in ["*", "/"]:
if token == "/":
Expand Down

0 comments on commit 05a83b6

Please sign in to comment.