Merge pull request #179 from henrikh/fmtQA

Unify inline card formatting
This commit is contained in:
Damien Elmes 2017-03-14 15:39:19 +09:00 committed by GitHub
commit ef0a28f294
3 changed files with 18 additions and 18 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

@ -9,7 +9,7 @@ from aqt.utils import saveGeom, restoreGeom, showWarning, askUser, shortcut, \
tooltip, openHelp, addCloseShortcut, downArrow tooltip, openHelp, addCloseShortcut, downArrow
from anki.sound import clearAudioQueue from anki.sound import clearAudioQueue
from anki.hooks import addHook, remHook, runHook from anki.hooks import addHook, remHook, runHook
from anki.utils import stripHTMLMedia, isMac from anki.utils import stripHTMLMedia, fmtQA, isMac
import aqt.editor, aqt.modelchooser, aqt.deckchooser import aqt.editor, aqt.modelchooser, aqt.deckchooser
class AddCards(QDialog): class AddCards(QDialog):
@ -145,7 +145,7 @@ class AddCards(QDialog):
for nid in self.history: for nid in self.history:
if self.mw.col.findNotes("nid:%s" % nid): if self.mw.col.findNotes("nid:%s" % nid):
fields = self.mw.col.getNote(nid).fields fields = self.mw.col.getNote(nid).fields
txt = stripHTMLMedia(", ".join(fields)) txt = fmtQA(", ".join(fields))
if len(txt) > 30: if len(txt) > 30:
txt = txt[:30] + "..." txt = txt[:30] + "..."
a = m.addAction(_("Edit \"%s\"") % txt) a = m.addAction(_("Edit \"%s\"") % txt)

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)")