Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ Yuuki Gabriele Patriarca <yuukigpatriarca@gmail.com>
SecretX <https://github.com/SecretX33>
Daniel Pechersky <danny.pechersky@gmail.com>
fernandolins <1887601+fernandolins@users.noreply.github.com>
Athitheya Gobinathan <athith.g@gmail.com>

********************

Expand Down
53 changes: 47 additions & 6 deletions rslib/src/cloze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,44 @@ fn tokenize(mut text: &str) -> impl Iterator<Item = Token<'_>> {
nom::error::ErrorKind::Eof,
)));
}
let mut other_token = alt((open_cloze, close_cloze));
// start with the no-match case
let mut index = text.len();
for (idx, _) in text.char_indices() {
if other_token.parse(&text[idx..]).is_ok() {
index = idx;
let mut i = 0;
let mut mathjax_end: Option<&str> = None;
while i < text.len() {
let rest = &text[i..];
// Cloze openings should be recognized everywhere, including inside MathJax.
if open_cloze(rest).is_ok() {
index = i;
break;
}
// Cloze closings should only be recognized outside MathJax.
if mathjax_end.is_none() && close_cloze(rest).is_ok() {
index = i;
break;
}
// Enter MathJax when not already inside it.
if mathjax_end.is_none() {
if rest.starts_with(r"\(") {
mathjax_end = Some(r"\)");
i += 2;
continue;
}
if rest.starts_with(r"\[") {
mathjax_end = Some(r"\]");
i += 2;
continue;
}
} else if let Some(end) = mathjax_end {
// Exit MathJax when we hit the matching delimiter.
if rest.starts_with(end) {
mathjax_end = None;
i += 2;
continue;
}
}
i += rest.chars().next().unwrap().len_utf8();
}
Ok((&text[index..], Token::Text(&text[0..index])))
Ok((&text[index..], Token::Text(&text[..index])))
}

std::iter::from_fn(move || {
Expand Down Expand Up @@ -665,6 +693,19 @@ mod test {
);
}

#[test]
fn cloze_with_mathjax_braces() {
let text = r"{{c1:: \( \frac{1}{\sqrt{\pi}} \) }}";
assert_eq!(
strip_html(reveal_cloze_text(text, 1, true).as_ref()),
"[...]"
);
assert_eq!(
strip_html(reveal_cloze_text(text, 1, false).as_ref()),
r" \( \frac{1}{\sqrt{\pi}} \) "
);
}

#[test]
fn non_latin() {
assert!(cloze_numbers_in_string("öaöaöööaö").is_empty());
Expand Down