move empty card check into template code

This commit is contained in:
Damien Elmes 2020-02-14 19:19:40 +10:00
parent 33367c8edf
commit 0cc1938657
4 changed files with 19 additions and 11 deletions

View File

@ -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"<br><a href='{helplink}'>{help}</a>"
output.question_text = msg
hooks.card_did_render(output, ctx)
return output

View File

@ -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}}"

View File

@ -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.

View File

@ -20,6 +20,8 @@ type TemplateResult<T> = std::result::Result<T, TemplateError>;
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!(
"{}<br><a href='{}'>{}</a>",
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);