def _renderQA(self, data: QAData, qfmt: Optional[str] = None, afmt: Optional[str] = None) -> Dict[str, Union[str, int]]: "Returns hash of id, question, answer." # extract info from data split_fields = splitFields(data[6]) card_ord = data[4] model = self.models.get(data[2]) if model["type"] == MODEL_STD: template = model["tmpls"][data[4]] else: template = model["tmpls"][0] flag = data[7] deck_id = data[3] card_id = data[0] tags = data[5] qfmt = qfmt or template["qfmt"] afmt = afmt or template["afmt"] # create map of field names -> field content fields: Dict[str, str] = {} for (name, (idx, conf)) in list(self.models.fieldMap(model).items()): fields[name] = split_fields[idx] # add special fields fields["Tags"] = tags.strip() fields["Type"] = model["name"] fields["Deck"] = self.decks.name(deck_id) fields["Subdeck"] = fields["Deck"].split("::")[-1] fields["Card"] = template["name"] fields["CardFlag"] = self._flagNameFromCardFlags(flag) fields["c%d" % (card_ord + 1)] = "1" # allow add-ons to modify the available fields hooks.modify_fields_for_rendering_hook(fields, model, data) fields = runFilter("mungeFields", fields, model, data, self) # legacy # and the template prior to rendering qfmt = hooks.original_card_template_filter(qfmt, True) afmt = hooks.original_card_template_filter(afmt, False) # render fields qatext = render_card(self, qfmt, afmt, fields, card_ord) ret: Dict[str, Any] = dict(q=qatext[0], a=qatext[1], id=card_id) # allow add-ons to modify the generated result for type in "q", "a": ret[type] = hooks.rendered_card_template_filter( ret[type], type, fields, model, data, self) # empty cloze? if type == "q" and model["type"] == MODEL_CLOZE: if not self.models._availClozeOrds(model, data[6], False): ret["q"] += "<p>" + _( "Please edit this note and add some cloze deletions. (%s)" ) % ("<a href=%s#cloze>%s</a>" % (HELP_SITE, _("help"))) return ret
def _renderQA(self, data: QAData, qfmt: Optional[str] = None, afmt: Optional[str] = None) -> Dict[str, Union[str, int]]: # extract info from data split_fields = splitFields(data[6]) card_ord = data[4] model = self.models.get(data[2]) if model["type"] == MODEL_STD: template = model["tmpls"][data[4]] else: template = model["tmpls"][0] flag = data[7] deck_id = data[3] card_id = data[0] tags = data[5] qfmt = qfmt or template["qfmt"] afmt = afmt or template["afmt"] # create map of field names -> field content fields: Dict[str, str] = {} for (name, (idx, conf)) in list(self.models.fieldMap(model).items()): fields[name] = split_fields[idx] # add special fields fields["Tags"] = tags.strip() fields["Type"] = model["name"] fields["Deck"] = self.decks.name(deck_id) fields["Subdeck"] = fields["Deck"].split("::")[-1] fields["Card"] = template["name"] fields["CardFlag"] = self._flagNameFromCardFlags(flag) fields["c%d" % (card_ord + 1)] = "1" # legacy hook fields = runFilter("mungeFields", fields, model, data, self) ctx = TemplateRenderContext(self, data, fields) # render fields. if any custom filters are encountered, # the field_filter hook will be called. try: qtext, atext = render_card(self, qfmt, afmt, ctx) except anki.rsbackend.BackendException as e: errmsg = _("Card template has a problem:") + f"<br>{e}" qtext = errmsg atext = errmsg # avoid showing the user a confusing blank card if they've # forgotten to add a cloze deletion if model["type"] == MODEL_CLOZE: if not self.models._availClozeOrds(model, data[6], False): qtext = (qtext + "<p>" + _( "Please edit this note and add some cloze deletions. (%s)") % ("<a href=%s#cloze>%s</a>" % (HELP_SITE, _("help")))) # allow add-ons to modify the generated result (qtext, atext) = hooks.card_did_render((qtext, atext), ctx) # legacy hook qtext = runFilter("mungeQA", qtext, "q", fields, model, data, self) atext = runFilter("mungeQA", atext, "a", fields, model, data, self) return dict(q=qtext, a=atext, id=card_id)