prepareQA hook

see updated docs on https://apps.ankiweb.net/docs/addons21.html
This commit is contained in:
Damien Elmes 2017-09-06 13:02:00 +10:00
parent ca6412b6c9
commit 38688ef8ef
3 changed files with 11 additions and 3 deletions

View file

@ -19,7 +19,7 @@ from aqt.utils import saveGeom, restoreGeom, saveSplitter, restoreSplitter, \
saveHeader, restoreHeader, saveState, restoreState, applyStyles, getTag, \ saveHeader, restoreHeader, saveState, restoreState, applyStyles, getTag, \
showInfo, askUser, tooltip, openHelp, showWarning, shortcut, mungeQA, \ showInfo, askUser, tooltip, openHelp, showWarning, shortcut, mungeQA, \
getOnlyText, MenuList, SubMenu getOnlyText, MenuList, SubMenu
from anki.hooks import runHook, addHook, remHook from anki.hooks import runHook, addHook, remHook, runFilter
from aqt.webview import AnkiWebView from aqt.webview import AnkiWebView
from anki.consts import * from anki.consts import *
from anki.sound import playFromText, clearAudioQueue from anki.sound import playFromText, clearAudioQueue
@ -1356,6 +1356,8 @@ where id in %s""" % ids2str(sf))
playFromText(txt) playFromText(txt)
txt = mungeQA(self.col, txt) txt = mungeQA(self.col, txt)
txt = runFilter("prepareQA", txt, c,
"preview"+self._previewState.capitalize())
self._previewWeb.eval( self._previewWeb.eval(
f"{func}({json.dumps(txt)},'{bodyclass}');") f"{func}({json.dumps(txt)},'{bodyclass}');")

View file

@ -15,6 +15,8 @@ from aqt.utils import saveGeom, restoreGeom, mungeQA,\
from anki.utils import isMac, isWin, joinFields from anki.utils import isMac, isWin, joinFields
from aqt.webview import AnkiWebView from aqt.webview import AnkiWebView
import json import json
from anki.hooks import runFilter
class CardLayout(QDialog): class CardLayout(QDialog):
@ -296,8 +298,12 @@ Please create a new card type first."""))
c = self.card c = self.card
ti = self.maybeTextInput ti = self.maybeTextInput
bodyclass="card card%d" % (c.ord+1) bodyclass="card card%d" % (c.ord+1)
q = ti(mungeQA(self.mw.col, c.q(reload=True))) q = ti(mungeQA(self.mw.col, c.q(reload=True)))
q = runFilter("prepareQA", q, c, "clayoutQuestion")
a = ti(mungeQA(self.mw.col, c.a()), type='a') a = ti(mungeQA(self.mw.col, c.a()), type='a')
a = runFilter("prepareQA", a, c, "clayoutAnswer")
# use _showAnswer to avoid the longer delay # use _showAnswer to avoid the longer delay
self.pform.frontWeb.eval("_showAnswer(%s,'%s');" % (json.dumps(q), bodyclass)) self.pform.frontWeb.eval("_showAnswer(%s,'%s');" % (json.dumps(q), bodyclass))

View file

@ -167,7 +167,7 @@ The front of this card is empty. Please run Tools>Empty Cards.""")
playFromText(q) playFromText(q)
# render & update bottom # render & update bottom
q = self._mungeQA(q) q = self._mungeQA(q)
q = runFilter("prepareQuestion", q) q = runFilter("prepareQA", q, c, "reviewQuestion")
bodyclass = "card card%d" % (c.ord+1) bodyclass = "card card%d" % (c.ord+1)
@ -211,7 +211,7 @@ The front of this card is empty. Please run Tools>Empty Cards.""")
if self.autoplay(c): if self.autoplay(c):
playFromText(a) playFromText(a)
a = self._mungeQA(a) a = self._mungeQA(a)
a = runFilter("prepareAnswer", a) a = runFilter("prepareQA", a, c, "reviewAnswer")
# render and update bottom # render and update bottom
self.web.eval("_showAnswer(%s);" % json.dumps(a)) self.web.eval("_showAnswer(%s);" % json.dumps(a))
self._showEaseButtons() self._showEaseButtons()