Use feed icon’s gathered from HTML metadata.

This commit is contained in:
Brent Simmons 2017-12-29 20:41:41 -08:00
parent d2d7369d6a
commit 5dd20264bf
1 changed files with 38 additions and 2 deletions

View File

@ -20,6 +20,7 @@ class ArticleRenderer {
let article: Article let article: Article
let articleStyle: ArticleStyle let articleStyle: ArticleStyle
static var faviconImgTagCache = [Feed: String]() static var faviconImgTagCache = [Feed: String]()
static var feedIconImgTagCache = [Feed: String]()
lazy var longDateFormatter: DateFormatter = { lazy var longDateFormatter: DateFormatter = {
let dateFormatter = DateFormatter() let dateFormatter = DateFormatter()
@ -171,8 +172,7 @@ class ArticleRenderer {
d["avatars"] = "" d["avatars"] = ""
var didAddAvatar = false var didAddAvatar = false
if let avatar = avatarToUse() { if let avatarHTML = avatarImgTag() {
let avatarHTML = avatar.html(dimension: 64)
d["avatars"] = avatarHTML d["avatars"] = avatarHTML
didAddAvatar = true didAddAvatar = true
} }
@ -245,6 +245,23 @@ class ArticleRenderer {
return nil return nil
} }
private func feedIconImgTag(forFeed feed: Feed) -> String? {
if let cachedImgTag = ArticleRenderer.feedIconImgTagCache[feed] {
return cachedImgTag
}
if let icon = appDelegate.feedIconDownloader.icon(for: feed) {
if let s = base64String(forImage: icon) {
let imgTag = "<img src=\"data:image/tiff;base64, " + s + "\" height=48 width=48 />"
ArticleRenderer.feedIconImgTagCache[feed] = imgTag
return imgTag
}
}
return nil
}
private func base64String(forImage image: NSImage) -> String? { private func base64String(forImage image: NSImage) -> String? {
@ -318,6 +335,25 @@ class ArticleRenderer {
return nil return nil
} }
private let avatarDimension = 48
private func avatarImgTag() -> String? {
if let author = singleArticleSpecifiedAuthor(), let imageURL = author.avatarURL {
return Avatar(imageURL: imageURL, url: author.url).html(dimension: avatarDimension)
}
if let feed = article.feed, let imgTag = feedIconImgTag(forFeed: feed) {
return imgTag
}
if let feedIconURL = article.feed?.iconURL {
return Avatar(imageURL: feedIconURL, url: article.feed?.homePageURL ?? article.feed?.url).html(dimension: avatarDimension)
}
if let author = singleFeedSpecifiedAuthor(), let imageURL = author.avatarURL {
return Avatar(imageURL: imageURL, url: author.url).html(dimension: avatarDimension)
}
return nil
}
// private func authorAvatar() -> String { // private func authorAvatar() -> String {
// //
// guard let authors = article.authors, authors.count == 1, let author = authors.first else { // guard let authors = article.authors, authors.count == 1, let author = authors.first else {