Support for multiline clozes

danielrsabsay's Avatar

danielrsabsay

25 Apr, 2020 09:04 PM

I'd like to use multiline cloze deletions for code snippets. Something like this:

image also attached below

I've considered these possible solutions:

1. Add-on: Multi-Line Type Answer Box

This add-on provides support for multiline type boxes. At a high level, it works be replacing the <input type="text"> tag with a textarea tag. Unfortunately, it doesn't support cloze deletions. But a possible workaround is to create Notes with separate fields (i.e. not use Anki's built-in cloze support). This approach has two disadvantages:

  1. Doesn't work on AnkiMobile. Probably not that important, as answering lots of cards on a phone would be tedious. But I'd like to at least be able to review the card (not typing) on mobile. Haven't test this yet.

  2. I'm a little uncomfortable with this plugin as is because (a) it uses monkey-patching and (b) seems to duplicate a fair bit of the filtering done in aqt/reviewer.py. I fear this could become a maintenance problem.

2. A new add-on

I've thought about writing a new add-on to support this. I've come up with two approaches:

  1. Use monkey-patching similar to the existing add-on (above), but instead of completely replacing the methods, wrap them. The wrapped version of the methods would call the existing methods (in aqt/reviewer.py) and simply apply additional filtering to their return values.
  2. Use the new flavor of hooks as documented here. There seems to be a hook webview_will_set_content that could potentially be used. But I'm not entirely sure how that would be implemented.

With either approach, I would want the add-on to be as unobtrusive as possible. Ideally, I would like to have a separate card type, or a separate field type as in the existing add-on, and make the add-on apply only to that. Then, if the add-on was misbehaving (for whatever reason), users could simply generate cards using the standard Cloze card type from their existing Notes. Unfortunately, clozes are treated specially, and thus it is currently impossible to have multiple card types for a Cloze note.

3. Modifying Anki qt core code

Given the maintenance question of option 1, and the (probably surmountable) challenges in option 2, I considered whether it made sense to add support for multiline clozes natively. I believe this is all that would be required:

  1. Change input HTML tag to a multiline text box here.
  2. Change the answer-checking logic here to accommodate multiple lines, as done in the existing add-on on this line.

I don't have a good feeling for how disruptive this kind of change could be (i.e. could it break existing Cloze cards?).

Request for thoughts

More than anything, I'm just hoping for some thoughts on how best to go about this. I've spent 2-3 days digging through the code and docs to come up with these possible solutions, but now am stuck. I don't know which option makes the most sense. Any ideas?*

* Normally, of course, a pragmatic answer to this kind of query is "just try one of them and see how it goes", but since I'm looking to make a long term investment in Anki as a learning tool, I really don't want to have to revisit this again in a few years (e.g. re-write all my cards because this feature no longer works, fix my add-on every time Anki is updated, etc.). That is why I'm looking for advice on the most sensible "long-term" solution.

  1. Support Staff 1 Posted by Damien Elmes on 25 Apr, 2020 11:30 PM

    Damien Elmes's Avatar

    You could probably accomplish this just by using Javascript on your card templates. I do not recommend it however - longer cards are time consuming, and if you're using the language you'll have plenty of opportunities to type in such constructs in the course of making something.

  2. 2 Posted by danielrsabsay on 26 Apr, 2020 05:09 PM

    danielrsabsay's Avatar

    Thanks! I like the JS approach. Easy to just remove at a later point if things break.

    Now I only have a minor quibble, which is that the type answer comparison logic strips out the newlines (as mentioned above), resulting in things like the attached image.

    Since my cards show the full code snippet on the back, it's not a huge problem, but it is a little extra cognitive load to look at the gray/red characters and decide if they're just unmatched whitespace. I have experimented with simply replacing the newlines with spaces in my JS script before calling pycmd("ans"). That works okay, but is still slightly harder to parse with my eyes. Not sure anything can be done about this without a lot more work (e.g. modifying core or an add-on). Any other ideas?

    I take your point about longer cards. I don't intend for these to be much longer than about 2 lines. The intent is to keep basic language constructs fresh for languages I don't get an opportunity to use often (but have used on projects in the past).

  3. Support Staff 3 Posted by Damien Elmes on 27 Apr, 2020 07:43 AM

    Damien Elmes's Avatar

    None off the top of my head, sorry.

Comments are closed, but you can start a new discussion.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac