mirror of
https://github.com/ankitects/anki.git
synced 2025-09-19 22:42:25 -04:00
support stripping context of cloze
This commit is contained in:
parent
3c269d5fba
commit
03d00228aa
4 changed files with 30 additions and 8 deletions
|
@ -482,7 +482,11 @@ select id from cards where fid in (select id from facts where mid = ?)""",
|
||||||
if type == "q":
|
if type == "q":
|
||||||
format = format.replace("cloze:", "cq:")
|
format = format.replace("cloze:", "cq:")
|
||||||
else:
|
else:
|
||||||
format = format.replace("cloze:", "ca:")
|
if model.conf['clozectx']:
|
||||||
|
name = "cactx:"
|
||||||
|
else:
|
||||||
|
name = "ca:"
|
||||||
|
format = format.replace("cloze:", name)
|
||||||
fields = runFilter("mungeFields", fields, model, gname, data, self)
|
fields = runFilter("mungeFields", fields, model, gname, data, self)
|
||||||
html = anki.template.render(format, fields)
|
html = anki.template.render(format, fields)
|
||||||
d[type] = runFilter(
|
d[type] = runFilter(
|
||||||
|
|
|
@ -13,6 +13,7 @@ from anki.lang import _
|
||||||
defaultConf = {
|
defaultConf = {
|
||||||
'sortf': 0,
|
'sortf': 0,
|
||||||
'gid': 1,
|
'gid': 1,
|
||||||
|
'clozectx': False,
|
||||||
}
|
}
|
||||||
|
|
||||||
defaultField = {
|
defaultField = {
|
||||||
|
@ -40,7 +41,7 @@ defaultTemplate = {
|
||||||
'bg': "#fff",
|
'bg': "#fff",
|
||||||
'emptyAns': True,
|
'emptyAns': True,
|
||||||
'typeAns': None,
|
'typeAns': None,
|
||||||
'gid': None
|
'gid': None,
|
||||||
}
|
}
|
||||||
|
|
||||||
class Model(object):
|
class Model(object):
|
||||||
|
|
|
@ -130,7 +130,7 @@ class Template(object):
|
||||||
func = modifiers[tag_type]
|
func = modifiers[tag_type]
|
||||||
replacement = func(self, tag_name, context)
|
replacement = func(self, tag_name, context)
|
||||||
template = template.replace(tag, replacement)
|
template = template.replace(tag, replacement)
|
||||||
except:
|
except SyntaxError:
|
||||||
return u"{{invalid template}}"
|
return u"{{invalid template}}"
|
||||||
|
|
||||||
return template
|
return template
|
||||||
|
@ -157,8 +157,10 @@ class Template(object):
|
||||||
if txt:
|
if txt:
|
||||||
return stripHTML(txt)
|
return stripHTML(txt)
|
||||||
return ""
|
return ""
|
||||||
elif tag_name.startswith("cq:") or tag_name.startswith("ca:"):
|
elif (tag_name.startswith("cq:") or
|
||||||
m = re.match("c(.):(\d+):(.+)", tag_name)
|
tag_name.startswith("ca:") or
|
||||||
|
tag_name.startswith("cactx:")):
|
||||||
|
m = re.match("c(.+):(\d+):(.+)", tag_name)
|
||||||
(type, ord, tag) = (m.group(1), m.group(2), m.group(3))
|
(type, ord, tag) = (m.group(1), m.group(2), m.group(3))
|
||||||
txt = get_or_attr(context, tag)
|
txt = get_or_attr(context, tag)
|
||||||
if txt:
|
if txt:
|
||||||
|
@ -166,8 +168,6 @@ class Template(object):
|
||||||
return ""
|
return ""
|
||||||
return get_or_attr(context, tag_name, '{unknown field %s}' % tag_name)
|
return get_or_attr(context, tag_name, '{unknown field %s}' % tag_name)
|
||||||
|
|
||||||
# fixme: need a way to conditionally add these, so that including extra
|
|
||||||
# fields in the question fmt doesn't make empty clozes
|
|
||||||
def clozeText(self, txt, ord, type):
|
def clozeText(self, txt, ord, type):
|
||||||
reg = clozeReg
|
reg = clozeReg
|
||||||
m = re.search(reg%ord, txt)
|
m = re.search(reg%ord, txt)
|
||||||
|
@ -180,8 +180,13 @@ class Template(object):
|
||||||
txt = re.sub(reg%ord, "<b>...(\\3)</b>", txt)
|
txt = re.sub(reg%ord, "<b>...(\\3)</b>", txt)
|
||||||
else:
|
else:
|
||||||
txt = re.sub(reg%ord, "<b>...</b>", txt)
|
txt = re.sub(reg%ord, "<b>...</b>", txt)
|
||||||
else:
|
elif type == "actx":
|
||||||
txt = re.sub(reg%ord, "<b>\\1</b>", txt)
|
txt = re.sub(reg%ord, "<b>\\1</b>", txt)
|
||||||
|
else:
|
||||||
|
# just the answers
|
||||||
|
ans = re.findall(reg%ord, txt)
|
||||||
|
ans = ["<b>"+a[0]+"</b>" for a in ans]
|
||||||
|
return ", ".join(ans)
|
||||||
# and display other clozes normally
|
# and display other clozes normally
|
||||||
return re.sub(reg%".*?", "\\1", txt)
|
return re.sub(reg%".*?", "\\1", txt)
|
||||||
|
|
||||||
|
|
|
@ -120,6 +120,11 @@ def test_cloze():
|
||||||
f['Text'] = "hello {{c1::world}}"
|
f['Text'] = "hello {{c1::world}}"
|
||||||
assert d.addFact(f) == 1
|
assert d.addFact(f) == 1
|
||||||
assert "hello <b>...</b>" in f.cards()[0].q()
|
assert "hello <b>...</b>" in f.cards()[0].q()
|
||||||
|
# the default is no context
|
||||||
|
assert "<b>world</b>" in f.cards()[0].a()
|
||||||
|
assert "hello <b>world</b>" not in f.cards()[0].a()
|
||||||
|
# check context works too
|
||||||
|
f.model().conf['clozectx'] = True
|
||||||
assert "hello <b>world</b>" in f.cards()[0].a()
|
assert "hello <b>world</b>" in f.cards()[0].a()
|
||||||
# and with a comment
|
# and with a comment
|
||||||
f = d.newFact()
|
f = d.newFact()
|
||||||
|
@ -136,6 +141,13 @@ def test_cloze():
|
||||||
assert "<b>world</b> bar" in c1.a()
|
assert "<b>world</b> bar" in c1.a()
|
||||||
assert "world <b>...</b>" in c2.q()
|
assert "world <b>...</b>" in c2.q()
|
||||||
assert "world <b>bar</b>" in c2.a()
|
assert "world <b>bar</b>" in c2.a()
|
||||||
|
# if there are multiple answers for a single cloze, they are given in a
|
||||||
|
# list
|
||||||
|
f.model().conf['clozectx'] = False
|
||||||
|
f = d.newFact()
|
||||||
|
f['Text'] = "a {{c1::b}} {{c1::c}}"
|
||||||
|
assert d.addFact(f) == 1
|
||||||
|
assert "<b>b</b>, <b>c</b>" in f.cards()[0].a()
|
||||||
# clozes should be supported in sections too
|
# clozes should be supported in sections too
|
||||||
m = d.currentModel()
|
m = d.currentModel()
|
||||||
m.templates[0]['qfmt'] = "{{#cloze:1:Text}}{{Notes}}{{/cloze:1:Text}}"
|
m.templates[0]['qfmt'] = "{{#cloze:1:Text}}{{Notes}}{{/cloze:1:Text}}"
|
||||||
|
|
Loading…
Reference in a new issue