fix type:cloze, and remove misleading comments

This commit is contained in:
Damien Elmes 2020-01-09 07:56:22 +10:00
parent afe391c18b
commit 5a9af48178
2 changed files with 23 additions and 12 deletions

View file

@ -118,7 +118,7 @@ def apply_field_filters(
field_name: str, field_text: str, fields: Dict[str, str], filters: List[str] field_name: str, field_text: str, fields: Dict[str, str], filters: List[str]
) -> str: ) -> str:
"""Apply filters to field text, returning modified text.""" """Apply filters to field text, returning modified text."""
_sort_filters(filters) filters = _adjust_filters(filters)
for filter in filters: for filter in filters:
if "-" in filter: if "-" in filter:
@ -137,15 +137,11 @@ def apply_field_filters(
return field_text return field_text
def _sort_filters(filters: List[str]): def _adjust_filters(filters: List[str]) -> List[str]:
"Mutate the list of filters into the correct order." "Handle the type:cloze: special case."
# Since 'text:' and other mods can affect html on which Anki relies to if filters == ["cloze", "type"]:
# process clozes, we need to make sure clozes are always filters = ["type-cloze"]
# treated after all the other mods, regardless of how they're specified return filters
# in the template, so that {{cloze:text: == {{text:cloze:
# For type:, we return directly since no other mod than cloze (or other
# pre-defined mods) can be present and those are treated separately
filters.sort(key=lambda s: not s == "type")
# Cloze filter # Cloze filter
@ -344,9 +340,12 @@ def text_filter(txt: str, *args) -> str:
return stripHTML(txt) return stripHTML(txt)
def type_answer_filter(txt: str, args, context, tag: str, dummy) -> str: def type_answer_filter(txt: str, filter_args: str, context, tag: str, dummy) -> str:
# convert it to [[type:...]] for the gui code to process # convert it to [[type:...]] for the gui code to process
return "[[type:%s]]" % tag if filter_args:
return f"[[type:{filter_args}:{tag}]]"
else:
return f"[[type:{tag}]]"
addHook("fmod_text", text_filter) addHook("fmod_text", text_filter)

View file

@ -220,6 +220,18 @@ def test_cloze_mathjax():
) )
def test_typecloze():
d = getEmptyCol()
m = d.models.byName("Cloze")
d.models.setCurrent(m)
m["tmpls"][0]["qfmt"] = "{{type:cloze:Text}}"
d.models.save(m)
f = d.newNote()
f["Text"] = "hello {{c1::world}}"
d.addNote(f)
assert "[[type:cloze:Text]]" in f.cards()[0].q()
def test_chained_mods(): def test_chained_mods():
d = getEmptyCol() d = getEmptyCol()
d.models.setCurrent(d.models.byName("Cloze")) d.models.setCurrent(d.models.byName("Cloze"))