An untitled item is named as it is stored, not by the page each time (#502)
The page named an item its feed gave no title (the first 120 characters of its text, else its file's name) every time it drew one, so the iOS widget, CarPlay, search and Signals never saw a name, and the list had to carry each item's whole text to name the untitled ones (#501). The feed parser now names them the same way as it reads a feed, for ipx and the directory alike, and ipx names the untitled items already stored once, in batches, before making search text, which the name goes into. The reader and the directory's preview leave off a title that is only the opening of the text below it, as they did for an untitled item. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01RqkrVCCH8YcP2Y3i8VNYWA
This commit is contained in:
@@ -21,7 +21,7 @@ pub use failure::{Failure, explain_failure, failure_kind, is_temporary_lookup_fa
|
||||
pub use fetch::{FEED_SILENCE, FEED_TIMEOUT, FETCHED_BYTES, FETCHES, Fetched, fetch, is_youtube_feed};
|
||||
pub use jev::{JEV_URL, classify};
|
||||
pub use outlines::{collect_outlines, is_opml, opml_title, parse_opml, read_opml};
|
||||
pub use parse::{Enclosure, Entry, ParsedFeed, markdown_html, parse, same_file_key};
|
||||
pub use parse::{Enclosure, Entry, ParsedFeed, markdown_html, parse, same_file_key, untitled_name};
|
||||
pub use patreon::{is_patreon_creator, patreon_shows, same_feed};
|
||||
pub use privacy::{redact, redact_text, url_looks_private};
|
||||
|
||||
|
||||
@@ -62,9 +62,53 @@ pub struct Enclosure {
|
||||
pub length: Option<i64>,
|
||||
}
|
||||
|
||||
/// RSS first, then Atom -- the same split the original made on `parsedFeed.version`.
|
||||
/// RSS first, then Atom -- the same split the original made on `parsedFeed.version`. An item
|
||||
/// without a title is named here, once, for everything that shows it (#502).
|
||||
#[tracing::instrument(skip_all, fields(bytes = bytes.len()))]
|
||||
pub fn parse(bytes: &[u8]) -> Result<ParsedFeed> {
|
||||
let mut feed = parse_any(bytes)?;
|
||||
for e in &mut feed.entries {
|
||||
if e.title.is_none() {
|
||||
e.title = untitled_name(e.description.as_deref(), e.enclosures.first().map(|x| x.url.as_str()));
|
||||
}
|
||||
}
|
||||
Ok(feed)
|
||||
}
|
||||
|
||||
/// A name for an item its feed gave no title: the first words of its text, cut at a word near
|
||||
/// 120 characters, or else its file's name without the extension. What the page worked out each
|
||||
/// time it drew one, until the iOS widget, CarPlay and search, which never saw it, needed it too.
|
||||
pub fn untitled_name(description: Option<&str>, file: Option<&str>) -> Option<String> {
|
||||
// Entities decoded, spaces made single; a tag is a space, so "2010)</p><p>Recorded" stays apart.
|
||||
let words = description
|
||||
.and_then(|d| title_text(Some(&text_of(d).split_whitespace().collect::<Vec<_>>().join(" "))))
|
||||
.unwrap_or_default();
|
||||
if !words.is_empty() {
|
||||
let mut chars = words.chars();
|
||||
let head: String = chars.by_ref().take(120).collect();
|
||||
if chars.next().is_none() {
|
||||
return Some(words);
|
||||
}
|
||||
// The word the cut fell in is dropped whole, as the page's /\s+\S*$/ did.
|
||||
let cut = head.rfind(char::is_whitespace).map_or(head.as_str(), |at| head[..at].trim_end());
|
||||
return Some(format!("{cut}\u{2026}"));
|
||||
}
|
||||
let path = file?.split(['?', '#']).next()?;
|
||||
let last = path.rsplit('/').next().filter(|s| !s.is_empty())?;
|
||||
let stem = match last.rsplit_once('.') {
|
||||
Some((stem, ext))
|
||||
if !stem.is_empty() && (1..=5).contains(&ext.len()) && ext.chars().all(|c| c.is_ascii_alphanumeric()) =>
|
||||
{
|
||||
stem
|
||||
}
|
||||
_ => last,
|
||||
};
|
||||
let name =
|
||||
percent_encoding::percent_decode_str(stem).decode_utf8().map_or_else(|_| stem.to_owned(), |d| d.into_owned());
|
||||
non_empty(Some(&name))
|
||||
}
|
||||
|
||||
fn parse_any(bytes: &[u8]) -> Result<ParsedFeed> {
|
||||
if bytes.trim_ascii_start().starts_with(b"[")
|
||||
&& let Ok(posts) = serde_json::from_slice::<Vec<serde_json::Value>>(bytes)
|
||||
{
|
||||
@@ -613,6 +657,37 @@ mod tests {
|
||||
use super::*;
|
||||
use crate::failure;
|
||||
|
||||
#[test]
|
||||
fn an_untitled_item_is_named_by_its_words_or_its_file() {
|
||||
// Short text whole, decoded, the blocks kept apart.
|
||||
assert_eq!(
|
||||
untitled_name(Some("<p>Fish & chips (2010)</p><p>Recorded</p>"), None).as_deref(),
|
||||
Some("Fish & chips (2010) Recorded")
|
||||
);
|
||||
// Long text cut at the last whole word inside 120 characters.
|
||||
let long = "word ".repeat(40);
|
||||
let name = untitled_name(Some(&long), None).unwrap();
|
||||
assert_eq!(name, format!("{}\u{2026}", "word ".repeat(24).trim_end()));
|
||||
assert!(name.chars().count() <= 121);
|
||||
// No text: the file's name, its query and extension gone, its escapes decoded.
|
||||
assert_eq!(
|
||||
untitled_name(Some("<img src=x>"), Some("https://x.example/a/Ep%2042.mp3?t=1")).as_deref(),
|
||||
Some("Ep 42")
|
||||
);
|
||||
assert_eq!(untitled_name(None, Some("https://x.example/")), None);
|
||||
assert_eq!(untitled_name(None, None), None);
|
||||
// Through parse: the feed's own title is kept, and only a missing one is made.
|
||||
let feed = parse(
|
||||
br#"<rss version="2.0"><channel><title>t</title>
|
||||
<item><guid>a</guid><description>Hello there</description></item>
|
||||
<item><guid>b</guid><title>Kept</title><description>Not this</description></item>
|
||||
</channel></rss>"#,
|
||||
)
|
||||
.unwrap();
|
||||
let titles: Vec<_> = feed.entries.iter().map(|e| e.title.as_deref()).collect();
|
||||
assert_eq!(titles, [Some("Hello there"), Some("Kept")]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_rss_with_itunes_extensions() {
|
||||
let bytes = include_bytes!("../../../tests/data/rss2.xml");
|
||||
|
||||
Reference in New Issue
Block a user