From 270cef63f4a8c933e1a52c0e99160eb926fa6442 Mon Sep 17 00:00:00 2001 From: Henrik Giesel Date: Fri, 12 Feb 2021 01:53:03 +0100 Subject: [PATCH 1/3] Allow for passing in custom note types for rendering ephemeral cards --- pylib/anki/notes.py | 12 ++++++++++-- qt/aqt/clayout.py | 24 +++++------------------- 2 files changed, 15 insertions(+), 21 deletions(-) diff --git a/pylib/anki/notes.py b/pylib/anki/notes.py index a9be4f69a..9121bf83e 100644 --- a/pylib/anki/notes.py +++ b/pylib/anki/notes.py @@ -78,7 +78,11 @@ class Note: return joinFields(self.fields) def ephemeral_card( - self, ord: int = 0, *, fill_empty: bool = False + self, + ord: int = 0, + *, + custom_note_type: NoteType = None, + fill_empty: bool = False, ) -> anki.cards.Card: card = anki.cards.Card(self.col) card.ord = ord @@ -86,7 +90,11 @@ class Note: model = self.model() template = copy.copy( - model["tmpls"][ord] if model["type"] == MODEL_STD else model["tmpls"][0] + custom_note_type or ( + 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 06035ca4f..0e8a5797b 100644 --- a/qt/aqt/clayout.py +++ b/qt/aqt/clayout.py @@ -474,7 +474,11 @@ 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, + custom_note_type=self.current_template(), + fill_empty=self.fill_empty_action_toggled, + ) ti = self.maybeTextInput @@ -535,24 +539,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 87febe489e56fe04a093eb439981ac0c4e12cd3c Mon Sep 17 00:00:00 2001 From: Henrik Giesel Date: Fri, 12 Feb 2021 01:59:35 +0100 Subject: [PATCH 2/3] Allow for passing in custom note type and template --- pylib/anki/notes.py | 12 ++++++------ qt/aqt/clayout.py | 3 ++- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/pylib/anki/notes.py b/pylib/anki/notes.py index 9121bf83e..19b8e5fb8 100644 --- a/pylib/anki/notes.py +++ b/pylib/anki/notes.py @@ -11,7 +11,7 @@ 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.models import NoteType, Template from anki.utils import joinFields @@ -82,18 +82,18 @@ class Note: ord: int = 0, *, custom_note_type: NoteType = None, + custom_template: Template = None, fill_empty: bool = False, ) -> anki.cards.Card: card = anki.cards.Card(self.col) card.ord = ord card.did = 1 - model = self.model() + model = custom_note_type or self.model() template = copy.copy( - custom_note_type or ( - model["tmpls"][ord] - if model["type"] == MODEL_STD - else model["tmpls"][0] + custom_template + or ( + model["tmpls"][ord] if model["type"] == MODEL_STD else model["tmpls"][0] ) ) # may differ in cloze case diff --git a/qt/aqt/clayout.py b/qt/aqt/clayout.py index 0e8a5797b..3002d4add 100644 --- a/qt/aqt/clayout.py +++ b/qt/aqt/clayout.py @@ -476,7 +476,8 @@ class CardLayout(QDialog): c = self.rendered_card = self.note.ephemeral_card( self.ord, - custom_note_type=self.current_template(), + custom_note_type=self.model, + custom_template=self.current_template(), fill_empty=self.fill_empty_action_toggled, ) From adb002f05f35e9e4117f1ec4a28e7997e89bb3de Mon Sep 17 00:00:00 2001 From: Henrik Giesel Date: Fri, 12 Feb 2021 02:16:05 +0100 Subject: [PATCH 3/3] Remove unused imports --- qt/aqt/clayout.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/qt/aqt/clayout.py b/qt/aqt/clayout.py index 3002d4add..fc935d753 100644 --- a/qt/aqt/clayout.py +++ b/qt/aqt/clayout.py @@ -1,18 +1,15 @@ # 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 *