From b9fd6688d28e6d17b63ec5e42753fb878160b94d Mon Sep 17 00:00:00 2001 From: RumovZ Date: Wed, 20 Jul 2022 12:26:26 +0200 Subject: [PATCH] Fix field check for cloze conditional (#1975) --- rslib/src/template.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/rslib/src/template.rs b/rslib/src/template.rs index e8314bffb..41ef81b00 100644 --- a/rslib/src/template.rs +++ b/rslib/src/template.rs @@ -502,9 +502,7 @@ impl<'a> RenderContext<'a> { fn evaluate_conditional(&self, key: &str, negated: bool) -> TemplateResult { if self.nonempty_fields.contains(key) { Ok(true ^ negated) - } else if self.fields.contains_key(key) - || (key.starts_with('c') && key[1..].parse::().is_ok()) - { + } else if self.fields.contains_key(key) || is_cloze_conditional(key) { Ok(false ^ negated) } else { let prefix = if negated { "^" } else { "#" }; @@ -828,7 +826,7 @@ fn find_field_references<'a>( } ParsedNode::Conditional { key, children } | ParsedNode::NegatedConditional { key, children } => { - if with_conditionals { + if with_conditionals && !is_cloze_conditional(key) { fields.insert(key); } find_field_references(children, fields, cloze_only, with_conditionals); @@ -837,6 +835,11 @@ fn find_field_references<'a>( } } +fn is_cloze_conditional(key: &str) -> bool { + key.strip_prefix('c') + .map_or(false, |s| s.parse::().is_ok()) +} + // Tests //---------------------------------------