dae/anki — pull request #97 by corpulentcoffee (2014-12-30 02:20:54+00:00)
Title: Use alternate pattern match during field changes

Hello!

I'm including a patch here to solve a problem I ran into this weekend. I know you guys are in feature freeze with 2.0.x, so I won't be upset if it cannot be dealt with now, but I wanted to share what I found anyway.

I noticed that renaming or deleting fields breaks my templates whenever I have more than one braced construct on a single line. Specifically:

* during renames, only the last use of a field on a line would be renamed
* during deletes, *everything* between the first braced construct and the field would be nuked

---

For example, try creating a note with a **Flag** field and a template like

{{#Flag}}
...
{{/Flag}}{{^Flag}}
...
{{/Flag}}

or

{{Front}} {{#Flag}}...{{/Flag}}

... and then renaming the **Flag** field to something else.

---

Or try creating a **Victim** field, a template like

{{Front}} Hello. {{Victim}}

... and then deleting the **Victim** field.

---

I took a quick look and it *looks* like the leading `(.*)` in the regex pattern for `ModelManager#renameField` needs to be non-greedy and/or needs to specifically exclude braces... otherwise, it captures everything on the line between the first braced construct and the actual matching one.

I see that the `(.*)` was added for things like `{{type:cloze:Text}}`. I think the alternative should work just as well for those.

Cheers! Happy new year.