From 10afe2883acb462c7b8df8f32662922c66114f1b Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Tue, 21 Jul 2020 12:59:28 +1000 Subject: [PATCH] add cloze-only filter https://anki.tenderapp.com/discussions/ankidesktop/42383-two-feature-suggestions --- rslib/src/cloze.rs | 46 +++++++++++++++++++++++++++++++++-- rslib/src/template_filters.rs | 3 ++- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/rslib/src/cloze.rs b/rslib/src/cloze.rs index 4fa51401a..47ea27e54 100644 --- a/rslib/src/cloze.rs +++ b/rslib/src/cloze.rs @@ -92,6 +92,33 @@ pub fn reveal_cloze_text(text: &str, cloze_ord: u16, question: bool) -> Cow } } +pub fn reveal_cloze_text_only(text: &str, cloze_ord: u16, question: bool) -> Cow { + for caps in CLOZE.captures_iter(text) { + let captured_ord = caps + .get(cloze_caps::ORD) + .unwrap() + .as_str() + .parse() + .unwrap_or(0); + + if captured_ord == cloze_ord { + let cloze = if question { + // hint provided? + if let Some(hint) = caps.get(cloze_caps::HINT) { + hint.as_str() + } else { + "..." + } + } else { + caps.get(cloze_caps::TEXT).unwrap().as_str() + }; + return cloze.to_owned().into(); + } + } + + "".into() +} + /// If text contains any LaTeX tags, render the front and back /// of each cloze deletion so that LaTeX can be generated. If /// no LaTeX is found, returns an empty string. @@ -144,10 +171,13 @@ pub(crate) fn cloze_filter<'a>(text: &'a str, context: &RenderContext) -> Cow<'a .into() } +pub(crate) fn cloze_only_filter<'a>(text: &'a str, context: &RenderContext) -> Cow<'a, str> { + reveal_cloze_text_only(text, context.card_ord + 1, context.question_side) +} + #[cfg(test)] mod test { - use super::strip_html_inside_mathjax; - use crate::cloze::{cloze_numbers_in_string, expand_clozes_to_reveal_latex}; + use super::*; use crate::text::strip_html; use std::collections::HashSet; @@ -174,6 +204,18 @@ mod test { assert!(expanded.contains("foo bar")); } + #[test] + fn cloze_only() { + assert_eq!(reveal_cloze_text_only("foo", 1, true), ""); + assert_eq!(reveal_cloze_text_only("foo {{c1::bar}}", 1, true), "..."); + assert_eq!( + reveal_cloze_text_only("foo {{c1::bar::baz}}", 1, true), + "baz" + ); + assert_eq!(reveal_cloze_text_only("foo {{c1::bar}}", 1, false), "bar"); + assert_eq!(reveal_cloze_text_only("foo {{c1::bar}}", 2, false), ""); + } + #[test] fn mathjax_html() { // escaped angle brackets should be preserved diff --git a/rslib/src/template_filters.rs b/rslib/src/template_filters.rs index b0c500d8b..648dd2559 100644 --- a/rslib/src/template_filters.rs +++ b/rslib/src/template_filters.rs @@ -1,7 +1,7 @@ // Copyright: Ankitects Pty Ltd and contributors // License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html -use crate::cloze::cloze_filter; +use crate::cloze::{cloze_filter, cloze_only_filter}; use crate::template::RenderContext; use crate::text::strip_html; use blake3::Hasher; @@ -73,6 +73,7 @@ fn apply_filter<'a>( "type-cloze" => type_cloze_filter(field_name), "hint" => hint_filter(text, field_name), "cloze" => cloze_filter(text, context), + "cloze-only" => cloze_only_filter(text, context), // an empty filter name (caused by using two colons) is ignored "" => text.into(), _ => {