Move card formatting to anki.utils

This commit is contained in:
Henrik Enggaard Hansen 2017-03-13 15:52:02 +01:00
parent 4693e71104
commit 1dfc4466f1
2 changed files with 16 additions and 16 deletions

View file

@ -153,6 +153,17 @@ def minimizeHTML(s):
'<u>\\1</u>', s) '<u>\\1</u>', s)
return s return s
def fmtQA(s):
s = s.replace("<br>", " ")
s = s.replace("<br />", " ")
s = s.replace("<div>", " ")
s = s.replace("\n", " ")
s = re.sub("\[sound:[^]]+\]", "", s)
s = re.sub("\[\[type:[^]]+\]\]", "", s)
s = stripHTMLMedia(s)
s = s.strip()
return s
def entsToTxt(html): def entsToTxt(html):
# entitydefs defines nbsp as \xa0 instead of a standard space, so we # entitydefs defines nbsp as \xa0 instead of a standard space, so we
# replace it first # replace it first

View file

@ -12,7 +12,7 @@ from anki.lang import ngettext
from aqt.qt import * from aqt.qt import *
import anki import anki
import aqt.forms import aqt.forms
from anki.utils import fmtTimeSpan, ids2str, stripHTMLMedia, isWin, intTime, isMac from anki.utils import fmtTimeSpan, ids2str, stripHTMLMedia, fmtQA, isWin, intTime, isMac
from aqt.utils import saveGeom, restoreGeom, saveSplitter, restoreSplitter, \ 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
@ -234,7 +234,7 @@ class DataModel(QAbstractTableModel):
return self.answer(c) return self.answer(c)
elif type == "noteFld": elif type == "noteFld":
f = c.note() f = c.note()
return self.formatQA(f.fields[self.col.models.sortIdx(f.model())]) return fmtQA(f.fields[self.col.models.sortIdx(f.model())])
elif type == "template": elif type == "template":
t = c.template()['name'] t = c.template()['name']
if c.model()['type'] == MODEL_CLOZE: if c.model()['type'] == MODEL_CLOZE:
@ -283,31 +283,20 @@ class DataModel(QAbstractTableModel):
return self.browser.mw.col.decks.name(c.did) return self.browser.mw.col.decks.name(c.did)
def question(self, c): def question(self, c):
return self.formatQA(c.q(browser=True)) return fmtQA(c.q(browser=True))
def answer(self, c): def answer(self, c):
if c.template().get('bafmt'): if c.template().get('bafmt'):
# they have provided a template, use it verbatim # they have provided a template, use it verbatim
c.q(browser=True) c.q(browser=True)
return self.formatQA(c.a()) return fmtQA(c.a())
# need to strip question from answer # need to strip question from answer
q = self.question(c) q = self.question(c)
a = self.formatQA(c.a()) a = fmtQA(c.a())
if a.startswith(q): if a.startswith(q):
return a[len(q):].strip() return a[len(q):].strip()
return a return a
def formatQA(self, txt):
s = txt.replace("<br>", " ")
s = s.replace("<br />", " ")
s = s.replace("<div>", " ")
s = s.replace("\n", " ")
s = re.sub("\[sound:[^]]+\]", "", s)
s = re.sub("\[\[type:[^]]+\]\]", "", s)
s = stripHTMLMedia(s)
s = s.strip()
return s
def nextDue(self, c, index): def nextDue(self, c, index):
if c.odid: if c.odid:
return _("(filtered)") return _("(filtered)")