added flag_av_tags() for replay button support

This commit is contained in:
Damien Elmes 2020-01-21 14:44:27 +10:00
parent 7504ef6857
commit 4fb227ca4c
4 changed files with 22 additions and 3 deletions

View file

@ -18,6 +18,7 @@ message BackendInput {
int64 local_minutes_west = 22; int64 local_minutes_west = 22;
string strip_av_tags = 23; string strip_av_tags = 23;
string get_av_tags = 24; string get_av_tags = 24;
string flag_av_tags = 25;
} }
} }
@ -32,6 +33,7 @@ message BackendOutput {
sint32 local_minutes_west = 22; sint32 local_minutes_west = 22;
string strip_av_tags = 23; string strip_av_tags = 23;
GetAVTagsOut get_av_tags = 24; GetAVTagsOut get_av_tags = 24;
string flag_av_tags = 25;
BackendError error = 2047; BackendError error = 2047;
} }

View file

@ -170,3 +170,6 @@ class RustBackend:
).get_av_tags.av_tags, ).get_av_tags.av_tags,
) )
) )
def flag_av_tags(self, text: str) -> str:
return self._run_command(pb.BackendInput(flag_av_tags=text)).flag_av_tags

View file

@ -10,7 +10,7 @@ use crate::template::{
render_card, without_legacy_template_directives, FieldMap, FieldRequirements, ParsedTemplate, render_card, without_legacy_template_directives, FieldMap, FieldRequirements, ParsedTemplate,
RenderedNode, RenderedNode,
}; };
use crate::text::{av_tags_in_string, strip_av_tags, AVTag}; use crate::text::{av_tags_in_string, flag_av_tags, strip_av_tags, AVTag};
use prost::Message; use prost::Message;
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use std::path::PathBuf; use std::path::PathBuf;
@ -101,6 +101,7 @@ impl Backend {
} }
Value::StripAvTags(text) => OValue::StripAvTags(strip_av_tags(&text).into()), Value::StripAvTags(text) => OValue::StripAvTags(strip_av_tags(&text).into()),
Value::GetAvTags(text) => OValue::GetAvTags(self.get_av_tags(&text)), Value::GetAvTags(text) => OValue::GetAvTags(self.get_av_tags(&text)),
Value::FlagAvTags(text) => OValue::FlagAvTags(flag_av_tags(&text).into()),
}) })
} }

View file

@ -3,7 +3,7 @@
use htmlescape; use htmlescape;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use regex::Regex; use regex::{Captures, Regex};
use std::borrow::Cow; use std::borrow::Cow;
use std::collections::HashSet; use std::collections::HashSet;
use std::ptr; use std::ptr;
@ -78,6 +78,14 @@ pub fn strip_av_tags(text: &str) -> Cow<str> {
AV_TAGS.replace_all(text, "") AV_TAGS.replace_all(text, "")
} }
pub fn flag_av_tags(text: &str) -> Cow<str> {
let mut idx = 0;
AV_TAGS.replace_all(text, |_caps: &Captures| {
let text = format!("[anki:play]{}[/anki:play]", idx);
idx += 1;
text
})
}
pub fn av_tags_in_string(text: &str) -> impl Iterator<Item = AVTag> { pub fn av_tags_in_string(text: &str) -> impl Iterator<Item = AVTag> {
AV_TAGS.captures_iter(text).map(|caps| { AV_TAGS.captures_iter(text).map(|caps| {
if let Some(av_file) = caps.get(1) { if let Some(av_file) = caps.get(1) {
@ -141,7 +149,7 @@ pub fn cloze_numbers_in_string(html: &str) -> HashSet<u16> {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use crate::text::{ use crate::text::{
av_tags_in_string, cloze_numbers_in_string, strip_av_tags, strip_html, av_tags_in_string, cloze_numbers_in_string, flag_av_tags, strip_av_tags, strip_html,
strip_html_preserving_image_filenames, AVTag, strip_html_preserving_image_filenames, AVTag,
}; };
use std::collections::HashSet; use std::collections::HashSet;
@ -192,5 +200,10 @@ mod test {
}, },
] ]
); );
assert_eq!(
flag_av_tags(s),
"abc[anki:play]0[/anki:play]def[anki:play]1[/anki:play]gh"
);
} }
} }