Add AV prettifier for use in browser table

This commit is contained in:
RumovZ 2021-12-15 12:57:33 +01:00
parent e033525fc3
commit 5b5e6d7c72
3 changed files with 29 additions and 3 deletions

View file

@ -9,7 +9,7 @@ use strum::{Display, EnumIter, EnumString, IntoEnumIterator};
use crate::{
backend_proto as pb,
card::{CardQueue, CardType},
card_rendering::extract_av_tags,
card_rendering::prettify_av_tags,
notetype::{CardTemplate, NotetypeKind},
prelude::*,
scheduler::{timespan::time_span, timing::SchedTimingToday},
@ -271,7 +271,7 @@ impl RenderContext {
} => current_text,
})
.join("");
let question = extract_av_tags(&qnodes_text, true, &col.tr).0;
let question = prettify_av_tags(&qnodes_text);
Ok(RenderContext {
question,
@ -411,7 +411,7 @@ impl RowContext {
} => current_text,
})
.join("");
let answer = extract_av_tags(&answer, false, &self.tr).0;
let answer = prettify_av_tags(&answer);
html_to_text_line(
if let Some(stripped) = answer.strip_prefix(&render_context.question) {
stripped

View file

@ -17,6 +17,10 @@ pub fn extract_av_tags(txt: &str, question_side: bool, tr: &I18n) -> (String, Ve
CardNodes::parse(txt).write_and_extract_av_tags(question_side, tr)
}
pub fn prettify_av_tags(txt: &str) -> String {
CardNodes::parse(txt).write_with_pretty_av_tags()
}
#[derive(Debug, PartialEq)]
struct CardNodes<'a>(Vec<Node<'a>>);

View file

@ -23,6 +23,10 @@ impl<'a> CardNodes<'a> {
let mut extractor = AvExtractor::new(question_side, tr);
(extractor.write(self), extractor.tags)
}
pub(super) fn write_with_pretty_av_tags(&self) -> String {
AvPrettifier::new().write(self)
}
}
trait Write {
@ -168,6 +172,24 @@ impl Write for AvExtractor<'_> {
}
}
struct AvPrettifier;
impl AvPrettifier {
fn new() -> Self {
Self {}
}
}
impl Write for AvPrettifier {
fn write_sound(&mut self, buf: &mut String, resource: &str) {
write!(buf, "🔉{}🔉", resource).unwrap();
}
fn write_tts_tag(&mut self, buf: &mut String, tag: &TtsTag) {
write!(buf, "💬{}💬", tag.content).unwrap();
}
}
#[cfg(test)]
mod test {
use super::*;