From 05a83b6cf07f00e35751c8a796e60af067845984 Mon Sep 17 00:00:00 2001 From: Martin Bernstorff Date: Sun, 19 Nov 2023 10:38:28 +0100 Subject: [PATCH] refactor: move things out of globals (#224) Auto-created --- .vscode/tasks.json | 1 + .../exporters/anki/card_types/cloze.py | 40 +++++++-- .../exporters/anki/card_types/qa.py | 61 ++++++++++++-- .../data_access/exporters/anki/globals.py | 84 +------------------ .../domain/markdown_to_html.py | 22 +---- 5 files changed, 89 insertions(+), 119 deletions(-) diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 6245bd55..0305eb18 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -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 diff --git a/personal_mnemonic_medium/data_access/exporters/anki/card_types/cloze.py b/personal_mnemonic_medium/data_access/exporters/anki/card_types/cloze.py index 0ad173fb..720cfc9f 100644 --- a/personal_mnemonic_medium/data_access/exporters/anki/card_types/cloze.py +++ b/personal_mnemonic_medium/data_access/exporters/anki/card_types/cloze.py @@ -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, ) @@ -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
{{{{Extra}}}}
\n{}".format( + CARD_MATHJAX_CONTENT + ), + "afmt": r"{{{{cloze:Text}}}}\n
{{{{Extra}}}}
\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 ) diff --git a/personal_mnemonic_medium/data_access/exporters/anki/card_types/qa.py b/personal_mnemonic_medium/data_access/exporters/anki/card_types/qa.py index 98c0a7c5..33fd48b9 100644 --- a/personal_mnemonic_medium/data_access/exporters/anki/card_types/qa.py +++ b/personal_mnemonic_medium/data_access/exporters/anki/card_types/qa.py @@ -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, ) @@ -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): @@ -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""" +
+ {QUESTION_STR}{TTS_QUESTION_STR} +
+
+ {EXTRA_STR} +
+ """, + "afmt": f""" +
+
+ {QUESTION_STR} +
+
+ {ANSWER_STR}{TTS_ANSWER_STR} +
+
+ {EXTRA_STR} +
+
+ """, + } + ] + 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 diff --git a/personal_mnemonic_medium/data_access/exporters/anki/globals.py b/personal_mnemonic_medium/data_access/exporters/anki/globals.py index f30bfa9a..bca47cea 100644 --- a/personal_mnemonic_medium/data_access/exporters/anki/globals.py +++ b/personal_mnemonic_medium/data_access/exporters/anki/globals.py @@ -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""" -
- {QUESTION_STR}{TTS_QUESTION_STR} -
-
- {EXTRA_STR} -
- """, - "afmt": f""" -
-
- {QUESTION_STR} -
-
- {ANSWER_STR}{TTS_ANSWER_STR} -
-
- {EXTRA_STR} -
-
- """, - } -] - -CLOZE_MODEL_TEMPLATE = [ - { - "name": "Ankdown Cloze Card with UUID", - "qfmt": r"{{{{cloze:Text}}}}\n
{{{{Extra}}}}
\n{}".format( - CARD_MATHJAX_CONTENT - ), - "afmt": r"{{{{cloze:Text}}}}\n
{{{{Extra}}}}
\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", diff --git a/personal_mnemonic_medium/domain/markdown_to_html.py b/personal_mnemonic_medium/domain/markdown_to_html.py index d14a446a..7b231b5b 100644 --- a/personal_mnemonic_medium/domain/markdown_to_html.py +++ b/personal_mnemonic_medium/domain/markdown_to_html.py @@ -3,10 +3,6 @@ import misaka -from personal_mnemonic_medium.data_access.exporters.anki.globals import ( - CONFIG, -) - def field_to_html(field: Any) -> str: # Math processing @@ -14,21 +10,9 @@ def field_to_html(field: Any) -> str: 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"(?