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 {
|
|
|
|
|
2020-01-14 05:20:20 +01:00
|
|
|
func OPMLString(indentLevel: Int, allowCustomAttributes: 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 {
|
2020-01-14 05:20:20 +01:00
|
|
|
s += child.OPMLString(indentLevel: indentLevel + 1)
|
2019-10-17 22:25:11 +02:00
|
|
|
}
|
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 {
|
|
|
|
|
2020-01-14 05:20:20 +01:00
|
|
|
func OPMLString(indentLevel: Int, allowCustomAttributes: Bool) -> String {
|
2020-01-17 03:09:18 +01:00
|
|
|
let t = title?.escapingSpecialXMLCharacters ?? ""
|
2019-10-17 22:25:11 +02:00
|
|
|
guard let children = children else {
|
|
|
|
// Empty folder.
|
2020-01-17 03:09:18 +01:00
|
|
|
return "<outline text=\"\(t)\" title=\"\(t)\" />\n".prepending(tabCount: indentLevel)
|
2019-10-17 22:25:11 +02:00
|
|
|
}
|
|
|
|
|
2020-01-17 03:09:18 +01:00
|
|
|
var s = "<outline text=\"\(t)\" title=\"\(t)\">\n".prepending(tabCount: indentLevel)
|
2019-10-17 22:25:11 +02:00
|
|
|
for child in children {
|
2020-01-14 05:20:20 +01:00
|
|
|
s += child.OPMLString(indentLevel: indentLevel + 1)
|
2019-10-17 22:25:11 +02:00
|
|
|
}
|
|
|
|
|
2020-01-17 03:09:18 +01:00
|
|
|
s += "</outline>\n".prepending(tabCount: indentLevel)
|
2019-10-17 22:25:11 +02:00
|
|
|
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 {
|
|
|
|
|
2020-01-14 05:20:20 +01:00
|
|
|
func OPMLString(indentLevel: Int, allowCustomAttributes: Bool) -> String {
|
2020-01-17 03:09:18 +01:00
|
|
|
let t = title?.escapingSpecialXMLCharacters ?? ""
|
|
|
|
let p = homePageURL?.escapingSpecialXMLCharacters ?? ""
|
|
|
|
let f = feedURL?.escapingSpecialXMLCharacters ?? ""
|
2019-10-17 22:25:11 +02:00
|
|
|
|
|
|
|
var s = "<outline text=\"\(t)\" title=\"\(t)\" description=\"\" type=\"rss\" version=\"RSS\" htmlUrl=\"\(p)\" xmlUrl=\"\(f)\"/>\n"
|
2020-01-17 03:09:18 +01:00
|
|
|
s = s.prepending(tabCount: indentLevel)
|
2019-10-17 22:25:11 +02:00
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|