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. /// Adjust filename into the format Anki expects.
/// ///
/// - The filename is normalized to NFC. /// - 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. /// See normalize_filename(). This function expects NFC-normalized input.
pub(crate) fn normalize_nfc_filename(mut fname: Cow<str>) -> Cow<str> { 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() 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}") { if let Cow::Owned(o) = WINDOWS_DEVICE_NAME.replace_all(fname.as_ref(), "${1}_${2}") {
fname = o.into(); 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> { pub fn decode_entities(html: &str) -> Cow<str> {
if html.contains('&') { if html.contains('&') {
match htmlescape::decode_html(html) { match htmlescape::decode_html(html) {
Ok(text) => text.replace("\u{a0}", " "), Ok(text) => text.replace('\u{a0}', " "),
Err(e) => format!("{:?}", e), Err(e) => format!("{:?}", e),
} }
.into() .into()