mirror of
https://github.com/ankitects/anki.git
synced 2025-09-19 14:32:22 -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);
|
||||
|
||||
Ok(anki_proto::card_rendering::ExtractLatexResponse {
|
||||
text,
|
||||
text: text.into_owned(),
|
||||
latex: extracted
|
||||
.into_iter()
|
||||
.map(
|
||||
|
|
|
@ -46,18 +46,19 @@ pub struct ExtractedLatex {
|
|||
pub(crate) fn extract_latex_expanding_clozes(
|
||||
text: &str,
|
||||
svg: bool,
|
||||
) -> (String, Vec<ExtractedLatex>) {
|
||||
let text: Cow<str> = if text.contains("{{c") {
|
||||
expand_clozes_to_reveal_latex(text).into()
|
||||
) -> (Cow<str>, Vec<ExtractedLatex>) {
|
||||
if text.contains("{{c") {
|
||||
let expanded = expand_clozes_to_reveal_latex(text);
|
||||
let (text, extracts) = extract_latex(&expanded, svg);
|
||||
(text.into_owned().into(), extracts)
|
||||
} else {
|
||||
text.into()
|
||||
};
|
||||
extract_latex(&text, svg)
|
||||
extract_latex(text, svg)
|
||||
}
|
||||
}
|
||||
|
||||
/// Extract LaTeX from the provided text.
|
||||
/// 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 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
|
||||
});
|
||||
|
||||
(new_text.into(), extracted)
|
||||
(new_text, extracted)
|
||||
}
|
||||
|
||||
fn strip_html_for_latex(html: &str) -> Cow<str> {
|
||||
|
@ -122,7 +123,8 @@ mod test {
|
|||
format!(
|
||||
"a<img class=latex alt=\"one
and
two\" src=\"{}\">b",
|
||||
fname
|
||||
),
|
||||
)
|
||||
.into(),
|
||||
vec![ExtractedLatex {
|
||||
fname: fname.into(),
|
||||
latex: "one\nand\ntwo".into()
|
||||
|
|
Loading…
Reference in a new issue