Accept String in av tag routines

... and avoid redundant writes if no changes need to be made.
This commit is contained in:
RumovZ 2021-12-16 21:55:28 +01:00
parent 5b5e6d7c72
commit a94042b491
3 changed files with 30 additions and 10 deletions

View file

@ -22,7 +22,7 @@ impl CardRenderingService for Backend {
&self, &self,
input: pb::ExtractAvTagsRequest, input: pb::ExtractAvTagsRequest,
) -> Result<pb::ExtractAvTagsResponse> { ) -> 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 { Ok(pb::ExtractAvTagsResponse {
text: out.0, text: out.0,
av_tags: out.1, av_tags: out.1,
@ -117,7 +117,7 @@ impl CardRenderingService for Backend {
} }
fn strip_av_tags(&self, input: pb::String) -> Result<pb::String> { 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> { fn render_markdown(&self, input: pb::RenderMarkdownRequest) -> Result<pb::String> {

View file

@ -271,7 +271,7 @@ impl RenderContext {
} => current_text, } => current_text,
}) })
.join(""); .join("");
let question = prettify_av_tags(&qnodes_text); let question = prettify_av_tags(qnodes_text);
Ok(RenderContext { Ok(RenderContext {
question, question,
@ -411,7 +411,7 @@ impl RowContext {
} => current_text, } => current_text,
}) })
.join(""); .join("");
let answer = prettify_av_tags(&answer); let answer = prettify_av_tags(answer);
html_to_text_line( html_to_text_line(
if let Some(stripped) = answer.strip_prefix(&render_context.question) { if let Some(stripped) = answer.strip_prefix(&render_context.question) {
stripped stripped

View file

@ -9,16 +9,36 @@ use crate::prelude::*;
mod parser; mod parser;
mod writer; mod writer;
pub fn strip_av_tags(txt: &str) -> String { pub fn strip_av_tags<S: Into<String> + AsRef<str>>(txt: S) -> String {
CardNodes::parse(txt).write_without_av_tags() 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>) { pub fn extract_av_tags<S: Into<String> + AsRef<str>>(
CardNodes::parse(txt).write_and_extract_av_tags(question_side, tr) 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 { pub fn prettify_av_tags<S: Into<String> + AsRef<str>>(txt: S) -> String {
CardNodes::parse(txt).write_with_pretty_av_tags() 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)] #[derive(Debug, PartialEq)]