From 84167d6e6be9e15848f52c43a0fd963ea21d724d Mon Sep 17 00:00:00 2001 From: RumovZ Date: Mon, 6 Mar 2023 10:29:19 +0100 Subject: [PATCH] Export static references from sound tags (#2420) --- rslib/src/text.rs | 41 ++++++++++++++++++----------------------- 1 file changed, 18 insertions(+), 23 deletions(-) diff --git a/rslib/src/text.rs b/rslib/src/text.rs index b09c50abf..8dab98830 100644 --- a/rslib/src/text.rs +++ b/rslib/src/text.rs @@ -168,13 +168,15 @@ lazy_static! { /// Strings, src and data attributes with a leading underscore. static ref UNDERSCORED_REFERENCES: Regex = Regex::new( r#"(?x) - "(_[^"]+)" # double quoted - | # or - '(_[^']+)' # single quoted string - | # or - \b(?:src|data) # a 'src' or 'data' attribute - = # followed by - (_[^ >]+) # an unquoted value + \[sound:(_[^]]+)\] # a filename in an Anki sound tag + | # or + "(_[^"]+)" # a double quoted + | # or + '(_[^']+)' # single quoted string + | # or + \b(?:src|data) # a 'src' or 'data' attribute + = # followed by + (_[^ >]+) # an unquoted value "#).unwrap(); } @@ -312,31 +314,24 @@ pub fn replace_media_refs( pub(crate) fn extract_underscored_css_imports(text: &str) -> Vec<&str> { UNDERSCORED_CSS_IMPORTS .captures_iter(text) - .map(|caps| { - caps.get(1) - .or_else(|| caps.get(2)) - .or_else(|| caps.get(3)) - .or_else(|| caps.get(4)) - .or_else(|| caps.get(5)) - .unwrap() - .as_str() - }) + .map(extract_match) .collect() } pub(crate) fn extract_underscored_references(text: &str) -> Vec<&str> { UNDERSCORED_REFERENCES .captures_iter(text) - .map(|caps| { - caps.get(1) - .or_else(|| caps.get(2)) - .or_else(|| caps.get(3)) - .unwrap() - .as_str() - }) + .map(extract_match) .collect() } +/// Returns the first matching group as a str. This is intended for regexes +/// where exactly one group matches, and will panic for matches without matching +/// groups. +fn extract_match(caps: Captures) -> &str { + caps.iter().skip(1).find_map(|g| g).unwrap().as_str() +} + pub fn strip_html_preserving_media_filenames(html: &str) -> Cow { HTML_MEDIA_TAGS .replace_all(html, r" ${1}${2}${3} ")