diff --git a/ankiqt/ui/view.py b/ankiqt/ui/view.py
index efda0e21d..bebf805b2 100644
--- a/ankiqt/ui/view.py
+++ b/ankiqt/ui/view.py
@@ -9,7 +9,7 @@ from anki.sound import playFromText, stripSounds
from anki.latex import renderLatex, stripLatex
from anki.utils import stripHTML
from anki.hooks import runHook
-import types, time, re, os, urllib, sys
+import types, time, re, os, urllib, sys, difflib
from ankiqt import ui
from ankiqt.ui.utils import mungeQA
from anki.utils import fmtTimeSpan
@@ -131,7 +131,30 @@ class View(object):
self.write(self.center(self.mungeQA(self.main.deck, q), height))
if self.state != self.oldState and not nosound:
playFromText(q)
+
+ def correct(self, a, b):
+ ret = "";
+ s = difflib.SequenceMatcher(None, b, a)
+ sz = self.main.currentCard.cardModel.answerFontSize
+ ok = "background: %s; color: #000; font-size: %dpx" % (
+ passedCharColour, sz)
+ bad = "background: %s; color: #000; font-size: %dpx;" % (
+ failedCharColour, sz)
+
+ for tag, i1, i2, j1, j2 in s.get_opcodes():
+ if tag == "equal":
+ ret += ("%s" % (ok, b[i1:i2]))
+ elif tag == "replace":
+ ret += ("%s"
+ % (bad, b[i1:i2] + (" " * ((j2 - j1) - (i2 - i1)))))
+ elif tag == "delete":
+ ret += ("%s" % (bad, b[i1:i2]))
+ elif tag == "insert":
+ ret += ("%s" % (bad, " " * (j2 - j1)))
+ return ret
+
+
def drawAnswer(self):
"Show the answer."
a = self.main.currentCard.htmlAnswer()
@@ -143,27 +166,10 @@ class View(object):
cor = ""
if cor:
given = unicode(self.main.typeAnswerField.text())
- res = []
- if len(given) < len(cor):
- given += " " * (len(cor) - len(given))
- sz = self.main.currentCard.cardModel.answerFontSize
- ok = "background: %s; color: #000; font-size: %dpx" % (
- passedCharColour, sz)
- bad = "background: %s; color: #000; font-size: %dpx;" % (
- failedCharColour, sz)
- for (i, c) in enumerate(given):
- try:
- yes = c == cor[i]
- except IndexError:
- yes = False
- if yes:
- res.append(
- "%s" % (ok, c))
- else:
- res.append("%s" % (bad, c))
- a = "".join(res) + "
" + a
- self.write(self.center('' +
- self.mungeQA(self.main.deck, a)))
+ res = self.correct(cor, given)
+ a = res + "
" + a
+ self.write(self.center(''
+ + self.mungeQA(self.main.deck, a)))
if self.state != self.oldState:
playFromText(a)