diff --git a/Modules/Parser/Sources/FeedParser/Feeds/XML/AtomParser.swift b/Modules/Parser/Sources/FeedParser/Feeds/XML/AtomParser.swift index edc154c8d..fe32232c8 100644 --- a/Modules/Parser/Sources/FeedParser/Feeds/XML/AtomParser.swift +++ b/Modules/Parser/Sources/FeedParser/Feeds/XML/AtomParser.swift @@ -72,6 +72,14 @@ private extension AtomParser { static let feed = "feed".utf8CString static let source = "source".utf8CString static let author = "author".utf8CString + static let name = "name".utf8CString + static let email = "email".utf8CString + static let uri = "uri".utf8CString + static let title = "title".utf8CString + } + + func addFeedTitle() { + } func addFeedLink() { @@ -87,6 +95,10 @@ private extension AtomParser { articles.append(article) } + func addArticleElement(_ localName: XMLPointer, _ prefix: XMLPointer?) { + + } + func addXHTMLTag(_ localName: XMLPointer) { guard var xhtmlString else { @@ -182,6 +194,85 @@ extension AtomParser: SAXParserDelegate { public func saxParser(_ saxParser: SAXParser, xmlEndElement localName: XMLPointer, prefix: XMLPointer?, uri: XMLPointer?) { + if SAXEqualTags(localName, XMLName.feed) { + endFeedFound = true + return + } + + if endFeedFound { + return + } + + if parsingXHTML { + + let isContentTag = SAXEqualTags(localName, XMLName.content) + let isSummaryTag = SAXEqualTags(localName, XMLName.summary) + + if parsingArticle && (isContentTag || isSummaryTag) { + + if isContentTag { + currentArticle?.body = xhtmlString + } + + else if isSummaryTag { + if (currentArticle?.body?.count ?? 0) < 1 { + currentArticle?.body = xhtmlString + } + } + } + + if isContentTag || isSummaryTag { + parsingXHTML = false + } + + if var xhtmlString { + if let localNameString = String(xmlPointer: localName) { + xhtmlString.append("") + } + } else { + assertionFailure("xhtmlString must not be nil when parsingXHTML in xmlEndElement.") + } + } + + else if parsingAuthor { + + if SAXEqualTags(localName, XMLName.author) { + parsingAuthor = false + if let currentAuthor, !currentAuthor.isEmpty() { + currentArticle?.addAuthor(currentAuthor) + } + currentAuthor = nil + } + else if SAXEqualTags(localName, XMLName.name) { + currentAuthor?.name = saxParser.currentStringWithTrimmedWhitespace + } + else if SAXEqualTags(localName, XMLName.email) { + currentAuthor?.emailAddress = saxParser.currentStringWithTrimmedWhitespace + } + else if SAXEqualTags(localName, XMLName.uri) { + currentAuthor?.url = saxParser.currentStringWithTrimmedWhitespace + } + } + + else if SAXEqualTags(localName, XMLName.entry) { + parsingArticle = false + } + + else if parsingArticle && !parsingSource { + addArticleElement(localName, prefix) + } + + else if SAXEqualTags(localName, XMLName.source) { + parsingSource = false + } + + else if !parsingArticle && !parsingSource && SAXEqualTags(localName, XMLName.title) { + addFeedTitle() + } + + _ = attributesStack.popLast() } public func saxParser(_ saxParser: SAXParser, xmlCharactersFound: XMLPointer, count: Int) { diff --git a/Modules/Parser/Sources/FeedParser/Feeds/XML/RSSAuthor.swift b/Modules/Parser/Sources/FeedParser/Feeds/XML/RSSAuthor.swift index 9a5e70f88..b0b93a6e3 100644 --- a/Modules/Parser/Sources/FeedParser/Feeds/XML/RSSAuthor.swift +++ b/Modules/Parser/Sources/FeedParser/Feeds/XML/RSSAuthor.swift @@ -32,4 +32,9 @@ final class RSSAuthor { self.init(name: singleString) } } + + func isEmpty() -> Bool { + + name != nil || url != nil || avatarURL != nil || emailAddress != nil + } }