From 4d119cada250d3545269334ed949888e3d88cfe3 Mon Sep 17 00:00:00 2001 From: Henrik Giesel Date: Tue, 2 Feb 2021 16:27:18 +0100 Subject: [PATCH 1/3] Add ephemeral card method --- pylib/anki/notes.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/pylib/anki/notes.py b/pylib/anki/notes.py index 456fd28bd..5c626a49d 100644 --- a/pylib/anki/notes.py +++ b/pylib/anki/notes.py @@ -4,6 +4,7 @@ from __future__ import annotations import pprint +import copy from typing import Any, List, Optional, Sequence, Tuple import anki # pylint: disable=unused-import @@ -75,6 +76,27 @@ class Note: def joinedFields(self) -> str: return joinFields(self.fields) + def ephemeral_card(self, ord=0, *, fill_empty=False) -> anki.cards.Card: + card = anki.cards.Card(self.col) + card.ord = ord + card.did = 1 + + model = self.model() + template = copy.copy(model["tmpls"][ord]) + # may differ in cloze case + template["ord"] = card.ord + + output = anki.template.TemplateRenderContext.from_card_layout( + self, + card, + notetype=model, + template=template, + fill_empty=fill_empty, + ).render() + card.set_render_output(output) + card._note = self + return card + def cards(self) -> List[anki.cards.Card]: return [self.col.getCard(id) for id in self.card_ids()] From 2483ef451727aea2175cac440b685eec71f3a58a Mon Sep 17 00:00:00 2001 From: Henrik Giesel Date: Tue, 2 Feb 2021 16:47:25 +0100 Subject: [PATCH 2/3] Use new note.ephemeral_card method in clayout --- pylib/anki/notes.py | 7 ++++++- qt/aqt/clayout.py | 23 +++-------------------- 2 files changed, 9 insertions(+), 21 deletions(-) diff --git a/pylib/anki/notes.py b/pylib/anki/notes.py index 5c626a49d..936e72d3b 100644 --- a/pylib/anki/notes.py +++ b/pylib/anki/notes.py @@ -12,6 +12,7 @@ import anki._backend.backend_pb2 as _pb from anki import hooks from anki.models import NoteType from anki.utils import joinFields +from anki.consts import MODEL_STD class Note: @@ -82,7 +83,11 @@ class Note: card.did = 1 model = self.model() - template = copy.copy(model["tmpls"][ord]) + template = copy.copy( + model["tmpls"][ord] + if model["type"] == MODEL_STD + else model["tmpls"][0] + ) # may differ in cloze case template["ord"] = card.ord diff --git a/qt/aqt/clayout.py b/qt/aqt/clayout.py index e2f9ddc70..cdd3ab8bd 100644 --- a/qt/aqt/clayout.py +++ b/qt/aqt/clayout.py @@ -475,8 +475,9 @@ class CardLayout(QDialog): def _renderPreview(self) -> None: self.cancelPreviewTimer() - c = self.rendered_card = self.ephemeral_card_for_rendering() - + c = self.rendered_card = self.note.ephemeral_card( + self.ord, fill_empty=self.fill_empty_action_toggled + ) ti = self.maybeTextInput bodyclass = theme_manager.body_classes_for_card_ord( @@ -536,24 +537,6 @@ class CardLayout(QDialog): repl = answerRepl return re.sub(r"\[\[type:.+?\]\]", repl, txt) - def ephemeral_card_for_rendering(self) -> Card: - card = Card(self.col) - card.ord = self.ord - card.did = 1 - template = copy.copy(self.current_template()) - # may differ in cloze case - template["ord"] = card.ord - output = TemplateRenderContext.from_card_layout( - self.note, - card, - notetype=self.model, - template=template, - fill_empty=self.fill_empty_action_toggled, - ).render() - card.set_render_output(output) - card._note = self.note - return card - # Card operations ###################################################################### From 22a5db8acef08a942fb797bb52052fdd5735b85b Mon Sep 17 00:00:00 2001 From: Henrik Giesel Date: Tue, 2 Feb 2021 17:01:25 +0100 Subject: [PATCH 3/3] Remove unused imports --- pylib/anki/notes.py | 12 ++++++------ qt/aqt/clayout.py | 3 --- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/pylib/anki/notes.py b/pylib/anki/notes.py index 936e72d3b..a9be4f69a 100644 --- a/pylib/anki/notes.py +++ b/pylib/anki/notes.py @@ -3,16 +3,16 @@ from __future__ import annotations -import pprint import copy +import pprint from typing import Any, List, Optional, Sequence, Tuple import anki # pylint: disable=unused-import import anki._backend.backend_pb2 as _pb from anki import hooks +from anki.consts import MODEL_STD from anki.models import NoteType from anki.utils import joinFields -from anki.consts import MODEL_STD class Note: @@ -77,16 +77,16 @@ class Note: def joinedFields(self) -> str: return joinFields(self.fields) - def ephemeral_card(self, ord=0, *, fill_empty=False) -> anki.cards.Card: + def ephemeral_card( + self, ord: int = 0, *, fill_empty: bool = False + ) -> anki.cards.Card: card = anki.cards.Card(self.col) card.ord = ord card.did = 1 model = self.model() template = copy.copy( - model["tmpls"][ord] - if model["type"] == MODEL_STD - else model["tmpls"][0] + model["tmpls"][ord] if model["type"] == MODEL_STD else model["tmpls"][0] ) # may differ in cloze case template["ord"] = card.ord diff --git a/qt/aqt/clayout.py b/qt/aqt/clayout.py index cdd3ab8bd..041e4f031 100644 --- a/qt/aqt/clayout.py +++ b/qt/aqt/clayout.py @@ -1,19 +1,16 @@ # Copyright: Ankitects Pty Ltd and contributors # License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html -import copy import json import re from concurrent.futures import Future from typing import Any, Dict, List, Match, Optional import aqt -from anki.cards import Card from anki.consts import * from anki.errors import TemplateError from anki.lang import without_unicode_isolation from anki.notes import Note -from anki.template import TemplateRenderContext from aqt import AnkiQt, gui_hooks from aqt.forms.browserdisp import Ui_Dialog from aqt.qt import *