2017-05-27 19:43:27 +02:00
|
|
|
//
|
2021-09-07 23:58:06 +02:00
|
|
|
// ArticleTheme.swift
|
2018-08-29 07:18:24 +02:00
|
|
|
// NetNewsWire
|
2017-05-27 19:43:27 +02:00
|
|
|
//
|
|
|
|
// Created by Brent Simmons on 9/26/15.
|
|
|
|
// Copyright © 2015 Ranchero Software, LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
2021-09-07 23:58:06 +02:00
|
|
|
struct ArticleTheme: Equatable {
|
2021-09-21 04:43:12 +02:00
|
|
|
|
2021-09-07 23:58:06 +02:00
|
|
|
static let defaultTheme = ArticleTheme()
|
2021-09-08 07:28:13 +02:00
|
|
|
static let nnwThemeSuffix = ".nnwtheme"
|
2021-09-21 04:43:12 +02:00
|
|
|
|
2021-09-08 07:28:13 +02:00
|
|
|
private static let defaultThemeName = NSLocalizedString("Default", comment: "Default")
|
|
|
|
private static let unknownValue = NSLocalizedString("Unknown", comment: "Unknown Value")
|
2021-09-21 04:43:12 +02:00
|
|
|
|
2017-05-27 19:43:27 +02:00
|
|
|
let path: String?
|
|
|
|
let template: String?
|
|
|
|
let css: String?
|
2022-02-08 01:23:08 +01:00
|
|
|
let isAppTheme: Bool
|
2021-09-08 07:28:13 +02:00
|
|
|
|
|
|
|
var name: String {
|
|
|
|
guard let path = path else { return Self.defaultThemeName }
|
|
|
|
return Self.themeNameForPath(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
var creatorHomePage: String {
|
2021-09-21 03:10:56 +02:00
|
|
|
return info?.creatorHomePage ?? Self.unknownValue
|
2021-09-08 07:28:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var creatorName: String {
|
2021-09-21 03:10:56 +02:00
|
|
|
return info?.creatorName ?? Self.unknownValue
|
2021-09-08 07:28:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var version: String {
|
2021-09-21 03:10:56 +02:00
|
|
|
return String(describing: info?.version ?? 0)
|
2021-09-08 07:28:13 +02:00
|
|
|
}
|
|
|
|
|
2021-09-21 03:10:56 +02:00
|
|
|
private let info: ArticleThemePlist?
|
2021-09-21 04:43:12 +02:00
|
|
|
|
2017-05-27 19:43:27 +02:00
|
|
|
init() {
|
|
|
|
self.path = nil;
|
2021-09-21 03:10:56 +02:00
|
|
|
self.info = ArticleThemePlist(name: "Article Theme", themeIdentifier: "com.ranchero.netnewswire.theme.article", creatorHomePage: "https://netnewswire.com/", creatorName: "Ranchero Software", version: 1)
|
2021-09-21 04:43:12 +02:00
|
|
|
|
2021-09-12 00:49:56 +02:00
|
|
|
let corePath = Bundle.main.path(forResource: "core", ofType: "css")!
|
|
|
|
let stylesheetPath = Bundle.main.path(forResource: "stylesheet", ofType: "css")!
|
|
|
|
css = Self.stringAtPath(corePath)! + "\n" + Self.stringAtPath(stylesheetPath)!
|
2021-09-21 04:43:12 +02:00
|
|
|
|
2017-05-27 19:43:27 +02:00
|
|
|
let templatePath = Bundle.main.path(forResource: "template", ofType: "html")!
|
2021-09-12 00:49:56 +02:00
|
|
|
template = Self.stringAtPath(templatePath)!
|
2022-02-08 01:23:08 +01:00
|
|
|
|
|
|
|
isAppTheme = true
|
2017-05-27 19:43:27 +02:00
|
|
|
}
|
2021-09-21 04:43:12 +02:00
|
|
|
|
2022-02-08 01:23:08 +01:00
|
|
|
init(path: String, isAppTheme: Bool) throws {
|
2017-05-27 19:43:27 +02:00
|
|
|
self.path = path
|
2021-09-21 04:43:12 +02:00
|
|
|
|
2021-09-12 00:49:56 +02:00
|
|
|
let infoPath = (path as NSString).appendingPathComponent("Info.plist")
|
2021-09-21 03:10:56 +02:00
|
|
|
let data = try Data(contentsOf: URL(fileURLWithPath: infoPath))
|
|
|
|
self.info = try PropertyListDecoder().decode(ArticleThemePlist.self, from: data)
|
|
|
|
|
2021-09-12 00:49:56 +02:00
|
|
|
let corePath = Bundle.main.path(forResource: "core", ofType: "css")!
|
|
|
|
let stylesheetPath = (path as NSString).appendingPathComponent("stylesheet.css")
|
|
|
|
if let stylesheetCSS = Self.stringAtPath(stylesheetPath) {
|
|
|
|
self.css = Self.stringAtPath(corePath)! + "\n" + stylesheetCSS
|
2021-09-08 06:50:12 +02:00
|
|
|
} else {
|
2021-09-12 00:49:56 +02:00
|
|
|
self.css = nil
|
2017-05-27 19:43:27 +02:00
|
|
|
}
|
2021-09-21 04:43:12 +02:00
|
|
|
|
2021-09-12 00:49:56 +02:00
|
|
|
let templatePath = (path as NSString).appendingPathComponent("template.html")
|
|
|
|
self.template = Self.stringAtPath(templatePath)
|
2022-02-08 01:23:08 +01:00
|
|
|
|
|
|
|
self.isAppTheme = isAppTheme
|
2017-05-27 19:43:27 +02:00
|
|
|
}
|
2021-09-08 07:28:13 +02:00
|
|
|
|
|
|
|
static func stringAtPath(_ f: String) -> String? {
|
|
|
|
if !FileManager.default.fileExists(atPath: f) {
|
|
|
|
return nil
|
|
|
|
}
|
2021-09-21 04:43:12 +02:00
|
|
|
|
2021-09-08 07:28:13 +02:00
|
|
|
if let s = try? NSString(contentsOfFile: f, usedEncoding: nil) as String {
|
|
|
|
return s
|
|
|
|
}
|
2017-05-27 19:43:27 +02:00
|
|
|
return nil
|
|
|
|
}
|
2021-09-21 04:43:12 +02:00
|
|
|
|
2021-09-08 07:28:13 +02:00
|
|
|
static func filenameWithThemeSuffixRemoved(_ filename: String) -> String {
|
|
|
|
return filename.stripping(suffix: Self.nnwThemeSuffix)
|
|
|
|
}
|
2021-09-21 04:43:12 +02:00
|
|
|
|
2021-09-08 07:28:13 +02:00
|
|
|
static func themeNameForPath(_ f: String) -> String {
|
|
|
|
let filename = (f as NSString).lastPathComponent
|
|
|
|
return filenameWithThemeSuffixRemoved(filename)
|
|
|
|
}
|
2021-09-21 04:43:12 +02:00
|
|
|
|
2021-09-08 07:28:13 +02:00
|
|
|
static func pathIsPathForThemeName(_ themeName: String, path: String) -> Bool {
|
|
|
|
let filename = (path as NSString).lastPathComponent
|
|
|
|
return filenameWithThemeSuffixRemoved(filename) == themeName
|
2017-05-27 19:43:27 +02:00
|
|
|
}
|
2021-09-08 07:28:13 +02:00
|
|
|
|
2017-05-27 19:43:27 +02:00
|
|
|
}
|