mirror of
https://github.com/ankitects/anki.git
synced 2025-09-24 16:56:36 -04:00
add cloze-only filter
https://anki.tenderapp.com/discussions/ankidesktop/42383-two-feature-suggestions
This commit is contained in:
parent
01ff6ab55d
commit
10afe2883a
2 changed files with 46 additions and 3 deletions
|
@ -92,6 +92,33 @@ pub fn reveal_cloze_text(text: &str, cloze_ord: u16, question: bool) -> Cow<str>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn reveal_cloze_text_only(text: &str, cloze_ord: u16, question: bool) -> Cow<str> {
|
||||||
|
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
|
/// If text contains any LaTeX tags, render the front and back
|
||||||
/// of each cloze deletion so that LaTeX can be generated. If
|
/// of each cloze deletion so that LaTeX can be generated. If
|
||||||
/// no LaTeX is found, returns an empty string.
|
/// 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()
|
.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)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use super::strip_html_inside_mathjax;
|
use super::*;
|
||||||
use crate::cloze::{cloze_numbers_in_string, expand_clozes_to_reveal_latex};
|
|
||||||
use crate::text::strip_html;
|
use crate::text::strip_html;
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
|
|
||||||
|
@ -174,6 +204,18 @@ mod test {
|
||||||
assert!(expanded.contains("foo bar"));
|
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]
|
#[test]
|
||||||
fn mathjax_html() {
|
fn mathjax_html() {
|
||||||
// escaped angle brackets should be preserved
|
// escaped angle brackets should be preserved
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
// Copyright: Ankitects Pty Ltd and contributors
|
// Copyright: Ankitects Pty Ltd and contributors
|
||||||
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
// 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::template::RenderContext;
|
||||||
use crate::text::strip_html;
|
use crate::text::strip_html;
|
||||||
use blake3::Hasher;
|
use blake3::Hasher;
|
||||||
|
@ -73,6 +73,7 @@ fn apply_filter<'a>(
|
||||||
"type-cloze" => type_cloze_filter(field_name),
|
"type-cloze" => type_cloze_filter(field_name),
|
||||||
"hint" => hint_filter(text, field_name),
|
"hint" => hint_filter(text, field_name),
|
||||||
"cloze" => cloze_filter(text, context),
|
"cloze" => cloze_filter(text, context),
|
||||||
|
"cloze-only" => cloze_only_filter(text, context),
|
||||||
// an empty filter name (caused by using two colons) is ignored
|
// an empty filter name (caused by using two colons) is ignored
|
||||||
"" => text.into(),
|
"" => text.into(),
|
||||||
_ => {
|
_ => {
|
||||||
|
|
Loading…
Reference in a new issue