diff --git a/rslib/src/card_rendering/mod.rs b/rslib/src/card_rendering/mod.rs index 2f93b85c2..53c6735c0 100644 --- a/rslib/src/card_rendering/mod.rs +++ b/rslib/src/card_rendering/mod.rs @@ -82,6 +82,33 @@ struct OtherTag<'a> { options: HashMap<&'a str, &'a str>, } +impl TtsTag<'_> { + pub(crate) fn new<'a>(content: &'a str, options: Vec<(&'a str, &'a str)>) -> TtsTag<'a> { + let mut options: HashMap<&str, &str> = options.into_iter().collect(); + let lang = options.remove("lang").unwrap_or_default(); + let voices = options + .remove("voices") + .unwrap_or_default() + .split(',') + .collect(); + let speed = options + .remove("speed") + .unwrap_or_default() + .parse() + .unwrap_or(1.0); + let blank = options.remove("cloze_blank"); + + TtsTag { + content, + lang, + voices, + speed, + blank, + options, + } + } +} + #[cfg(feature = "bench")] #[inline] pub fn anki_tag_benchmark() { diff --git a/rslib/src/card_rendering/parser.rs b/rslib/src/card_rendering/parser.rs index bc779aceb..edd38b0c7 100644 --- a/rslib/src/card_rendering/parser.rs +++ b/rslib/src/card_rendering/parser.rs @@ -1,8 +1,6 @@ // Copyright: Ankitects Pty Ltd and contributors // License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html -use std::collections::HashMap; - use nom::{ branch::alt, bytes::complete::{is_not, tag}, @@ -31,34 +29,7 @@ impl<'a> CardNodes<'a> { impl<'a> Tag<'a> { fn new(name: &'a str, options: Vec<(&'a str, &'a str)>, content: &'a str) -> Self { match name { - "tts" => { - let mut lang = ""; - let mut voices = vec![]; - let mut speed = 1.0; - let mut blank = None; - let mut other_options = HashMap::new(); - - for option in options { - match option.0 { - "lang" => lang = option.1, - "voices" => voices = option.1.split(',').collect(), - "speed" => speed = option.1.parse().unwrap_or(1.0), - "cloze_blank" => blank = Some(option.1), - _ => { - other_options.insert(option.0, option.1); - } - } - } - - Self::Tts(TtsTag { - content, - lang, - voices, - speed, - blank, - options: other_options, - }) - } + "tts" => Self::Tts(TtsTag::new(content, options)), _ => Self::Other(OtherTag { name, content, @@ -162,6 +133,8 @@ fn text_node(s: &str) -> IResult { #[cfg(test)] mod test { + use std::collections::HashMap; + use super::*; macro_rules! assert_parsed_nodes {