From 0cc193865729a888a0ef58047cc3af060576fe49 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Fri, 14 Feb 2020 19:19:40 +1000 Subject: [PATCH] move empty card check into template code --- pylib/anki/template.py | 8 -------- pylib/tests/test_models.py | 2 +- rslib/src/i18n/card-templates.ftl | 2 ++ rslib/src/template.rs | 18 ++++++++++++++++-- 4 files changed, 19 insertions(+), 11 deletions(-) diff --git a/pylib/anki/template.py b/pylib/anki/template.py index 238b0764f..0a05aeee1 100644 --- a/pylib/anki/template.py +++ b/pylib/anki/template.py @@ -34,7 +34,6 @@ from typing import Any, Dict, List, Optional, Tuple import anki from anki import hooks from anki.cards import Card -from anki.lang import _ from anki.models import NoteType from anki.notes import Note from anki.rsbackend import TemplateReplacementList @@ -128,13 +127,6 @@ def render_card( answer_av_tags=[], ) - if not output.question_text.strip(): - msg = _("The front of this card is blank.") - help = _("More info") - helplink = CARD_BLANK_HELP - msg += f"
{help}" - output.question_text = msg - hooks.card_did_render(output, ctx) return output diff --git a/pylib/tests/test_models.py b/pylib/tests/test_models.py index 4c55a13d8..8f6ceecee 100644 --- a/pylib/tests/test_models.py +++ b/pylib/tests/test_models.py @@ -223,7 +223,7 @@ def test_typecloze(): d = getEmptyCol() m = d.models.byName("Cloze") d.models.setCurrent(m) - m["tmpls"][0]["qfmt"] = "{{type:cloze:Text}}" + m["tmpls"][0]["qfmt"] = "{{cloze:Text}}{{type:cloze:Text}}" d.models.save(m) f = d.newNote() f["Text"] = "hello {{c1::world}}" diff --git a/rslib/src/i18n/card-templates.ftl b/rslib/src/i18n/card-templates.ftl index b3695c7db..08649039d 100644 --- a/rslib/src/i18n/card-templates.ftl +++ b/rslib/src/i18n/card-templates.ftl @@ -13,3 +13,5 @@ no-such-field = Found '{$found}', but there is no field called '{$field}' more-info = More information + +empty-front = The front of this card is blank. diff --git a/rslib/src/template.rs b/rslib/src/template.rs index 9e06f3e33..6ffb04d8a 100644 --- a/rslib/src/template.rs +++ b/rslib/src/template.rs @@ -20,6 +20,8 @@ type TemplateResult = std::result::Result; static TEMPLATE_ERROR_LINK: &str = "https://anki.tenderapp.com/kb/problems/card-template-has-a-problem"; +static TEMPLATE_BLANK_LINK: &str = + "https://anki.tenderapp.com/kb/card-appearance/the-front-of-this-card-is-blank"; // Lexing //---------------------------------------- @@ -500,10 +502,22 @@ pub fn render_card( // question side let qnorm = without_legacy_template_directives(qfmt); - let qnodes = ParsedTemplate::from_text(qnorm.as_ref()) - .and_then(|tmpl| tmpl.render(&context)) + let (qnodes, qtmpl) = ParsedTemplate::from_text(qnorm.as_ref()) + .and_then(|tmpl| Ok((tmpl.render(&context)?, tmpl))) .map_err(|e| template_error_to_anki_error(e, true, i18n))?; + // check if the front side was empty + if !qtmpl.renders_with_fields(context.nonempty_fields) { + let cat = i18n.get(TranslationFile::CardTemplates); + let info = format!( + "{}
{}", + cat.tr("empty-front"), + TEMPLATE_BLANK_LINK, + cat.tr("more-info") + ); + return Err(AnkiError::TemplateError { info }); + }; + // answer side context.question_side = false; let anorm = without_legacy_template_directives(afmt);