Merge branch 'feature/feed-images-from-feed-5258'

This commit is contained in:
Brent Simmons committed 2026-04-14 20:22:55 -07:00
commit e91b5b78d4
11 files changed
+136 -27

No files matched your search

+7 -8
View File
@@ -115,21 +115,20 @@ private extension IconView {
let imageSize = image.size
let viewSize = bounds.size
if imageSize.height == imageSize.width {
if imageSize.height >= viewSize.height * 0.75 {
// Close enough to viewSize to scale up the image.
return NSRect(x: 0.0, y: 0.0, width: viewSize.width, height: viewSize.height)
}
let offset = floor((viewSize.height - imageSize.height) / 2.0)
return NSRect(x: offset, y: offset, width: imageSize.width, height: imageSize.height)
} else if imageSize.height > imageSize.width {
return NSRect(x: 0.0, y: 0.0, width: viewSize.width, height: viewSize.height)
}
// Non-square: aspect fit — scale to fill one dimension, center in the other
if imageSize.height > imageSize.width {
let factor = viewSize.height / imageSize.height
let width = imageSize.width * factor
let originX = floor((viewSize.width - width) / 2.0)
return NSRect(x: originX, y: 0.0, width: width, height: viewSize.height)
}
// Wider than tall: imageSize.width > imageSize.height
// Wider than tall
let factor = viewSize.width / imageSize.width
let height = imageSize.height * factor
let originY = floor((viewSize.height - height) / 2.0)
+26 -2
View File
@@ -43,7 +43,8 @@
@property (nonatomic) RSParsedAuthor *rootAuthor;
@property (nonatomic, readonly) NSDate *currentDate;
@property (nonatomic) NSString *language;
@property (nonatomic) BOOL isDaringFireball; // Special case — sometimes permalink and external link are swapped.
@property (nonatomic) NSString *iconURLString;
@property (nonatomic) BOOL isDaringFireball; // Special case— sometimes permalink and external link are swapped.
@end
@@ -85,7 +86,7 @@
[self parse];
RSParsedFeed *parsedFeed = [[RSParsedFeed alloc] initWithURLString:self.urlString title:self.title homepageURLString:self.homepageURLString language:self.language articles:self.articles];
RSParsedFeed *parsedFeed = [[RSParsedFeed alloc] initWithURLString:self.urlString title:self.title homepageURLString:self.homepageURLString iconURL:self.iconURLString language:self.language articles:self.articles];
return parsedFeed;
}
@@ -210,6 +211,12 @@ static const NSInteger kEnclosureLength = 10;
static const char *kLength = "length";
static const NSInteger kLengthLength = 7;
static const char *kIcon = "icon";
static const NSInteger kIconLength = 5;
static const char *kLogo = "logo";
static const NSInteger kLogoLength = 5;
#pragma mark - Parsing
- (void)parse {
@@ -657,6 +664,23 @@ static NSString *httpURLPrefix = @"http://";
[self addFeedTitle];
}
else if (!self.parsingArticle && !self.parsingSource && RSSAXEqualTags(localName, kIcon, kIconLength)) {
NSString *urlString = [self currentString];
if (!RSParserStringIsEmpty(urlString)) {
self.iconURLString = urlString;
}
}
else if (!self.parsingArticle && !self.parsingSource && RSSAXEqualTags(localName, kLogo, kLogoLength)) {
// Use logo as fallback when icon isn’t specified
if (!self.iconURLString) {
NSString *urlString = [self currentString];
if (!RSParserStringIsEmpty(urlString)) {
self.iconURLString = urlString;
}
}
}
[self.attributesStack removeLastObject];
}
+2 -1
View File
@@ -12,11 +12,12 @@
@interface RSParsedFeed : NSObject
- (nonnull instancetype)initWithURLString:(NSString * _Nonnull)urlString title:(NSString * _Nullable)title homepageURLString:(NSString * _Nullable)homepageURLString language:(NSString * _Nullable)language articles:(NSArray <RSParsedArticle *>* _Nonnull)articles;
- (nonnull instancetype)initWithURLString:(NSString * _Nonnull)urlString title:(NSString * _Nullable)title homepageURLString:(NSString * _Nullable)homepageURLString iconURL:(NSString * _Nullable)iconURL language:(NSString * _Nullable)language articles:(NSArray <RSParsedArticle *>* _Nonnull)articles;
@property (nonatomic, readonly, nonnull) NSString *urlString;
@property (nonatomic, readonly, nullable) NSString *title;
@property (nonatomic, readonly, nullable) NSString *homepageURLString;
@property (nonatomic, readonly, nullable) NSString *iconURL;
@property (nonatomic, readonly, nullable) NSString *language;
@property (nonatomic, readonly, nonnull) NSSet <RSParsedArticle *>*articles;
+4 -3
View File
@@ -12,19 +12,20 @@
@implementation RSParsedFeed
- (instancetype)initWithURLString:(NSString *)urlString title:(NSString *)title homepageURLString:(NSString *)homepageURLString language:(NSString *)language articles:(NSSet *)articles {
- (instancetype)initWithURLString:(NSString *)urlString title:(NSString *)title homepageURLString:(NSString *)homepageURLString iconURL:(NSString *)iconURL language:(NSString *)language articles:(NSSet *)articles {
self = [super init];
if (!self) {
return nil;
}
_urlString = urlString;
_title = title;
_homepageURLString = homepageURLString;
_iconURL = iconURL;
_language = language;
_articles = articles;
return self;
}
+10 -1
View File
@@ -33,6 +33,7 @@
@property (nonatomic) BOOL parsingAuthor;
@property (nonatomic, readonly) RSParsedArticle *currentArticle;
@property (nonatomic) BOOL parsingChannelImage;
@property (nonatomic) NSString *channelImageURLString;
@property (nonatomic, readonly) NSDate *currentDate;
@property (nonatomic) BOOL endRSSFound;
@property (nonatomic) NSString *homepageURLString;
@@ -77,7 +78,7 @@
[self parse];
RSParsedFeed *parsedFeed = [[RSParsedFeed alloc] initWithURLString:self.urlString title:self.title homepageURLString:self.homepageURLString language:self.language articles:self.articles];
RSParsedFeed *parsedFeed = [[RSParsedFeed alloc] initWithURLString:self.urlString title:self.title homepageURLString:self.homepageURLString iconURL:self.channelImageURLString language:self.language articles:self.articles];
return parsedFeed;
}
@@ -452,6 +453,10 @@ static const NSInteger kLanguageLength = 9;
if (!self.parsingChannelImage) {
[self.parser beginStoringCharacters];
}
else if (!prefix && RSSAXEqualTags(localName, kURL, kURLLength)) {
// Channel image URL
[self.parser beginStoringCharacters];
}
}
@@ -469,6 +474,10 @@ static const NSInteger kLanguageLength = 9;
self.endRSSFound = YES;
}
else if (self.parsingChannelImage && !self.parsingArticle && RSSAXEqualTags(localName, kURL, kURLLength)) {
self.channelImageURLString = [self currentString];
}
else if (RSSAXEqualTags(localName, kImage, kImageLength)) {
self.parsingChannelImage = NO;
}
@@ -50,9 +50,14 @@ public struct RSSInJSONParser {
let feedDescription = channelObject["description"] as? String
let feedLanguage = channelObject["language"] as? String
var iconURL: String?
if let imageObject = channelObject["image"] as? JSONDictionary {
iconURL = imageObject["url"] as? String
}
let items = parseItems(itemsObject!, parserData.url)
return ParsedFeed(type: .rssInJSON, title: title, homePageURL: homePageURL, feedURL: feedURL, language: feedLanguage, feedDescription: feedDescription, nextURL: nil, iconURL: nil, faviconURL: nil, authors: nil, expired: false, hubs: nil, items: items)
return ParsedFeed(type: .rssInJSON, title: title, homePageURL: homePageURL, feedURL: feedURL, language: feedLanguage, feedDescription: feedDescription, nextURL: nil, iconURL: iconURL, faviconURL: nil, authors: nil, expired: false, hubs: nil, items: items)
} catch { throw error }
}
@@ -21,7 +21,7 @@ struct RSParsedFeedTransformer {
static func parsedFeed(_ rsParsedFeed: RSParsedFeed) -> ParsedFeed {
let items = parsedItems(rsParsedFeed.articles)
return ParsedFeed(type: .rss, title: rsParsedFeed.title, homePageURL: rsParsedFeed.homepageURLString, feedURL: rsParsedFeed.urlString, language: rsParsedFeed.language, feedDescription: nil, nextURL: nil, iconURL: nil, faviconURL: nil, authors: nil, expired: false, hubs: nil, items: items)
return ParsedFeed(type: .rss, title: rsParsedFeed.title, homePageURL: rsParsedFeed.homepageURLString, feedURL: rsParsedFeed.urlString, language: rsParsedFeed.language, feedDescription: nil, nextURL: nil, iconURL: rsParsedFeed.iconURL, faviconURL: nil, authors: nil, expired: false, hubs: nil, items: items)
}
}
@@ -186,6 +186,12 @@ final class AtomParserTests: XCTestCase {
}
}
func testFeedIconURL() {
let d = parserData("root-author", "atom", "https://fvsch.com/feed.xml")
let parsedFeed = try! FeedParser.parse(d)!
XCTAssertEqual(parsedFeed.iconURL, "https://fvsch.com/assets/images/icon.png?v=ql0r5y")
}
func testAuthorAtRoot() {
let d = parserData("root-author", "atom", "https://fvsch.com/feed.xml")
let parsedFeed = try! FeedParser.parse(d)!
@@ -191,6 +191,20 @@ final class RSSParserTests: XCTestCase {
}
}
func testFeedIconURL() {
let d = parserData("KatieFloyd", "rss", "http://katiefloyd.com/")
let parsedFeed = try! FeedParser.parse(d)!
XCTAssertEqual(parsedFeed.iconURL, "https://static.feedpress.it/logo/katiefloyd.png")
}
func testFeedIconURLNotSetByItemLevelImages() {
// aktuality.rss has <image> elements inside <item>s, not at the channel level.
// These should not be treated as the feed icon.
let d = parserData("aktuality", "rss", "https://www.aktuality.sk/")
let parsedFeed = try! FeedParser.parse(d)!
XCTAssertNil(parsedFeed.iconURL)
}
func testMedscapeExternalURLs() {
let d = parserData("medscape", "rss", "https://www.medscape.com/cx/rssfeeds/2674.xml")
let parsedFeed = try! FeedParser.parse(d)!
+53 -2
View File
@@ -60,9 +60,29 @@ extension Notification.Name {
return cachedImage
}
if let homePageURLString = feed.homePageURL, let homePageURL = URL(string: homePageURLString), homePageURL.host == "nnw.ranchero.com" || homePageURL.host == "netnewswire.blog" {
if let homePageURLString = feed.homePageURL, let homePageURL = URL(string: homePageURLString) {
if homePageURL.host == "nnw.ranchero.com" || homePageURL.host == "netnewswire.blog" || homePageURL.host == "services.netnewswire.com" {
return IconImage.nnwFeedIcon
}
}
if feed.url.hasPrefix("https://ranchero.com/downloads/netnewswire") {
return IconImage.nnwFeedIcon
}
if let knownIconURL = Self.knownIconURL(for: feed) {
icon(forURL: knownIconURL, feed: feed) { image in
MainActor.assumeIsolated {
if self.cache[feed] != nil {
return
}
if let image {
self.cache[feed] = IconImage(image)
self.postFeedIconDidBecomeAvailableNotification(feed)
}
}
}
return nil
}
if Self.shouldSkipDownloadingFeedIcon(feed: feed) {
return nil
}
@@ -87,7 +107,7 @@ extension Notification.Name {
}
@MainActor func checkFeedIconURL() {
if let iconURL = feed.iconURL {
if let iconURL = feed.iconURL, !Self.shouldIgnoreFeedIconURL(feed) {
icon(forURL: iconURL, feed: feed) { (image) in
Task { @MainActor in
if self.cache[feed] != nil {
@@ -141,6 +161,17 @@ private extension FeedIconDownloader {
static let specialCasesToSkip = ["macsparky.com", "xkcd.com", SpecialCase.rachelByTheBayHostName, SpecialCase.openRSSOrgHostName]
// Domains where the feed-specified icon URL should be ignored,
// falling back to the homepage icon lookup instead.
static let domainsToIgnoreFeedIconURL: [String] = ["stratechery.com", "propublica.org", "substack.com"]
// Domains with a known-good icon URL to use instead of the
// feed-specified icon or homepage lookup.
static let domainToIconURL: [String: String] = [
"github.blog": "https://github.blog/wp-content/uploads/2019/01/cropped-github-favicon-512.png",
"github.com": "https://github.blog/wp-content/uploads/2019/01/cropped-github-favicon-512.png"
]
static func shouldSkipDownloadingFeedIcon(feed: Feed) -> Bool {
shouldSkipDownloadingFeedIcon(feed.url)
}
@@ -149,6 +180,26 @@ private extension FeedIconDownloader {
SpecialCase.urlStringContainSpecialCase(urlString, specialCasesToSkip)
}
static func shouldIgnoreFeedIconURL(_ feed: Feed) -> Bool {
guard !domainsToIgnoreFeedIconURL.isEmpty else {
return false
}
return SpecialCase.urlStringContainSpecialCase(feed.url, domainsToIgnoreFeedIconURL)
}
static func knownIconURL(for feed: Feed) -> String? {
guard !domainToIconURL.isEmpty else {
return nil
}
let lowerFeedURL = feed.url.lowercased(with: localeForLowercasing)
for (domain, iconURL) in domainToIconURL {
if lowerFeedURL.contains(domain) {
return iconURL
}
}
return nil
}
func icon(forHomePageURL homePageURL: String, feed: Feed, _ resultBlock: @escaping @MainActor (RSImage?, String?) -> Void) {
if Self.shouldSkipDownloadingFeedIcon(homePageURL) {
resultBlock(nil, nil)
+7 -8
View File
@@ -91,21 +91,20 @@ private extension IconView {
let imageSize = image.size
let viewSize = bounds.size
if imageSize.height == imageSize.width {
if imageSize.height >= viewSize.height * 0.75 {
// Close enough to viewSize to scale up the image.
return CGRect(x: 0.0, y: 0.0, width: viewSize.width, height: viewSize.height)
}
let offset = floor((viewSize.height - imageSize.height) / 2.0)
return CGRect(x: offset, y: offset, width: imageSize.width, height: imageSize.height)
} else if imageSize.height > imageSize.width {
return CGRect(x: 0.0, y: 0.0, width: viewSize.width, height: viewSize.height)
}
// Non-square: aspect fit — scale to fill one dimension, center in the other
if imageSize.height > imageSize.width {
let factor = viewSize.height / imageSize.height
let width = imageSize.width * factor
let originX = floor((viewSize.width - width) / 2.0)
return CGRect(x: originX, y: 0.0, width: width, height: viewSize.height)
}
// Wider than tall: imageSize.width > imageSize.height
// Wider than tall
let factor = viewSize.width / imageSize.width
let height = imageSize.height * factor
let originY = floor((viewSize.height - height) / 2.0)