handle mathjax+cloze case

instead of trying to selectively change clozes from c to C, just
strip HTML from inside the mathjax sections instead
This commit is contained in:
Damien Elmes 2020-01-11 12:57:32 +10:00
parent cc09ca34d4
commit 389bf07b52

View file

@ -68,8 +68,9 @@ fn apply_filter<'a>(filter_name: &str, text: &'a str, field_name: &str) -> (bool
match base { match base {
"type" => type_filter(text, filter_args, field_name), "type" => type_filter(text, filter_args, field_name),
"hint" => hint_filter(text, field_name), "hint" => hint_filter(text, field_name),
//"cq" => cloze_filter(text, filter_args, true), "cq" => cloze_filter(text, filter_args, true),
//"ca" => cloze_filter(text, filter_args, false), "ca" => cloze_filter(text, filter_args, false),
// unrecognized filter
_ => return (false, None), _ => return (false, None),
} }
} }
@ -90,14 +91,22 @@ fn apply_filter<'a>(filter_name: &str, text: &'a str, field_name: &str) -> (bool
lazy_static! { lazy_static! {
static ref CLOZE: Regex = Regex::new( static ref CLOZE: Regex = Regex::new(
r#"(?xsi) r#"(?xsi)
\{\{ \{\{
(c)(\d+):: # 1 = c or C, 2 = cloze number (c)(\d+):: # 1 = c or C, 2 = cloze number
(.*?) # 3 = clozed text (.*?) # 3 = clozed text
(?: (?:
::(.*?) # 4 = optional hint ::(.*?) # 4 = optional hint
)? )?
\}\} \}\}
"# "#
)
.unwrap();
static ref MATHJAX: Regex = Regex::new(
r#"(?xsi)
(\\[(\[]) # 1 = mathjax opening tag
(.*?) # 2 = inner content
(\\[])]) # 3 = mathjax closing tag
"#
) )
.unwrap(); .unwrap();
} }
@ -113,6 +122,12 @@ mod cloze_caps {
pub static HINT: usize = 4; pub static HINT: usize = 4;
} }
mod mathjax_caps {
pub static OPENING_TAG: usize = 1;
pub static INNER_TEXT: usize = 2;
pub static CLOSING_TAG: usize = 3;
}
fn reveal_cloze_text(text: &str, ord: u16, question: bool) -> Cow<str> { fn reveal_cloze_text(text: &str, ord: u16, question: bool) -> Cow<str> {
let output = CLOZE.replace_all(text, |caps: &Captures| { let output = CLOZE.replace_all(text, |caps: &Captures| {
let captured_ord = caps let captured_ord = caps
@ -154,10 +169,22 @@ fn reveal_cloze_text(text: &str, ord: u16, question: bool) -> Cow<str> {
} }
} }
#[allow(dead_code)] fn strip_html_inside_mathjax(text: &str) -> Cow<str> {
MATHJAX.replace_all(text, |caps: &Captures| -> String {
format!(
"{}{}{}",
caps.get(mathjax_caps::OPENING_TAG).unwrap().as_str(),
strip_html(caps.get(mathjax_caps::INNER_TEXT).unwrap().as_str()).as_ref(),
caps.get(mathjax_caps::CLOSING_TAG).unwrap().as_str()
)
})
}
fn cloze_filter<'a>(text: &'a str, filter_args: &str, question: bool) -> Cow<'a, str> { fn cloze_filter<'a>(text: &'a str, filter_args: &str, question: bool) -> Cow<'a, str> {
let cloze_ord = filter_args.parse().unwrap_or(0); let cloze_ord = filter_args.parse().unwrap_or(0);
reveal_cloze_text(text, cloze_ord, question) strip_html_inside_mathjax(reveal_cloze_text(text, cloze_ord, question).as_ref())
.into_owned()
.into()
} }
// Ruby filters // Ruby filters