fix nonbreaking spaces breaking media

https://forums.ankiweb.net/t/unable-to-play-longer-audio-on-cards/1313
This commit is contained in:
Damien Elmes 2020-08-30 11:23:12 +10:00
parent f7928fe14f
commit 9fcd6c66f4
2 changed files with 12 additions and 2 deletions

View File

@ -67,6 +67,10 @@ fn disallowed_char(char: char) -> bool {
}
}
fn nonbreaking_space(char: char) -> bool {
char == '\u{a0}'
}
/// Adjust filename into the format Anki expects.
///
/// - The filename is normalized to NFC.
@ -85,10 +89,16 @@ pub(crate) fn normalize_filename(fname: &str) -> Cow<str> {
/// See normalize_filename(). This function expects NFC-normalized input.
pub(crate) fn normalize_nfc_filename(mut fname: Cow<str>) -> Cow<str> {
if fname.chars().any(disallowed_char) {
if fname.contains(disallowed_char) {
fname = fname.replace(disallowed_char, "").into()
}
// convert nonbreaking spaces to regular ones, as the filename extraction
// code treats nonbreaking spaces as regular ones
if fname.contains(nonbreaking_space) {
fname = fname.replace(nonbreaking_space, " ").into()
}
if let Cow::Owned(o) = WINDOWS_DEVICE_NAME.replace_all(fname.as_ref(), "${1}_${2}") {
fname = o.into();
}

View File

@ -95,7 +95,7 @@ pub fn strip_html_preserving_entities(html: &str) -> Cow<str> {
pub fn decode_entities(html: &str) -> Cow<str> {
if html.contains('&') {
match htmlescape::decode_html(html) {
Ok(text) => text.replace("\u{a0}", " "),
Ok(text) => text.replace('\u{a0}', " "),
Err(e) => format!("{:?}", e),
}
.into()