kerrickstaley / genanki

A Python 3 library for generating Anki decks
MIT License
2.06k stars 161 forks source link

Fixed the issue where cards couldn't be created if an anki option (like {{furigana:Word}}) was used in a partial in the qfmt field. #44

Closed JDEngi closed 2 years ago

JDEngi commented 4 years ago

I discovered that I couldn't generate any cards if I used either {{kanji:Word}} or {{furigana:Word}} in the qfmt field of a template.

I deducted that "pystache.render(....)" wasn't able to render the sentinal value into the template because of the prepended option.

I cleaned out the option using a regex subtitution and tested this implementation successfully in my own project.

JDEngi commented 4 years ago

I'm setting up a debian dev enviroment to run the tests and will recommit to this pull request once my fix passes the tests

kerrickstaley commented 2 years ago

The below code works for me. It defines an Anki model that includes a {{furigana:Answer}} field on the backside of the card. When I run it and import foo.apkg into Anki, this is what the card looks like:

Screenshot from 2022-05-22 15-15-34

So I think everything is working as expected.

I'm not sure what has changed since you filed this issue. If this is still a problem for you, could you provide some code that triggers the issue?

I also added this code as a test in https://github.com/kerrickstaley/genanki/commit/0c34ba402b331a5db78e0968ad9eb10898af64db

my_model = genanki.Model(
  1523004567,
  'Japanese',
  fields=[
    {'name': 'Question'},
    {'name': 'Answer'},
  ],
  templates=[
    {
      'name': 'Card 1',
      'qfmt': '{{Question}}',
      'afmt': '{{FrontSide}}<hr id="answer">{{furigana:Answer}}',
    },
  ])

my_note = genanki.Note(
  model=my_model,
  fields=['kanji character', '漢字[かんじ]'])

my_deck = genanki.Deck(1702181380, 'Japanese')
my_deck.add_note(my_note)

my_deck.write_to_file('/tmp/foo.apkg')