Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cards not created properly when the model template contains {{^FieldName}} #151

Open
twtu opened this issue Jan 5, 2025 · 0 comments
Open

Comments

@twtu
Copy link

twtu commented Jan 5, 2025

Description
Cards with conditional replacements of the form {{^FieldName}} are not handled correctly.
I think I have tracked it to an error in the line
rendered = chevron.render(template['qfmt'], field_values)
in model.py
that is, it does no render the same as anki.

Note that if you import the apkg generated by genanki into anki and then run "Check Database" anki will then create the correct cards.

To reproduce, add the following to test_genanki.py

TEST_MODEL_MUST_HAVE_HINT = genanki.Model(
  456789, 'with hint',
  fields=[{'name': 'Question'}, {'name': 'Hint'}, {'name': 'Answer'}],
  templates=[
    {
      'name': 'card1',
      'qfmt': '{{#Hint}}{{Question}}<br>Hint: {{Hint}}{{/Hint}}',
      'afmt': '{{Answer}}',
    },
  ],
)

TEST_MODEL_MUST_NOT_HAVE_HINT = genanki.Model(
  456789, 'with hint',
  fields=[{'name': 'Question'}, {'name': 'Hint'}, {'name': 'Answer'}],
  templates=[
    {
      'name': 'card1',
      'qfmt': '{{^Hint}}{{Question}}<br>Hint: {{Hint}}{{/Hint}}',
      'afmt': '{{Answer}}',
    },
  ],
)


  def test_notes_generate_cards_based_on_req__must_have_hint(self):
    # only n2 has a hint and will generate one card
    n1 = genanki.Note(model=TEST_MODEL_MUST_HAVE_HINT, fields=['capital of California', '', 'Sacramento'])
    n2 = genanki.Note(model=TEST_MODEL_MUST_HAVE_HINT, fields=['capital of Iowa', 'French for "The Moines"', 'Des Moines'])

    assert len(n1.cards) == 0
    assert len(n2.cards) == 1
    assert n2.cards[0].ord == 0

  def test_notes_generate_cards_based_on_req__must_not_have_hint(self):
    # n2 has a hint so only n1 will generate one card
    n1 = genanki.Note(model=TEST_MODEL_MUST_NOT_HAVE_HINT, fields=['capital of California', '', 'Sacramento'])
    n2 = genanki.Note(model=TEST_MODEL_MUST_NOT_HAVE_HINT, fields=['capital of Iowa', 'French for "The Moines"', 'Des Moines'])

    assert len(n1.cards) == 1
    assert n2.cards[0].ord == 1
    assert len(n2.cards) == 0
    # The following commented lines make the test pass, but they are wrong.
    # assert len(n1.cards) == 1
    # assert len(n2.cards) == 1

Repository owner deleted a comment from taymnw Jan 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant