mirror of
https://github.com/ankitects/anki.git
synced 2025-09-19 22:42:25 -04:00
Avoid allocating in extract_latex() if possible; make public
This commit is contained in:
parent
87e2cf7976
commit
d2022d1a6d
2 changed files with 12 additions and 10 deletions
|
@ -53,7 +53,7 @@ impl crate::services::CardRenderingService for Collection {
|
||||||
let (text, extracted) = func(&input.text, input.svg);
|
let (text, extracted) = func(&input.text, input.svg);
|
||||||
|
|
||||||
Ok(anki_proto::card_rendering::ExtractLatexResponse {
|
Ok(anki_proto::card_rendering::ExtractLatexResponse {
|
||||||
text,
|
text: text.into_owned(),
|
||||||
latex: extracted
|
latex: extracted
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(
|
.map(
|
||||||
|
|
|
@ -46,18 +46,19 @@ pub struct ExtractedLatex {
|
||||||
pub(crate) fn extract_latex_expanding_clozes(
|
pub(crate) fn extract_latex_expanding_clozes(
|
||||||
text: &str,
|
text: &str,
|
||||||
svg: bool,
|
svg: bool,
|
||||||
) -> (String, Vec<ExtractedLatex>) {
|
) -> (Cow<str>, Vec<ExtractedLatex>) {
|
||||||
let text: Cow<str> = if text.contains("{{c") {
|
if text.contains("{{c") {
|
||||||
expand_clozes_to_reveal_latex(text).into()
|
let expanded = expand_clozes_to_reveal_latex(text);
|
||||||
|
let (text, extracts) = extract_latex(&expanded, svg);
|
||||||
|
(text.into_owned().into(), extracts)
|
||||||
} else {
|
} else {
|
||||||
text.into()
|
extract_latex(text, svg)
|
||||||
};
|
}
|
||||||
extract_latex(&text, svg)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Extract LaTeX from the provided text.
|
/// Extract LaTeX from the provided text.
|
||||||
/// Expects cloze deletions to already be expanded.
|
/// Expects cloze deletions to already be expanded.
|
||||||
pub(crate) fn extract_latex(text: &str, svg: bool) -> (String, Vec<ExtractedLatex>) {
|
pub fn extract_latex(text: &str, svg: bool) -> (Cow<str>, Vec<ExtractedLatex>) {
|
||||||
let mut extracted = vec![];
|
let mut extracted = vec![];
|
||||||
|
|
||||||
let new_text = LATEX.replace_all(text, |caps: &Captures| {
|
let new_text = LATEX.replace_all(text, |caps: &Captures| {
|
||||||
|
@ -78,7 +79,7 @@ pub(crate) fn extract_latex(text: &str, svg: bool) -> (String, Vec<ExtractedLate
|
||||||
img_link
|
img_link
|
||||||
});
|
});
|
||||||
|
|
||||||
(new_text.into(), extracted)
|
(new_text, extracted)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn strip_html_for_latex(html: &str) -> Cow<str> {
|
fn strip_html_for_latex(html: &str) -> Cow<str> {
|
||||||
|
@ -122,7 +123,8 @@ mod test {
|
||||||
format!(
|
format!(
|
||||||
"a<img class=latex alt=\"one
and
two\" src=\"{}\">b",
|
"a<img class=latex alt=\"one
and
two\" src=\"{}\">b",
|
||||||
fname
|
fname
|
||||||
),
|
)
|
||||||
|
.into(),
|
||||||
vec![ExtractedLatex {
|
vec![ExtractedLatex {
|
||||||
fname: fname.into(),
|
fname: fname.into(),
|
||||||
latex: "one\nand\ntwo".into()
|
latex: "one\nand\ntwo".into()
|
||||||
|
|
Loading…
Reference in a new issue