From e22c17fd6a1fa6bce3fbc2abeee2dfec73624bf3 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Sat, 14 Sep 2024 14:45:51 -0700 Subject: [PATCH] Continue progress on AtomParser. --- .../FeedParser/Feeds/XML/AtomParser.swift | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/Modules/Parser/Sources/FeedParser/Feeds/XML/AtomParser.swift b/Modules/Parser/Sources/FeedParser/Feeds/XML/AtomParser.swift index a0aa59da8..9c69d24bc 100644 --- a/Modules/Parser/Sources/FeedParser/Feeds/XML/AtomParser.swift +++ b/Modules/Parser/Sources/FeedParser/Feeds/XML/AtomParser.swift @@ -86,7 +86,12 @@ private extension AtomParser { private struct XMLString { static let rel = "rel" static let alternate = "alternate" + static let related = "related" + static let enclosure = "enclosure" static let href = "href" + static let title = "title" + static let type = "type" + static let length = "length" static let xmlLang = "xml:lang" } @@ -202,6 +207,46 @@ private extension AtomParser { func addLink(_ article: RSSArticle) { + guard let attributes = currentAttributes else { + return + } + guard let urlString = attributes[XMLString.href], !urlString.isEmpty else { + return + } + + var rel = attributes[XMLString.rel] + if rel?.isEmpty ?? true { + rel = XMLString.alternate + } + + if rel == XMLString.related { + if article.link == nil { + article.link = urlString + } + } + else if rel == XMLString.alternate { + if article.permalink == nil { + article.permalink = urlString + } + } + else if rel == XMLString.enclosure { + if let enclosure = enclosure(urlString, attributes) { + article.addEnclosure(enclosure) + } + } + } + + func enclosure(_ urlString: String, _ attributes: SAXParser.XMLAttributesDictionary) -> RSSEnclosure? { + + let enclosure = RSSEnclosure(url: urlString) + enclosure.title = attributes[XMLString.title] + enclosure.mimeType = attributes[XMLString.type] + + if let lengthString = attributes[XMLString.length] { + enclosure.length = Int(lengthString) + } + + return enclosure } func addXHTMLTag(_ localName: XMLPointer) {