From 891416e7b70bf98992891eea62087b1fd99512f3 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Fri, 16 Feb 2018 13:13:00 -0800 Subject: [PATCH] Add a special case to the JSON Feed parser for feeds that include HTML entities in their titles. At the moment this is used for kottke.org and pxlnv.com. More could be added later, and these feeds could be removed if fixed. --- .../RSParser/Feeds/JSON/JSONFeedParser.swift | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/Frameworks/RSParser/Feeds/JSON/JSONFeedParser.swift b/Frameworks/RSParser/Feeds/JSON/JSONFeedParser.swift index c77c9dc58..c646c3ffe 100644 --- a/Frameworks/RSParser/Feeds/JSON/JSONFeedParser.swift +++ b/Frameworks/RSParser/Feeds/JSON/JSONFeedParser.swift @@ -134,7 +134,7 @@ private extension JSONFeedParser { let url = itemDictionary[Key.url] as? String let externalURL = itemDictionary[Key.externalURL] as? String - let title = itemDictionary[Key.title] as? String + let title = parseTitle(itemDictionary, feedURL) let summary = itemDictionary[Key.summary] as? String let imageURL = itemDictionary[Key.image] as? String let bannerImageURL = itemDictionary[Key.bannerImage] as? String @@ -152,6 +152,34 @@ private extension JSONFeedParser { return ParsedItem(syncServiceID: nil, uniqueID: uniqueID, feedURL: feedURL, url: url, externalURL: externalURL, title: title, contentHTML: decodedContentHTML, contentText: contentText, summary: summary, imageURL: imageURL, bannerImageURL: bannerImageURL, datePublished: datePublished, dateModified: dateModified, authors: authors, tags: tags, attachments: attachments) } + static func parseTitle(_ itemDictionary: JSONDictionary, _ feedURL: String) -> String? { + + guard let title = itemDictionary[Key.title] as? String else { + return nil + } + + if isSpecialCaseTitleWithEntitiesFeed(feedURL) { + return (title as NSString).rsparser_stringByDecodingHTMLEntities() + } + + return title + } + + static func isSpecialCaseTitleWithEntitiesFeed(_ feedURL: String) -> Bool { + + // As of 16 Feb. 2018, Kottke’s and Heer’s feeds includes HTML entities in the title elements. + // If we find more feeds like this, we’ll add them here. If these feeds get fixed, we’ll remove them. + + let matchStrings = ["kottke.org", "pxlnv.com"] + for matchString in matchStrings { + if feedURL.contains(matchString) { + return true + } + } + + return false + } + static func parseUniqueID(_ itemDictionary: JSONDictionary) -> String? { if let uniqueID = itemDictionary[Key.uniqueID] as? String {