diff --git a/ankihub/main/deck_creation.py b/ankihub/main/deck_creation.py index 1e4ba23d5..d899e9601 100644 --- a/ankihub/main/deck_creation.py +++ b/ankihub/main/deck_creation.py @@ -3,7 +3,6 @@ import re import typing import uuid -from copy import deepcopy from dataclasses import dataclass from typing import Dict, List @@ -23,7 +22,7 @@ change_note_types_of_notes, create_backup, get_note_types_in_deck, - modify_note_type, + modified_note_type, ) @@ -88,8 +87,7 @@ def _create_note_types_for_deck(deck_id: DeckId) -> Dict[NotetypeId, NotetypeId] result: Dict[NotetypeId, NotetypeId] = {} model_ids = get_note_types_in_deck(deck_id) for mid in model_ids: - new_model = deepcopy(aqt.mw.col.models.get(mid)) - modify_note_type(new_model) + new_model = modified_note_type(aqt.mw.col.models.get(mid)) name_without_modifications = _note_type_name_without_ankihub_modifications( new_model["name"] ) diff --git a/ankihub/main/importing.py b/ankihub/main/importing.py index 3b63f701d..eb7f7f466 100644 --- a/ankihub/main/importing.py +++ b/ankihub/main/importing.py @@ -46,7 +46,7 @@ get_unique_ankihub_deck_name, is_tag_in_list, lowest_level_common_ancestor_did, - note_type_with_updated_templates, + note_type_with_updated_templates_and_css, truncated_list, ) @@ -782,13 +782,10 @@ def _update_templates_and_css( ) ) - updated_note_type = note_type_with_updated_templates( + updated_note_type = note_type_with_updated_templates_and_css( old_note_type=local_note_type, - new_note_type=remote_note_type, - use_new_templates=use_new_templates_and_css, + new_note_type=remote_note_type if use_new_templates_and_css else None, ) - if use_new_templates_and_css: - updated_note_type["css"] = remote_note_type["css"] aqt.mw.col.models.update_dict(updated_note_type) diff --git a/ankihub/main/utils.py b/ankihub/main/utils.py index 999b98745..072298da3 100644 --- a/ankihub/main/utils.py +++ b/ankihub/main/utils.py @@ -22,9 +22,10 @@ from ..settings import ( ANKI_INT_VERSION, ANKI_VERSION_23_10_00, + ANKIHUB_CSS_END_COMMENT, + ANKIHUB_HTML_END_COMMENT, ANKIHUB_NOTE_TYPE_FIELD_NAME, ANKIHUB_NOTE_TYPE_MODIFICATION_STRING, - ANKIHUB_TEMPLATE_END_COMMENT, url_mh_integrations_preview, url_view_note, ) @@ -35,8 +36,11 @@ # Pattern for the AnkiHub end comment in card templates. # The end comment is used to allow users to add their own content below it without it being overwritten # when the template is updated. -ANKIHUB_END_COMMENT_PATTERN = re.compile( - rf"{ANKIHUB_TEMPLATE_END_COMMENT}(?P[\w\W]*)" +ANKIHUB_HTML_END_COMMENT_PATTERN = re.compile( + rf"{re.escape(ANKIHUB_HTML_END_COMMENT)}(?P[\w\W]*)" +) +ANKIHUB_CSS_END_COMMENT_PATTERN = re.compile( + rf"{re.escape(ANKIHUB_CSS_END_COMMENT)}(?P[\w\W]*)" ) # decks @@ -298,30 +302,20 @@ def get_anki_nid_to_mid_dict(nids: Collection[NoteId]) -> Dict[NoteId, NotetypeI ) -def modify_note_type(note_type: NotetypeDict) -> None: - """Adds the AnkiHub ID Field to the Note Type and modifies the card templates.""" - LOGGER.info( - "Modifying note type...", - note_type_name=note_type["name"], - note_type_id=note_type["id"], - ) - - modify_fields(note_type) - - templates = note_type["tmpls"] - for template in templates: - modify_template(template) +def modified_note_type(note_type: NotetypeDict) -> NotetypeDict: + """Returns a modified version of the note type with the AnkiHub field added and + the card templates updated.""" + note_type = copy.deepcopy(note_type) + _modify_fields(note_type) -def modify_note_type_templates(note_type_ids: Iterable[NotetypeId]) -> None: - for mid in note_type_ids: - note_type = aqt.mw.col.models.get(mid) - for template in note_type["tmpls"]: - modify_template(template) - aqt.mw.col.models.update_dict(note_type) + return note_type_with_updated_templates_and_css( + old_note_type=note_type, + new_note_type=None, + ) -def modify_fields(note_type: Dict) -> None: +def _modify_fields(note_type: Dict) -> None: fields = note_type["flds"] field_names = [field["name"] for field in fields] if settings.ANKIHUB_NOTE_TYPE_FIELD_NAME in field_names: @@ -337,10 +331,14 @@ def modify_fields(note_type: Dict) -> None: note_type["flds"].append(ankihub_field) -def modify_template(template: Dict) -> None: - # the order is important here, the end comment must be added last - template["afmt"] = _template_side_with_view_on_ankihub_snippet(template["afmt"]) - _add_ankihub_end_comment_to_template(template) +def modify_note_type_templates(note_type_ids: Iterable[NotetypeId]) -> None: + for mid in note_type_ids: + note_type = aqt.mw.col.models.get(mid) + note_type = note_type_with_updated_templates_and_css( + old_note_type=note_type, + new_note_type=None, + ) + aqt.mw.col.models.update_dict(note_type) def _template_side_with_view_on_ankihub_snippet(template_side: str) -> str: @@ -423,110 +421,101 @@ def _template_side_with_view_on_ankihub_snippet(template_side: str) -> str: ) -def _add_ankihub_end_comment_to_template(template: Dict) -> None: - """Add the AnkiHub end comment to the template if it is not already present. - The purpose of the AnkiHub end comment is to allow users to add their own content below it without it being - overwritten when the note type is updated.""" - for key in ["qfmt", "afmt"]: - cur_side = template[key] - if re.search(ANKIHUB_TEMPLATE_END_COMMENT, cur_side): - continue - - template[key] = ( - template[key].rstrip("\n ") + "\n\n" + ANKIHUB_TEMPLATE_END_COMMENT + "\n\n" - ) - LOGGER.info( - "Added ANKIHUB_TEMPLATE_END_COMMENT to template.", - template=template["name"], - key=key, - ) - - -def note_type_with_updated_templates( - old_note_type: NotetypeDict, new_note_type: NotetypeDict, use_new_templates: bool +def note_type_with_updated_templates_and_css( + old_note_type: NotetypeDict, + new_note_type: Optional[NotetypeDict], ) -> NotetypeDict: - """Returns the new note type with modifications applied to the card templates. - The new templates are used as the base if use_new_templates is True. + """Returns the updated note type with modifications applied to the card templates and css. + The templates and css of the new note type are used as the base if it is provided. The modifications are as follows: - The View on AnkiHub button is added to the back side of each template. - - Contents below the AnkiHub end comments are preserved when the template is updated. + - Contents below the AnkiHub end comments are preserved when the template/css is updated. Args: - old_note_type (NotetypeDict): The old note tpye. The contents below the AnkiHub end comments is preserved - when the template is updated. - new_note_type (NotetypeDict): The new note type. - use_new_templates (bool): If True, the templates from the new_note_type are used as the base for - the updated templates, otherwise the old templates are used. Then this function just refreshes - the modifications or adds them if they are not present. + old_note_type (NotetypeDict): The old note tpye. The contents below the AnkiHub end comments are preserved. + new_note_type (Optional[NotetypeDict]): The new note type. If provided, the templates and css are updated + based on this. Returns: NotetypeDict: The updated note type. """ updated_templates = [] - for new_template, old_template in zip( - new_note_type["tmpls"], old_note_type["tmpls"] - ): - if use_new_templates: + for template_idx, old_template in enumerate(old_note_type["tmpls"]): + if new_note_type is not None: + new_template = new_note_type["tmpls"][template_idx] updated_template = copy.deepcopy(new_template) else: updated_template = copy.deepcopy(old_template) - # Update template sides for template_side_name in ["qfmt", "afmt"]: - updated_template[template_side_name] = _updated_template_side( - new_template_side=new_template[template_side_name], - old_template_side=old_template[template_side_name], - template_side_name=template_side_name, - use_new_template=use_new_templates, + updated_template[template_side_name] = _updated_note_type_content( + old_content=old_template[template_side_name], + new_content=( + new_template[template_side_name] + if new_note_type is not None + else None + ), + add_view_on_ankihub_snippet=template_side_name == "afmt", + content_type="html", ) updated_templates.append(updated_template) result = copy.deepcopy(old_note_type) result["tmpls"] = updated_templates + + result["css"] = _updated_note_type_content( + old_content=old_note_type["css"], + new_content=new_note_type["css"] if new_note_type is not None else None, + add_view_on_ankihub_snippet=False, + content_type="css", + ) + return result -def _updated_template_side( - new_template_side: str, - old_template_side: str, - template_side_name: str, - use_new_template: bool, +def _updated_note_type_content( + old_content: str, + new_content: Optional[str], + add_view_on_ankihub_snippet: bool, + content_type: str, ) -> str: - """ - Args: - template_side_name (str): The name of the template side ("qfmt" or "afmt"). - use_new_template (bool): If True, the new template side is used as the base for - the updated template side, otherwise the old template side is used. + """Returns updated content with preserved content below ankihub end comment. - Returns: - str: The updated template side with the AnkiHub modifications applied. + Args: + old_content: Original content to preserve custom additions from + new_content: New base content to use, or None to use old_content + add_view_on_ankihub_snippet: Whether to add AnkiHub view button + content_type: Either "html" or "css" to determine comment style """ - m = re.search(ANKIHUB_END_COMMENT_PATTERN, old_template_side) - html_to_migrate = m.group("html_to_migrate") if m else "" + if content_type == "html": + end_comment = ANKIHUB_HTML_END_COMMENT + end_comment_pattern = ANKIHUB_HTML_END_COMMENT_PATTERN + else: + end_comment = ANKIHUB_CSS_END_COMMENT + end_comment_pattern = ANKIHUB_CSS_END_COMMENT_PATTERN + + m = re.search(end_comment_pattern, old_content) + text_to_migrate = m.group("text_to_migrate") if m else "" # Choose the base for the result - if use_new_template: - result = new_template_side - else: - result = old_template_side + result = new_content if new_content is not None else old_content - # Remove end comment and content below it from the template side. + # Remove end comment and content below it. # It will be added back below. - result = re.sub(ANKIHUB_END_COMMENT_PATTERN, "", result) + result = re.sub(end_comment_pattern, "", result) - if template_side_name == "afmt": - # The view on AnkiHub button is added to the back side of the template. + if add_view_on_ankihub_snippet: result = _template_side_with_view_on_ankihub_snippet(result) - # Add the AnkiHub end comment and the content below it back to the template side. + # Add the AnkiHub end comment and the content below it back. return ( result.rstrip("\n ") + "\n\n" - + ANKIHUB_TEMPLATE_END_COMMENT + + end_comment + "\n" - + html_to_migrate + + text_to_migrate.strip("\n ") ) diff --git a/ankihub/settings.py b/ankihub/settings.py index daea48f71..8a3b29e81 100644 --- a/ankihub/settings.py +++ b/ankihub/settings.py @@ -876,13 +876,20 @@ def _send_logs_to_datadog(self, records: List[logging.LogRecord]) -> None: ANKIHUB_NOTE_TYPE_FIELD_NAME = "ankihub_id" ANKIHUB_NOTE_TYPE_MODIFICATION_STRING = "ANKIHUB MODFICATIONS" -ANKIHUB_TEMPLATE_END_COMMENT = ( +ANKIHUB_HTML_END_COMMENT = ( "" ) +ANKIHUB_CSS_END_COMMENT = ( + "/*\n" + "ANKIHUB_END\n" + "Text below this comment will not be modified by AnkiHub or AnKing add-ons.\n" + "Do not edit or remove this comment if you want to protect the content below.\n" + "*/" +) ADDON_PACKAGE = __name__.split(".")[0] ICONS_PATH = ADDON_PATH / "gui/icons" diff --git a/tests/addon/approval_test_files/TestNoteTypeWithUpdatedTemplates.test_basic.afmt.approved.txt b/tests/addon/approval_test_files/TestNoteTypeWithUpdatedTemplates.test_basic.afmt.approved.txt deleted file mode 100644 index af32eb130..000000000 --- a/tests/addon/approval_test_files/TestNoteTypeWithUpdatedTemplates.test_basic.afmt.approved.txt +++ /dev/null @@ -1,64 +0,0 @@ -new content - - -{{#ankihub_id}} - - View Note on AnkiHub - - - - - - -{{/ankihub_id}} - - - diff --git a/tests/addon/approval_test_files/TestNoteTypeWithUpdatedTemplates.test_basic.css.False.approved.txt b/tests/addon/approval_test_files/TestNoteTypeWithUpdatedTemplates.test_basic.css.False.approved.txt new file mode 100644 index 000000000..1cbb94572 --- /dev/null +++ b/tests/addon/approval_test_files/TestNoteTypeWithUpdatedTemplates.test_basic.css.False.approved.txt @@ -0,0 +1,7 @@ +old content + +/* +ANKIHUB_END +Text below this comment will not be modified by AnkiHub or AnKing add-ons. +Do not edit or remove this comment if you want to protect the content below. +*/ diff --git a/tests/addon/approval_test_files/TestNoteTypeWithUpdatedTemplates.test_basic.qfmt.approved.txt b/tests/addon/approval_test_files/TestNoteTypeWithUpdatedTemplates.test_basic.css.True.approved.txt similarity index 95% rename from tests/addon/approval_test_files/TestNoteTypeWithUpdatedTemplates.test_basic.qfmt.approved.txt rename to tests/addon/approval_test_files/TestNoteTypeWithUpdatedTemplates.test_basic.css.True.approved.txt index 21872bc57..3a7009f0e 100644 --- a/tests/addon/approval_test_files/TestNoteTypeWithUpdatedTemplates.test_basic.qfmt.approved.txt +++ b/tests/addon/approval_test_files/TestNoteTypeWithUpdatedTemplates.test_basic.css.True.approved.txt @@ -1,7 +1,7 @@ new content - +*/ diff --git a/tests/addon/approval_test_files/TestNoteTypeWithUpdatedTemplates.test_with_migrating_content_from_old_note_type.afmt.False.approved.txt b/tests/addon/approval_test_files/TestNoteTypeWithUpdatedTemplates.test_with_migrating_content_from_old_note_type.afmt.False.approved.txt index f9c236f9a..b647df0c4 100644 --- a/tests/addon/approval_test_files/TestNoteTypeWithUpdatedTemplates.test_with_migrating_content_from_old_note_type.afmt.False.approved.txt +++ b/tests/addon/approval_test_files/TestNoteTypeWithUpdatedTemplates.test_with_migrating_content_from_old_note_type.afmt.False.approved.txt @@ -62,5 +62,4 @@ ANKIHUB_END Text below this comment will not be modified by AnkiHub or AnKing add-ons. Do not edit or remove this comment if you want to protect the content below. --> - content to migrate diff --git a/tests/addon/approval_test_files/TestNoteTypeWithUpdatedTemplates.test_with_migrating_content_from_old_note_type.afmt.True.approved.txt b/tests/addon/approval_test_files/TestNoteTypeWithUpdatedTemplates.test_with_migrating_content_from_old_note_type.afmt.True.approved.txt index a2db625a0..f6c8e212c 100644 --- a/tests/addon/approval_test_files/TestNoteTypeWithUpdatedTemplates.test_with_migrating_content_from_old_note_type.afmt.True.approved.txt +++ b/tests/addon/approval_test_files/TestNoteTypeWithUpdatedTemplates.test_with_migrating_content_from_old_note_type.afmt.True.approved.txt @@ -62,5 +62,4 @@ ANKIHUB_END Text below this comment will not be modified by AnkiHub or AnKing add-ons. Do not edit or remove this comment if you want to protect the content below. --> - content to migrate diff --git a/tests/addon/approval_test_files/TestNoteTypeWithUpdatedTemplates.test_with_migrating_content_from_old_note_type.afmt.approved.txt b/tests/addon/approval_test_files/TestNoteTypeWithUpdatedTemplates.test_with_migrating_content_from_old_note_type.afmt.approved.txt deleted file mode 100644 index a2db625a0..000000000 --- a/tests/addon/approval_test_files/TestNoteTypeWithUpdatedTemplates.test_with_migrating_content_from_old_note_type.afmt.approved.txt +++ /dev/null @@ -1,66 +0,0 @@ -new content - - -{{#ankihub_id}} - - View Note on AnkiHub - - - - - - -{{/ankihub_id}} - - - - -content to migrate diff --git a/tests/addon/approval_test_files/TestNoteTypeWithUpdatedTemplates.test_with_migrating_content_from_old_note_type.css.False.approved.txt b/tests/addon/approval_test_files/TestNoteTypeWithUpdatedTemplates.test_with_migrating_content_from_old_note_type.css.False.approved.txt new file mode 100644 index 000000000..1a6966d1a --- /dev/null +++ b/tests/addon/approval_test_files/TestNoteTypeWithUpdatedTemplates.test_with_migrating_content_from_old_note_type.css.False.approved.txt @@ -0,0 +1,8 @@ +old css + +/* +ANKIHUB_END +Text below this comment will not be modified by AnkiHub or AnKing add-ons. +Do not edit or remove this comment if you want to protect the content below. +*/ +content to migrate diff --git a/tests/addon/approval_test_files/TestNoteTypeWithUpdatedTemplates.test_with_migrating_content_from_old_note_type.qfmt.approved.txt b/tests/addon/approval_test_files/TestNoteTypeWithUpdatedTemplates.test_with_migrating_content_from_old_note_type.css.True.approved.txt similarity index 95% rename from tests/addon/approval_test_files/TestNoteTypeWithUpdatedTemplates.test_with_migrating_content_from_old_note_type.qfmt.approved.txt rename to tests/addon/approval_test_files/TestNoteTypeWithUpdatedTemplates.test_with_migrating_content_from_old_note_type.css.True.approved.txt index 8d3ccfc5d..df37bc329 100644 --- a/tests/addon/approval_test_files/TestNoteTypeWithUpdatedTemplates.test_with_migrating_content_from_old_note_type.qfmt.approved.txt +++ b/tests/addon/approval_test_files/TestNoteTypeWithUpdatedTemplates.test_with_migrating_content_from_old_note_type.css.True.approved.txt @@ -1,9 +1,8 @@ new content - - +*/ content to migrate diff --git a/tests/addon/approval_test_files/TestNoteTypeWithUpdatedTemplates.test_with_migrating_content_from_old_note_type.qfmt.False.approved.txt b/tests/addon/approval_test_files/TestNoteTypeWithUpdatedTemplates.test_with_migrating_content_from_old_note_type.qfmt.False.approved.txt index 5bf479027..55fc4b3db 100644 --- a/tests/addon/approval_test_files/TestNoteTypeWithUpdatedTemplates.test_with_migrating_content_from_old_note_type.qfmt.False.approved.txt +++ b/tests/addon/approval_test_files/TestNoteTypeWithUpdatedTemplates.test_with_migrating_content_from_old_note_type.qfmt.False.approved.txt @@ -5,5 +5,4 @@ ANKIHUB_END Text below this comment will not be modified by AnkiHub or AnKing add-ons. Do not edit or remove this comment if you want to protect the content below. --> - content to migrate diff --git a/tests/addon/approval_test_files/TestNoteTypeWithUpdatedTemplates.test_with_migrating_content_from_old_note_type.qfmt.True.approved.txt b/tests/addon/approval_test_files/TestNoteTypeWithUpdatedTemplates.test_with_migrating_content_from_old_note_type.qfmt.True.approved.txt index 8d3ccfc5d..8bfc5db82 100644 --- a/tests/addon/approval_test_files/TestNoteTypeWithUpdatedTemplates.test_with_migrating_content_from_old_note_type.qfmt.True.approved.txt +++ b/tests/addon/approval_test_files/TestNoteTypeWithUpdatedTemplates.test_with_migrating_content_from_old_note_type.qfmt.True.approved.txt @@ -5,5 +5,4 @@ ANKIHUB_END Text below this comment will not be modified by AnkiHub or AnKing add-ons. Do not edit or remove this comment if you want to protect the content below. --> - content to migrate diff --git a/tests/addon/test_integration.py b/tests/addon/test_integration.py index 4141abb25..4ee9cf1c6 100644 --- a/tests/addon/test_integration.py +++ b/tests/addon/test_integration.py @@ -184,7 +184,7 @@ FlashCardSelectorDialog, ) from ankihub.gui.suggestion_dialog import SuggestionDialog -from ankihub.main.deck_creation import create_ankihub_deck, modify_note_type +from ankihub.main.deck_creation import create_ankihub_deck, modified_note_type from ankihub.main.deck_unsubscribtion import uninstall_deck from ankihub.main.exporting import to_note_data from ankihub.main.importing import ( @@ -223,9 +223,9 @@ note_type_contains_field, ) from ankihub.settings import ( + ANKIHUB_HTML_END_COMMENT, ANKIHUB_NOTE_TYPE_FIELD_NAME, ANKIHUB_NOTE_TYPE_MODIFICATION_STRING, - ANKIHUB_TEMPLATE_END_COMMENT, AnkiHubCommands, BehaviorOnRemoteNoteDeleted, DeckConfig, @@ -880,7 +880,7 @@ def test_modify_note_type(anki_session_with_addon_data: AnkiSession): note_type = anki_session.mw.col.models.by_name("Basic") original_note_type = copy.deepcopy(note_type) original_note_template = original_note_type["tmpls"][0]["afmt"] - modify_note_type(note_type) + note_type = modified_note_type(note_type) modified_template = note_type["tmpls"][0]["afmt"] # # TODO Make more precise assertions. assert ANKIHUB_NOTE_TYPE_FIELD_NAME in modified_template @@ -1651,12 +1651,12 @@ def test_adjust_note_types(anki_session_with_addon_data: AnkiSession): ankihub_basic_1 = copy.deepcopy(mw.col.models.by_name("Basic")) ankihub_basic_1["id"] = 1 ankihub_basic_1["name"] = "AnkiHub Basic 1" - modify_note_type(ankihub_basic_1) + ankihub_basic_1 = modified_note_type(ankihub_basic_1) # for testing updating existing note type ankihub_basic_2 = copy.deepcopy(mw.col.models.by_name("Basic")) ankihub_basic_2["name"] = "AnkiHub Basic 2" - modify_note_type(ankihub_basic_2) + ankihub_basic_2 = modified_note_type(ankihub_basic_2) # ... save the note type ankihub_basic_2["id"] = 0 changes = mw.col.models.add_dict(ankihub_basic_2) @@ -2066,16 +2066,16 @@ def test_note_type_templates_and_css_are_updated( assert new_qfmt in updated_qfmt else: assert new_qfmt not in updated_qfmt - assert ANKIHUB_TEMPLATE_END_COMMENT in updated_qfmt + assert ANKIHUB_HTML_END_COMMENT in updated_qfmt updated_afmt = updated_note_type["tmpls"][0]["afmt"] if expected_template_and_css_updated: assert new_afmt in updated_afmt - assert updated_note_type["css"] == new_css + assert new_css in updated_note_type["css"] else: assert new_afmt not in updated_afmt - assert updated_note_type["css"] == old_css - assert ANKIHUB_TEMPLATE_END_COMMENT in updated_afmt + assert new_css not in updated_note_type["css"] + assert ANKIHUB_HTML_END_COMMENT in updated_afmt # This is only on the back template (afmt) assert ANKIHUB_NOTE_TYPE_MODIFICATION_STRING in updated_afmt diff --git a/tests/addon/test_unit.py b/tests/addon/test_unit.py index cf099846b..7535530fc 100644 --- a/tests/addon/test_unit.py +++ b/tests/addon/test_unit.py @@ -151,11 +151,12 @@ lowest_level_common_ancestor_deck_name, mh_tag_to_resource, mids_of_notes, - note_type_with_updated_templates, + note_type_with_updated_templates_and_css, retain_nids_with_ah_note_type, ) from ankihub.settings import ( - ANKIHUB_TEMPLATE_END_COMMENT, + ANKIHUB_CSS_END_COMMENT, + ANKIHUB_HTML_END_COMMENT, ANKIWEB_ID, DatadogLogHandler, config, @@ -2918,18 +2919,19 @@ class TestNoteTypeWithUpdatedTemplates: def test_basic(self, use_new_templates: bool): old_note_type_content = "old content" old_note_type = { - "tmpls": [{"qfmt": old_note_type_content, "afmt": old_note_type_content}] + "tmpls": [{"qfmt": old_note_type_content, "afmt": old_note_type_content}], + "css": old_note_type_content, } new_note_type_content = "new content" new_note_type = { - "tmpls": [{"qfmt": new_note_type_content, "afmt": new_note_type_content}] + "tmpls": [{"qfmt": new_note_type_content, "afmt": new_note_type_content}], + "css": new_note_type_content, } - updated_note_type = note_type_with_updated_templates( + updated_note_type = note_type_with_updated_templates_and_css( old_note_type=old_note_type, - new_note_type=new_note_type, - use_new_templates=use_new_templates, + new_note_type=new_note_type if use_new_templates else None, ) assert len(updated_note_type["tmpls"]) == 1 template = updated_note_type["tmpls"][0] @@ -2942,26 +2944,36 @@ def test_basic(self, use_new_templates: bool): template["afmt"], options=NamerFactory.with_parameters("afmt", use_new_templates), ) + verify( + updated_note_type["css"], + options=NamerFactory.with_parameters("css", use_new_templates), + ) @pytest.mark.parametrize("use_new_templates", [True, False]) def test_with_migrating_content_from_old_note_type(self, use_new_templates: bool): content_to_migrate = "content to migrate" - old_note_type_content = ( - f"old content\n{ANKIHUB_TEMPLATE_END_COMMENT}\n{content_to_migrate}" + old_note_type_html_content = ( + f"old content\n{ANKIHUB_HTML_END_COMMENT}\n{content_to_migrate}" + ) + old_note_type_css_content = ( + f"old css\n{ANKIHUB_CSS_END_COMMENT}\n{content_to_migrate}" ) old_note_type = { - "tmpls": [{"qfmt": old_note_type_content, "afmt": old_note_type_content}] + "tmpls": [ + {"qfmt": old_note_type_html_content, "afmt": old_note_type_html_content} + ], + "css": old_note_type_css_content, } new_note_type_content = "new content" new_note_type = { - "tmpls": [{"qfmt": new_note_type_content, "afmt": new_note_type_content}] + "tmpls": [{"qfmt": new_note_type_content, "afmt": new_note_type_content}], + "css": new_note_type_content, } - updated_note_type = note_type_with_updated_templates( + updated_note_type = note_type_with_updated_templates_and_css( old_note_type=old_note_type, - new_note_type=new_note_type, - use_new_templates=use_new_templates, + new_note_type=new_note_type if use_new_templates else None, ) assert len(updated_note_type["tmpls"]) == 1 template = updated_note_type["tmpls"][0] @@ -2974,6 +2986,10 @@ def test_with_migrating_content_from_old_note_type(self, use_new_templates: bool template["afmt"], options=NamerFactory.with_parameters("afmt", use_new_templates), ) + verify( + updated_note_type["css"], + options=NamerFactory.with_parameters("css", use_new_templates), + ) def test_get_daily_review_data_since_last_sync(mocker, anki_session_with_addon_data): diff --git a/tests/fixtures.py b/tests/fixtures.py index b17882c27..3532e9054 100644 --- a/tests/fixtures.py +++ b/tests/fixtures.py @@ -30,7 +30,7 @@ from ankihub.gui.media_sync import _AnkiHubMediaSync from ankihub.gui.suggestion_dialog import SuggestionMetadata from ankihub.main.importing import AnkiHubImporter -from ankihub.main.utils import modify_note_type +from ankihub.main.utils import modified_note_type from ankihub.settings import BehaviorOnRemoteNoteDeleted, DeckConfig, config @@ -84,7 +84,7 @@ def create_or_get_ah_version_of_note_type( if model := mw.col.models.by_name(note_type["name"]): return model - modify_note_type(note_type) + note_type = modified_note_type(note_type) mw.col.models.add_dict(note_type) return mw.col.models.by_name(note_type["name"])