diff --git a/crates/qbz-slint/src/album.rs b/crates/qbz-slint/src/album.rs index 6a011ea2e..f2fb90d3a 100644 --- a/crates/qbz-slint/src/album.rs +++ b/crates/qbz-slint/src/album.rs @@ -101,7 +101,7 @@ fn map_album(album: Album) -> AlbumData { let description = album .description .as_deref() - .map(strip_html) + .map(crate::strip_html::strip_html) .unwrap_or_default(); // The header description fills the full width to the right of the // artwork, so a longer truncation keeps it from looking like a thin @@ -172,40 +172,10 @@ fn quality_detail(bit_depth: Option, sample_rate: Option) -> String { } /// Crude HTML strip for Qobuz album descriptions. Break and paragraph -/// tags become newlines first so paragraph structure survives; remaining -/// tags are dropped and a few entities decoded. -fn strip_html(input: &str) -> String { - let normalized = input - .replace("
", "\n") - .replace("
", "\n") - .replace("
", "\n") - .replace("
", "\n") - .replace("
", "\n") - .replace("

", "\n\n") - .replace("

", "\n\n"); - - let mut out = String::new(); - let mut in_tag = false; - for ch in normalized.chars() { - match ch { - '<' => in_tag = true, - '>' => in_tag = false, - _ if !in_tag => out.push(ch), - _ => {} - } - } - - let mut out = out - .replace("&", "&") - .replace("'", "'") - .replace(""", "\"") - .replace(" ", " "); - // Collapse runs of blank lines left by stacked tags. - while out.contains("\n\n\n") { - out = out.replace("\n\n\n", "\n\n"); - } - out.trim().to_string() -} +// The previous local strip_html lived here; moved to +// `crate::strip_html` so artist and album views share the same +// paragraph + entity handling and pick up the same future +// improvements. fn map_track(track: Track) -> TrackData { let mut title = track.title; diff --git a/crates/qbz-slint/src/artist.rs b/crates/qbz-slint/src/artist.rs index 0c7d68203..2b86eb9d8 100644 --- a/crates/qbz-slint/src/artist.rs +++ b/crates/qbz-slint/src/artist.rs @@ -93,7 +93,7 @@ fn map_artist(page: PageArtistResponse) -> ArtistData { Some(biography) => { let content = biography .content - .map(|c| strip_html(&c)) + .map(|c| crate::strip_html::strip_html(&c)) .unwrap_or_default(); let source = biography .source @@ -408,34 +408,10 @@ fn mmss(secs: u32) -> String { /// regularly emits in biography bodies, with the © family explicitly /// covered because TiVo-sourced bios often close with a `© TiVo` /// credit line. -fn strip_html(input: &str) -> String { - let mut out = String::new(); - let mut in_tag = false; - for ch in input.chars() { - match ch { - '<' => in_tag = true, - '>' => in_tag = false, - _ if !in_tag => out.push(ch), - _ => {} - } - } - out.replace("&", "&") - .replace("'", "'") - .replace("'", "'") - .replace(""", "\"") - .replace(" ", " ") - .replace("©", "©") - .replace("©", "©") - .replace("©", "©") - .replace("©", "©") - .replace("®", "®") - .replace("™", "™") - .replace("—", "—") - .replace("–", "–") - .replace("…", "…") - .trim() - .to_string() -} +// strip_html now lives in `crate::strip_html` so both album and +// artist views use the same paragraph-preserving conversion. The +// previous artist-local helper produced one long paragraph for +// multi-paragraph biographies — gone with this refactor. fn card_to_item(card: CardData) -> AlbumCardItem { AlbumCardItem { diff --git a/crates/qbz-slint/src/main.rs b/crates/qbz-slint/src/main.rs index 1114537f5..4bcbbd726 100644 --- a/crates/qbz-slint/src/main.rs +++ b/crates/qbz-slint/src/main.rs @@ -24,6 +24,7 @@ mod discovery_dismiss; mod home; mod nav; mod play_history; +mod strip_html; mod playback; mod queue; mod recently; diff --git a/crates/qbz-slint/src/strip_html.rs b/crates/qbz-slint/src/strip_html.rs new file mode 100644 index 000000000..0a50b0b57 --- /dev/null +++ b/crates/qbz-slint/src/strip_html.rs @@ -0,0 +1,195 @@ +//! Convert HTML-ish strings from Qobuz (biographies, album reviews) +//! into Slint-friendly plain text. Slint's `Text` is single-style, so +//! we cannot render inline strong/em formatting — those tags are +//! stripped but their content stays inline. Paragraph and line-break +//! structure IS preserved: `
` collapses to `\n`, `

` to a +//! blank line so Text renders the paragraphs separated visually. + +/// Render an HTML-ish blurb into plain text with paragraph breaks. +pub fn strip_html(input: &str) -> String { + // Normalize break/paragraph tags into newlines first (case- + // insensitive, with and without self-closing slash and whitespace). + let normalized = normalize_breaks(input); + + // Drop every other tag — strong/em/i/b/a/etc lose styling but + // keep their text content because the rest of the loop emits + // characters as-is when not inside a tag. + let mut out = String::with_capacity(normalized.len()); + let mut in_tag = false; + for ch in normalized.chars() { + match ch { + '<' => in_tag = true, + '>' => in_tag = false, + _ if !in_tag => out.push(ch), + _ => {} + } + } + + let decoded = decode_entities(&out); + collapse_blank_lines(&decoded) +} + +fn normalize_breaks(input: &str) -> String { + // Hand-rolled lowercase walk so we match `
`, `
`, `

`, + // etc. without paying for a full regex dep. + let bytes = input.as_bytes(); + let mut out = String::with_capacity(input.len()); + let mut i = 0; + while i < bytes.len() { + if bytes[i] == b'<' { + if let Some((replacement, advance)) = match_break_or_paragraph(&bytes[i..]) { + out.push_str(replacement); + i += advance; + continue; + } + } + out.push(bytes[i] as char); + i += 1; + } + out +} + +/// Try to match `
` (any spacing/case) or `

` (any case) at the +/// start of `b`. Returns the replacement string + byte length to skip +/// when matched. +fn match_break_or_paragraph(b: &[u8]) -> Option<(&'static str, usize)> { + //
,
,
,
,
, ... + if b.len() >= 4 + && b[0] == b'<' + && (b[1] == b'b' || b[1] == b'B') + && (b[2] == b'r' || b[2] == b'R') + { + // Walk to the closing '>' + let mut j = 3usize; + while j < b.len() && b[j] != b'>' { + // Only allow whitespace and a single '/' inside + if !b[j].is_ascii_whitespace() && b[j] != b'/' { + return None; + } + j += 1; + } + if j < b.len() && b[j] == b'>' { + return Some(("\n", j + 1)); + } + return None; + } + //

,

+ if b.len() >= 4 + && b[0] == b'<' + && b[1] == b'/' + && (b[2] == b'p' || b[2] == b'P') + && b[3] == b'>' + { + return Some(("\n\n", 4)); + } + None +} + +fn decode_entities(input: &str) -> String { + // Hand-roll a few of the entities Qobuz uses most. Anything we + // don't recognise is left as-is rather than being garbled. + let mut out = String::with_capacity(input.len()); + let bytes = input.as_bytes(); + let mut i = 0; + while i < bytes.len() { + if bytes[i] == b'&' { + if let Some((replacement, advance)) = match_entity(&bytes[i..]) { + out.push_str(replacement); + i += advance; + continue; + } + } + out.push(bytes[i] as char); + i += 1; + } + out +} + +fn match_entity(b: &[u8]) -> Option<(&'static str, usize)> { + const TABLE: &[(&[u8], &str)] = &[ + (b"&", "&"), + (b"<", "<"), + (b">", ">"), + (b""", "\""), + (b"'", "'"), + (b"'", "'"), + (b" ", " "), + (b"©", "\u{00A9}"), + (b"©", "\u{00A9}"), + (b"©", "\u{00A9}"), + (b"®", "\u{00AE}"), + (b"—", "\u{2014}"), + (b"–", "\u{2013}"), + (b"…", "\u{2026}"), + (b"“", "\u{201C}"), + (b"”", "\u{201D}"), + (b"‘", "\u{2018}"), + (b"’", "\u{2019}"), + ]; + for (needle, replacement) in TABLE { + if b.len() >= needle.len() && b[..needle.len()] == **needle { + return Some((replacement, needle.len())); + } + } + None +} + +fn collapse_blank_lines(input: &str) -> String { + // Slint's word wrap renders `\n\n` as a clean paragraph break. + // Trim leading/trailing whitespace overall and prevent runs of + // 3+ newlines from blowing the layout open. + let trimmed = input.trim(); + let mut out = String::with_capacity(trimmed.len()); + let mut consecutive_newlines = 0; + for ch in trimmed.chars() { + if ch == '\n' { + consecutive_newlines += 1; + if consecutive_newlines <= 2 { + out.push(ch); + } + } else { + consecutive_newlines = 0; + out.push(ch); + } + } + out +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn strips_inline_formatting() { + let html = "

One bold and italic.

"; + let plain = strip_html(html); + assert_eq!(plain, "One bold and italic."); + } + + #[test] + fn converts_br_to_newline() { + let html = "Line 1
Line 2
Line 3"; + assert_eq!(strip_html(html), "Line 1\nLine 2\nLine 3"); + } + + #[test] + fn converts_paragraphs() { + let html = "

First.

Second.

"; + assert_eq!(strip_html(html), "First.\n\nSecond."); + } + + #[test] + fn decodes_common_entities() { + let html = "Rock & Roll — “the rest”."; + assert_eq!(strip_html(html), "Rock & Roll \u{2014} \u{201C}the rest\u{201D}."); + } + + #[test] + fn collapses_excess_newlines() { + let html = "

A

B

C

"; + let out = strip_html(html); + // Three paragraphs separated by single blank line, no extra + // padding above or below. + assert_eq!(out, "A\n\nB\n\nC"); + } +} diff --git a/crates/qbz-slint/ui/foundation/typography.slint b/crates/qbz-slint/ui/foundation/typography.slint index 644bb8e3a..7fc7eddc2 100644 --- a/crates/qbz-slint/ui/foundation/typography.slint +++ b/crates/qbz-slint/ui/foundation/typography.slint @@ -1,14 +1,14 @@ // Typography scale — font sizes and weights. export global Typography { - out property legal: 12px; - out property link: 13px; - out property body: 14px; - out property subtitle: 14px; - out property button: 16px; - out property heading: 18px; - out property title: 24px; - out property wordmark: 28px; + out property legal: 13px; + out property link: 14px; + out property body: 15px; + out property subtitle: 15px; + out property button: 17px; + out property heading: 19px; + out property title: 25px; + out property wordmark: 29px; out property regular: 400; out property medium: 500;