Export static references from sound tags (#2420)

This commit is contained in:
RumovZ 2023-03-06 10:29:19 +01:00 committed by GitHub
parent e23036ea8b
commit 84167d6e6b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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<str> {
HTML_MEDIA_TAGS
.replace_all(html, r" ${1}${2}${3} ")