mirror of
https://github.com/ankitects/anki.git
synced 2025-09-25 01:06:35 -04:00
Accept String in av tag routines
... and avoid redundant writes if no changes need to be made.
This commit is contained in:
parent
5b5e6d7c72
commit
a94042b491
3 changed files with 30 additions and 10 deletions
|
@ -22,7 +22,7 @@ impl CardRenderingService for Backend {
|
|||
&self,
|
||||
input: pb::ExtractAvTagsRequest,
|
||||
) -> Result<pb::ExtractAvTagsResponse> {
|
||||
let out = extract_av_tags(&input.text, input.question_side, self.i18n());
|
||||
let out = extract_av_tags(input.text, input.question_side, self.i18n());
|
||||
Ok(pb::ExtractAvTagsResponse {
|
||||
text: out.0,
|
||||
av_tags: out.1,
|
||||
|
@ -117,7 +117,7 @@ impl CardRenderingService for Backend {
|
|||
}
|
||||
|
||||
fn strip_av_tags(&self, input: pb::String) -> Result<pb::String> {
|
||||
Ok(strip_av_tags(&input.val).into())
|
||||
Ok(strip_av_tags(input.val).into())
|
||||
}
|
||||
|
||||
fn render_markdown(&self, input: pb::RenderMarkdownRequest) -> Result<pb::String> {
|
||||
|
|
|
@ -271,7 +271,7 @@ impl RenderContext {
|
|||
} => current_text,
|
||||
})
|
||||
.join("");
|
||||
let question = prettify_av_tags(&qnodes_text);
|
||||
let question = prettify_av_tags(qnodes_text);
|
||||
|
||||
Ok(RenderContext {
|
||||
question,
|
||||
|
@ -411,7 +411,7 @@ impl RowContext {
|
|||
} => current_text,
|
||||
})
|
||||
.join("");
|
||||
let answer = prettify_av_tags(&answer);
|
||||
let answer = prettify_av_tags(answer);
|
||||
html_to_text_line(
|
||||
if let Some(stripped) = answer.strip_prefix(&render_context.question) {
|
||||
stripped
|
||||
|
|
|
@ -9,16 +9,36 @@ use crate::prelude::*;
|
|||
mod parser;
|
||||
mod writer;
|
||||
|
||||
pub fn strip_av_tags(txt: &str) -> String {
|
||||
CardNodes::parse(txt).write_without_av_tags()
|
||||
pub fn strip_av_tags<S: Into<String> + AsRef<str>>(txt: S) -> String {
|
||||
nodes_or_text_only(txt.as_ref())
|
||||
.map(|nodes| nodes.write_without_av_tags())
|
||||
.unwrap_or_else(|| txt.into())
|
||||
}
|
||||
|
||||
pub fn extract_av_tags(txt: &str, question_side: bool, tr: &I18n) -> (String, Vec<pb::AvTag>) {
|
||||
CardNodes::parse(txt).write_and_extract_av_tags(question_side, tr)
|
||||
pub fn extract_av_tags<S: Into<String> + AsRef<str>>(
|
||||
txt: S,
|
||||
question_side: bool,
|
||||
tr: &I18n,
|
||||
) -> (String, Vec<pb::AvTag>) {
|
||||
nodes_or_text_only(txt.as_ref())
|
||||
.map(|nodes| nodes.write_and_extract_av_tags(question_side, tr))
|
||||
.unwrap_or_else(|| (txt.into(), vec![]))
|
||||
}
|
||||
|
||||
pub fn prettify_av_tags(txt: &str) -> String {
|
||||
CardNodes::parse(txt).write_with_pretty_av_tags()
|
||||
pub fn prettify_av_tags<S: Into<String> + AsRef<str>>(txt: S) -> String {
|
||||
nodes_or_text_only(txt.as_ref())
|
||||
.map(|nodes| nodes.write_with_pretty_av_tags())
|
||||
.unwrap_or_else(|| txt.into())
|
||||
}
|
||||
|
||||
/// Parse `txt` into [CardNodes] and return the result,
|
||||
/// or [None] if it is only a text node.
|
||||
fn nodes_or_text_only(txt: &str) -> Option<CardNodes> {
|
||||
let nodes = CardNodes::parse(txt);
|
||||
match nodes.0[..] {
|
||||
[Node::Text(_)] => None,
|
||||
_ => Some(nodes),
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
|
|
Loading…
Reference in a new issue