NetNewsWire/Shared/ArticleStyles/ArticleTheme.swift

81 lines
2.0 KiB
Swift
Raw Normal View History

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 {
2017-05-27 19:43:27 +02:00
2021-09-07 23:58:06 +02:00
static let defaultTheme = ArticleTheme()
2017-05-27 19:43:27 +02:00
let path: String?
let template: String?
let css: String?
2021-09-07 23:58:06 +02:00
let systemMessageCSS: String?
2017-05-27 19:43:27 +02:00
let info: NSDictionary?
init() {
self.path = nil;
2021-09-07 23:58:06 +02:00
self.systemMessageCSS = nil
2017-05-27 19:43:27 +02:00
self.info = ["CreatorHomePage": "https://netnewswire.com/", "CreatorName": "Ranchero Software", "Version": "1.0"]
2017-05-27 19:43:27 +02:00
2020-04-20 01:38:33 +02:00
let sharedCSSPath = Bundle.main.path(forResource: "shared", ofType: "css")!
let platformCSSPath = Bundle.main.path(forResource: "styleSheet", ofType: "css")!
if let sharedCSS = stringAtPath(sharedCSSPath), let platformCSS = stringAtPath(platformCSSPath) {
css = sharedCSS + "\n" + platformCSS
} else {
css = nil
}
2017-05-27 19:43:27 +02:00
let templatePath = Bundle.main.path(forResource: "template", ofType: "html")!
template = stringAtPath(templatePath)
}
init(path: String) {
self.path = path
2020-01-12 00:19:32 +01:00
let isFolder = FileManager.default.isFolder(atPath: path)
2017-05-27 19:43:27 +02:00
if isFolder {
let infoPath = (path as NSString).appendingPathComponent("Info.plist")
self.info = NSDictionary(contentsOfFile: infoPath)
let cssPath = (path as NSString).appendingPathComponent("stylesheet.css")
self.css = stringAtPath(cssPath)
2021-09-07 23:58:06 +02:00
let systemMessageCSSPath = (path as NSString).appendingPathComponent("system_message_stylesheet.css")
self.systemMessageCSS = stringAtPath(systemMessageCSSPath)
2017-05-27 19:43:27 +02:00
let templatePath = (path as NSString).appendingPathComponent("template.html")
self.template = stringAtPath(templatePath)
}
else {
self.css = stringAtPath(path)
self.template = nil
2021-09-07 23:58:06 +02:00
self.systemMessageCSS = nil
2017-05-27 19:43:27 +02:00
self.info = nil
}
}
}
private func stringAtPath(_ f: String) -> String? {
if !FileManager.default.fileExists(atPath: f) {
return nil
}
if let s = try? NSString(contentsOfFile: f, usedEncoding: nil) as String {
return s
}
return nil
}