mirror of
https://github.com/ankitects/anki.git
synced 2025-09-18 14:02:21 -04:00
expand clozes before rendering LaTeX
https://anki.tenderapp.com/discussions/ankidesktop/39071-latex-media-windows-version-2121-683b7983-fail-rendering-latex
This commit is contained in:
parent
cd8d1b5dcd
commit
9f7c3a2fcf
7 changed files with 39 additions and 15 deletions
|
@ -260,6 +260,7 @@ message TTSTag {
|
|||
message ExtractLatexIn {
|
||||
string text = 1;
|
||||
bool svg = 2;
|
||||
bool expand_clozes = 3;
|
||||
}
|
||||
|
||||
message ExtractLatexOut {
|
||||
|
|
|
@ -49,7 +49,7 @@ def render_latex(html: str, model: NoteType, col: anki.storage._Collection,) ->
|
|||
|
||||
|
||||
def render_latex_returning_errors(
|
||||
html: str, model: NoteType, col: anki.storage._Collection
|
||||
html: str, model: NoteType, col: anki.storage._Collection, expand_clozes=False
|
||||
) -> Tuple[str, List[str]]:
|
||||
"""Returns (text, errors).
|
||||
|
||||
|
@ -58,7 +58,7 @@ def render_latex_returning_errors(
|
|||
header = model["latexPre"]
|
||||
footer = model["latexPost"]
|
||||
|
||||
out = col.backend.extract_latex(html, svg)
|
||||
out = col.backend.extract_latex(html, svg, expand_clozes)
|
||||
errors = []
|
||||
html = out.html
|
||||
|
||||
|
|
|
@ -191,7 +191,9 @@ class MediaManager:
|
|||
):
|
||||
|
||||
model = self.col.models.get(mid)
|
||||
_html, errors = render_latex_returning_errors(flds, model, self.col)
|
||||
_html, errors = render_latex_returning_errors(
|
||||
flds, model, self.col, expand_clozes=True
|
||||
)
|
||||
if errors:
|
||||
return (nid, "\n".join(errors))
|
||||
|
||||
|
|
|
@ -292,9 +292,15 @@ class RustBackend:
|
|||
|
||||
return out.text, native_tags
|
||||
|
||||
def extract_latex(self, text: str, svg: bool) -> ExtractedLatexOutput:
|
||||
def extract_latex(
|
||||
self, text: str, svg: bool, expand_clozes: bool
|
||||
) -> ExtractedLatexOutput:
|
||||
out = self._run_command(
|
||||
pb.BackendInput(extract_latex=pb.ExtractLatexIn(text=text, svg=svg))
|
||||
pb.BackendInput(
|
||||
extract_latex=pb.ExtractLatexIn(
|
||||
text=text, svg=svg, expand_clozes=expand_clozes
|
||||
)
|
||||
)
|
||||
).extract_latex
|
||||
|
||||
return ExtractedLatexOutput(
|
||||
|
|
|
@ -6,7 +6,7 @@ use crate::backend_proto::backend_input::Value;
|
|||
use crate::backend_proto::{Empty, RenderedTemplateReplacement, SyncMediaIn};
|
||||
use crate::err::{AnkiError, NetworkErrorKind, Result, SyncErrorKind};
|
||||
use crate::i18n::{tr_args, FString, I18n};
|
||||
use crate::latex::{extract_latex, ExtractedLatex};
|
||||
use crate::latex::{extract_latex, extract_latex_expanding_clozes, ExtractedLatex};
|
||||
use crate::media::check::MediaChecker;
|
||||
use crate::media::sync::MediaSyncProgress;
|
||||
use crate::media::MediaManager;
|
||||
|
@ -340,7 +340,12 @@ impl Backend {
|
|||
}
|
||||
|
||||
fn extract_latex(&self, input: pb::ExtractLatexIn) -> pb::ExtractLatexOut {
|
||||
let (text, extracted) = extract_latex(&input.text, input.svg);
|
||||
let func = if input.expand_clozes {
|
||||
extract_latex_expanding_clozes
|
||||
} else {
|
||||
extract_latex
|
||||
};
|
||||
let (text, extracted) = func(&input.text, input.svg);
|
||||
|
||||
pb::ExtractLatexOut {
|
||||
text,
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
// Copyright: Ankitects Pty Ltd and contributors
|
||||
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
||||
|
||||
use crate::cloze::expand_clozes_to_reveal_latex;
|
||||
use crate::media::files::sha1_of_data;
|
||||
use crate::text::strip_html;
|
||||
use lazy_static::lazy_static;
|
||||
|
@ -38,6 +39,21 @@ pub struct ExtractedLatex {
|
|||
pub latex: String,
|
||||
}
|
||||
|
||||
/// Expand any cloze deletions, then extract LaTeX.
|
||||
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()
|
||||
} else {
|
||||
text.into()
|
||||
};
|
||||
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>) {
|
||||
let mut extracted = vec![];
|
||||
|
||||
|
|
|
@ -1,10 +1,9 @@
|
|||
// Copyright: Ankitects Pty Ltd and contributors
|
||||
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
||||
|
||||
use crate::cloze::expand_clozes_to_reveal_latex;
|
||||
use crate::err::{AnkiError, Result};
|
||||
use crate::i18n::{tr_args, tr_strs, FString, I18n};
|
||||
use crate::latex::extract_latex;
|
||||
use crate::latex::extract_latex_expanding_clozes;
|
||||
use crate::media::col::{
|
||||
for_every_note, get_note_types, mark_collection_modified, open_or_create_collection_db,
|
||||
set_note, Note,
|
||||
|
@ -429,12 +428,7 @@ fn find_unused_and_missing(
|
|||
|
||||
fn extract_latex_refs(note: &Note, seen_files: &mut HashSet<String>, svg: bool) {
|
||||
for field in note.fields() {
|
||||
let field_text: Cow<str> = if field.contains("{{c") {
|
||||
expand_clozes_to_reveal_latex(field).into()
|
||||
} else {
|
||||
field.into()
|
||||
};
|
||||
let (_, extracted) = extract_latex(field_text.as_ref(), svg);
|
||||
let (_, extracted) = extract_latex_expanding_clozes(field, svg);
|
||||
for e in extracted {
|
||||
seen_files.insert(e.fname);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue