2019-10-15 03:45:58 +02:00
|
|
|
//
|
|
|
|
// NNW3Document.swift
|
|
|
|
// NetNewsWire
|
|
|
|
//
|
|
|
|
// Created by Maurice Parker on 10/14/19.
|
|
|
|
// Copyright © 2019 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
2019-10-17 22:25:11 +02:00
|
|
|
import RSCore
|
2019-10-15 03:45:58 +02:00
|
|
|
|
2019-10-17 22:25:11 +02:00
|
|
|
struct NNW3Document {
|
|
|
|
|
|
|
|
private let children: [OPMLRepresentable]?
|
|
|
|
|
|
|
|
private init(plist: [[String: AnyObject]]) {
|
|
|
|
self.children = NNW3Folder.itemsWithPlist(plist: plist)
|
|
|
|
}
|
|
|
|
|
|
|
|
init?(subscriptionsPlistURL url: URL) {
|
|
|
|
guard let data = try? Data(contentsOf: url) else {
|
|
|
|
return nil
|
2019-10-15 03:45:58 +02:00
|
|
|
}
|
2019-10-17 22:25:11 +02:00
|
|
|
guard let plist = try? PropertyListSerialization.propertyList(from: data, options: [], format: nil) as? [[String: AnyObject]] else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
self.init(plist: plist)
|
2019-10-15 03:45:58 +02:00
|
|
|
}
|
2019-10-17 22:25:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: OPMLRepresentable
|
|
|
|
|
|
|
|
extension NNW3Document: OPMLRepresentable {
|
|
|
|
|
|
|
|
func OPMLString(indentLevel: Int, strictConformance: Bool) -> String {
|
2019-10-15 03:45:58 +02:00
|
|
|
var s =
|
|
|
|
"""
|
|
|
|
<?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
<opml version="1.1">
|
|
|
|
<head>
|
2019-10-17 22:25:11 +02:00
|
|
|
<title>NetNewsWire 3 Subscriptions</title>
|
2019-10-15 03:45:58 +02:00
|
|
|
</head>
|
|
|
|
<body>
|
2019-10-17 22:25:11 +02:00
|
|
|
|
2019-10-15 03:45:58 +02:00
|
|
|
"""
|
2019-10-17 22:25:11 +02:00
|
|
|
|
|
|
|
if let children = children {
|
|
|
|
for child in children {
|
|
|
|
s += child.OPMLString(indentLevel: indentLevel + 1, strictConformance: true)
|
|
|
|
}
|
2019-10-15 03:45:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
s +=
|
|
|
|
"""
|
2019-10-17 22:25:11 +02:00
|
|
|
</body>
|
|
|
|
</opml>
|
|
|
|
"""
|
|
|
|
|
2019-10-15 03:45:58 +02:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
}
|
2019-10-17 22:25:11 +02:00
|
|
|
|
|
|
|
// MARK: - NNW3Folder
|
|
|
|
|
|
|
|
private struct NNW3Folder {
|
|
|
|
|
|
|
|
private let title: String?
|
|
|
|
private let children: [OPMLRepresentable]?
|
|
|
|
|
|
|
|
init(plist: [String: Any]) {
|
|
|
|
self.title = plist["name"] as? String
|
|
|
|
guard let childrenArray = plist["childrenArray"] as? [[String: Any]] else {
|
|
|
|
self.children = nil
|
|
|
|
return
|
|
|
|
}
|
|
|
|
self.children = NNW3Folder.itemsWithPlist(plist: childrenArray)
|
|
|
|
}
|
|
|
|
|
|
|
|
static func itemsWithPlist(plist: [[String: Any]]) -> [OPMLRepresentable]? {
|
|
|
|
// Also used by NNW3Document.
|
|
|
|
var items = [OPMLRepresentable]()
|
|
|
|
for child in plist {
|
|
|
|
if child["isContainer"] as? Bool ?? false {
|
|
|
|
items.append(NNW3Folder(plist: child))
|
|
|
|
} else {
|
|
|
|
items.append(NNW3Feed(plist: child))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return items.isEmpty ? nil : items
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: OPMLRepresentable
|
|
|
|
|
|
|
|
extension NNW3Folder: OPMLRepresentable {
|
|
|
|
|
|
|
|
func OPMLString(indentLevel: Int, strictConformance: Bool) -> String {
|
|
|
|
let t = title?.rs_stringByEscapingSpecialXMLCharacters() ?? ""
|
|
|
|
guard let children = children else {
|
|
|
|
// Empty folder.
|
|
|
|
return "<outline text=\"\(t)\" title=\"\(t)\" />\n".rs_string(byPrependingNumberOfTabs: indentLevel)
|
|
|
|
}
|
|
|
|
|
|
|
|
var s = "<outline text=\"\(t)\" title=\"\(t)\">\n".rs_string(byPrependingNumberOfTabs: indentLevel)
|
|
|
|
for child in children {
|
|
|
|
s += child.OPMLString(indentLevel: indentLevel + 1, strictConformance: true)
|
|
|
|
}
|
|
|
|
|
|
|
|
s += "</outline>\n".rs_string(byPrependingNumberOfTabs: indentLevel)
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - NNW3Feed
|
|
|
|
|
|
|
|
private struct NNW3Feed {
|
|
|
|
|
|
|
|
private let title: String?
|
|
|
|
private let homePageURL: String?
|
|
|
|
private let feedURL: String?
|
|
|
|
|
|
|
|
init(plist: [String: Any]) {
|
|
|
|
self.title = plist["name"] as? String
|
|
|
|
self.homePageURL = plist["home"] as? String
|
|
|
|
self.feedURL = plist["rss"] as? String
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: OPMLRepresentable
|
|
|
|
|
|
|
|
extension NNW3Feed: OPMLRepresentable {
|
|
|
|
|
|
|
|
func OPMLString(indentLevel: Int, strictConformance: Bool) -> String {
|
|
|
|
let t = title?.rs_stringByEscapingSpecialXMLCharacters() ?? ""
|
|
|
|
let p = homePageURL?.rs_stringByEscapingSpecialXMLCharacters() ?? ""
|
|
|
|
let f = feedURL?.rs_stringByEscapingSpecialXMLCharacters() ?? ""
|
|
|
|
|
|
|
|
var s = "<outline text=\"\(t)\" title=\"\(t)\" description=\"\" type=\"rss\" version=\"RSS\" htmlUrl=\"\(p)\" xmlUrl=\"\(f)\"/>\n"
|
|
|
|
s = s.rs_string(byPrependingNumberOfTabs: indentLevel)
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|