NetNewsWire/Shared/Article Rendering/ArticleRenderer.swift

320 lines
8.9 KiB
Swift
Raw Normal View History

2017-05-27 19:43:27 +02:00
//
// ArticleRenderer.swift
2018-08-29 07:18:24 +02:00
// NetNewsWire
2017-05-27 19:43:27 +02:00
//
// Created by Brent Simmons on 9/8/15.
// Copyright © 2015 Ranchero Software, LLC. All rights reserved.
//
import Foundation
#if os(iOS)
import UIKit
#endif
2017-05-27 19:43:27 +02:00
import RSCore
import Articles
import Account
2017-05-27 19:43:27 +02:00
2019-02-11 07:06:03 +01:00
struct ArticleRenderer {
typealias Rendering = (style: String, html: String, title: String, baseURL: String)
struct Page {
let url: URL
let baseURL: URL
let html: String
init(name: String) {
url = Bundle.main.url(forResource: name, withExtension: "html")!
baseURL = url.deletingLastPathComponent()
html = try! NSString(contentsOfFile: url.path, encoding: String.Encoding.utf8.rawValue) as String
}
}
static var imageIconScheme = "nnwImageIcon"
static var blank = Page(name: "blank")
static var page = Page(name: "page")
private let article: Article?
2019-09-19 01:15:55 +02:00
private let extractedArticle: ExtractedArticle?
2021-09-07 23:58:06 +02:00
private let articleTheme: ArticleTheme
private let title: String
2019-09-19 01:15:55 +02:00
private let body: String
private let baseURL: String?
2021-09-07 23:58:06 +02:00
private init(article: Article?, extractedArticle: ExtractedArticle?, theme: ArticleTheme) {
2017-05-27 19:43:27 +02:00
self.article = article
2019-09-19 01:15:55 +02:00
self.extractedArticle = extractedArticle
2021-09-07 23:58:06 +02:00
self.articleTheme = theme
self.title = article?.sanitizedTitle() ?? ""
2019-09-19 01:15:55 +02:00
if let content = extractedArticle?.content {
self.body = content
2019-09-20 00:41:56 +02:00
self.baseURL = extractedArticle?.url
2019-09-19 01:15:55 +02:00
} else {
self.body = article?.body ?? ""
2019-09-20 00:41:56 +02:00
self.baseURL = article?.baseURL?.absoluteString
2019-09-19 01:15:55 +02:00
}
2017-05-27 19:43:27 +02:00
}
2019-02-11 07:06:03 +01:00
// MARK: - API
2021-09-07 23:58:06 +02:00
static func articleHTML(article: Article, extractedArticle: ExtractedArticle? = nil, theme: ArticleTheme) -> Rendering {
let renderer = ArticleRenderer(article: article, extractedArticle: extractedArticle, theme: theme)
return (renderer.articleCSS, renderer.articleHTML, renderer.title, renderer.baseURL ?? "")
2019-02-11 07:06:03 +01:00
}
2021-09-07 23:58:06 +02:00
static func multipleSelectionHTML(theme: ArticleTheme) -> Rendering {
let renderer = ArticleRenderer(article: nil, extractedArticle: nil, theme: theme)
return (renderer.articleCSS, renderer.multipleSelectionHTML, renderer.title, renderer.baseURL ?? "")
2019-02-11 07:06:03 +01:00
}
2021-09-07 23:58:06 +02:00
static func loadingHTML(theme: ArticleTheme) -> Rendering {
let renderer = ArticleRenderer(article: nil, extractedArticle: nil, theme: theme)
return (renderer.articleCSS, renderer.loadingHTML, renderer.title, renderer.baseURL ?? "")
2019-09-21 22:03:42 +02:00
}
2021-09-07 23:58:06 +02:00
static func noSelectionHTML(theme: ArticleTheme) -> Rendering {
let renderer = ArticleRenderer(article: nil, extractedArticle: nil, theme: theme)
return (renderer.articleCSS, renderer.noSelectionHTML, renderer.title, renderer.baseURL ?? "")
}
2021-09-07 23:58:06 +02:00
static func noContentHTML(theme: ArticleTheme) -> Rendering {
let renderer = ArticleRenderer(article: nil, extractedArticle: nil, theme: theme)
return (renderer.articleCSS, renderer.noContentHTML, renderer.title, renderer.baseURL ?? "")
}
}
2017-05-27 19:43:27 +02:00
2019-02-11 07:06:03 +01:00
// MARK: - Private
2017-05-27 19:43:27 +02:00
private extension ArticleRenderer {
2017-05-27 19:43:27 +02:00
2019-02-11 07:06:03 +01:00
private var articleHTML: String {
return try! MacroProcessor.renderedText(withTemplate: template(), substitutions: articleSubstitutions())
2019-02-11 07:06:03 +01:00
}
private var multipleSelectionHTML: String {
let body = "<h3 class='systemMessage'>Multiple selection</h3>"
return body
2019-02-11 07:06:03 +01:00
}
2019-09-21 22:03:42 +02:00
private var loadingHTML: String {
let body = "<h3 class='systemMessage'>Loading...</h3>"
return body
2019-09-21 22:03:42 +02:00
}
2019-02-11 07:06:03 +01:00
private var noSelectionHTML: String {
let body = "<h3 class='systemMessage'>No selection</h3>"
return body
2019-02-11 07:06:03 +01:00
}
private var noContentHTML: String {
return ""
}
private var articleCSS: String {
return try! MacroProcessor.renderedText(withTemplate: styleString(), substitutions: styleSubstitutions())
}
static var defaultStyleSheet: String = {
let path = Bundle.main.path(forResource: "styleSheet", ofType: "css")!
let s = try! NSString(contentsOfFile: path, encoding: String.Encoding.utf8.rawValue)
return "\n\(s)\n"
}()
2017-05-27 19:43:27 +02:00
static let defaultTemplate: String = {
let path = Bundle.main.path(forResource: "template", ofType: "html")!
let s = try! NSString(contentsOfFile: path, encoding: String.Encoding.utf8.rawValue)
return s as String
}()
func styleString() -> String {
2021-09-07 23:58:06 +02:00
return articleTheme.css ?? ArticleRenderer.defaultStyleSheet
2017-05-27 19:43:27 +02:00
}
func template() -> String {
2021-09-07 23:58:06 +02:00
return articleTheme.template ?? ArticleRenderer.defaultTemplate
2017-12-29 20:31:47 +01:00
}
func titleOrTitleLink() -> String {
if let link = article?.preferredLink {
return title.htmlByAddingLink(link)
2017-05-27 19:43:27 +02:00
}
return title
2017-05-27 19:43:27 +02:00
}
2019-09-19 01:15:55 +02:00
func articleSubstitutions() -> [String: String] {
2017-05-27 19:43:27 +02:00
var d = [String: String]()
guard let article = article else {
assertionFailure("Article should have been set before calling this function.")
return d
}
2017-05-27 19:43:27 +02:00
let title = titleOrTitleLink()
d["title"] = title
if let externalLink = article.externalURL, externalLink != article.preferredLink {
let displayLink = externalLink.strippingHTTPOrHTTPSScheme
let regarding = NSLocalizedString("Link", comment: "Link")
let externalLinkString = "\(regarding): <a href=\"\(externalLink)\">\(displayLink)</a>"
d["external_link"] = externalLinkString
} else {
d["external_link"] = ""
}
d["body"] = body
#if os(macOS)
d["text_size_class"] = AppDefaults.shared.articleTextSize.cssClass
#endif
var components = URLComponents()
components.scheme = Self.imageIconScheme
components.path = article.articleID
if let imageIconURLString = components.string {
d["avatar_src"] = imageIconURLString
}
else {
d["avatars"] = ""
}
if self.title.isEmpty {
d["dateline_style"] = "articleDatelineTitle"
} else {
d["dateline_style"] = "articleDateline"
}
2017-05-27 19:43:27 +02:00
var feedLink = ""
if let feedTitle = article.webFeed?.nameForDisplay {
2017-05-27 19:43:27 +02:00
feedLink = feedTitle
if let feedURL = article.webFeed?.homePageURL {
feedLink = feedLink.htmlByAddingLink(feedURL, className: "feedLink")
2017-05-27 19:43:27 +02:00
}
}
d["feedlink"] = feedLink
let datePublished = article.logicalDatePublished
let longDate = dateString(datePublished, .long, .medium)
let mediumDate = dateString(datePublished, .medium, .short)
let shortDate = dateString(datePublished, .short, .short)
if let permalink = article.url {
d["date_long"] = longDate.htmlByAddingLink(permalink)
d["date_medium"] = mediumDate.htmlByAddingLink(permalink)
d["date_short"] = shortDate.htmlByAddingLink(permalink)
}
else {
d["date_long"] = longDate
d["date_medium"] = mediumDate
d["date_short"] = shortDate
}
2017-12-29 20:31:47 +01:00
d["byline"] = byline()
2017-05-27 19:43:27 +02:00
return d
}
func byline() -> String {
guard let authors = article?.authors ?? article?.webFeed?.authors, !authors.isEmpty else {
2017-12-29 20:31:47 +01:00
return ""
}
// If the author's name is the same as the feed, then we don't want to display it.
// This code assumes that multiple authors would never match the feed name so that
// if there feed owner has an article co-author all authors are given the byline.
if authors.count == 1, let author = authors.first {
if author.name == article?.webFeed?.nameForDisplay {
return ""
}
}
var byline = ""
2017-12-29 20:31:47 +01:00
var isFirstAuthor = true
for author in authors {
if !isFirstAuthor {
byline += ", "
}
isFirstAuthor = false
var authorEmailAddress: String? = nil
if let emailAddress = author.emailAddress, !(emailAddress.contains("noreply@") || emailAddress.contains("no-reply@")) {
authorEmailAddress = emailAddress
}
if let emailAddress = authorEmailAddress, emailAddress.contains(" ") {
2017-12-29 20:31:47 +01:00
byline += emailAddress // probably name plus email address
}
else if let name = author.name, let url = author.url {
byline += name.htmlByAddingLink(url)
2017-12-29 20:31:47 +01:00
}
else if let name = author.name, let emailAddress = authorEmailAddress {
2020-04-15 23:39:58 +02:00
byline += "\(name) &lt;\(emailAddress)&gt;"
2017-12-29 20:31:47 +01:00
}
else if let name = author.name {
byline += name
}
else if let emailAddress = authorEmailAddress {
2017-12-29 20:31:47 +01:00
byline += "&lt;\(emailAddress)&gt;" // TODO: mailto link
}
else if let url = author.url {
byline += String.htmlWithLink(url)
2017-12-29 20:31:47 +01:00
}
}
return byline
2017-12-29 20:31:47 +01:00
}
func dateString(_ date: Date, _ dateStyle: DateFormatter.Style, _ timeStyle: DateFormatter.Style) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = dateStyle
dateFormatter.timeStyle = timeStyle
return dateFormatter.string(from: date)
}
#if os(iOS)
func styleSubstitutions() -> [String: String] {
var d = [String: String]()
let bodyFont = UIFont.preferredFont(forTextStyle: .body)
d["font-size"] = String(describing: bodyFont.pointSize)
return d
}
#else
func styleSubstitutions() -> [String: String] {
return [String: String]()
}
#endif
2017-05-27 19:43:27 +02:00
}
// MARK: - Article extension
private extension Article {
var baseURL: URL? {
var s = url
if s == nil {
s = webFeed?.homePageURL
}
if s == nil {
s = webFeed?.url
}
guard let urlString = s else {
return nil
}
var urlComponents = URLComponents(string: urlString)
if urlComponents == nil {
return nil
}
// Cant use url-with-fragment as base URL. The webview wont load. See scripting.com/rss.xml for example.
urlComponents!.fragment = nil
guard let url = urlComponents!.url, url.scheme == "http" || url.scheme == "https" else {
return nil
}
return url
}
}