From f305cc50e2e7077dd4f0ceed5bfc0fc6fe3c76c8 Mon Sep 17 00:00:00 2001 From: Maurice Parker Date: Sun, 19 Apr 2020 20:29:43 -0500 Subject: [PATCH] Implement twitter entities as html links --- .../FeedProvider/Twitter/TwitterHashtag.swift | 13 ++--- .../FeedProvider/Twitter/TwitterMention.swift | 8 ++- .../FeedProvider/Twitter/TwitterStatus.swift | 58 +++++++++++++++---- .../FeedProvider/Twitter/TwitterSymbol.swift | 15 ++--- .../FeedProvider/Twitter/TwitterURL.swift | 10 +++- 5 files changed, 70 insertions(+), 34 deletions(-) diff --git a/Frameworks/Account/FeedProvider/Twitter/TwitterHashtag.swift b/Frameworks/Account/FeedProvider/Twitter/TwitterHashtag.swift index a2a614e61..029f56c43 100644 --- a/Frameworks/Account/FeedProvider/Twitter/TwitterHashtag.swift +++ b/Frameworks/Account/FeedProvider/Twitter/TwitterHashtag.swift @@ -18,14 +18,11 @@ struct TwitterHashtag: Codable, TwitterEntity { case indices = "indices" } - var startIndex: Int { - if let indices = indices, indices.count > 0 { - return indices[0] - 1 - } - return 0 - } - func renderAsHTML() -> String { - return "" + var html = String() + if let text = text { + html += "#\(text)" + } + return html } } diff --git a/Frameworks/Account/FeedProvider/Twitter/TwitterMention.swift b/Frameworks/Account/FeedProvider/Twitter/TwitterMention.swift index 72310c8ae..4cd00551f 100644 --- a/Frameworks/Account/FeedProvider/Twitter/TwitterMention.swift +++ b/Frameworks/Account/FeedProvider/Twitter/TwitterMention.swift @@ -13,19 +13,21 @@ struct TwitterMention: Codable, TwitterEntity { let name: String? let indices: [Int]? let screenName: String? - let expandedURL: String? let idStr: String? enum CodingKeys: String, CodingKey { case name = "url" case indices = "indices" case screenName = "screen_name" - case expandedURL = "expandedURL" case idStr = "idStr" } func renderAsHTML() -> String { - return "" + var html = String() + if let screenName = screenName { + html += "@\(screenName)" + } + return html } } diff --git a/Frameworks/Account/FeedProvider/Twitter/TwitterStatus.swift b/Frameworks/Account/FeedProvider/Twitter/TwitterStatus.swift index 62f429371..cb9c2886c 100644 --- a/Frameworks/Account/FeedProvider/Twitter/TwitterStatus.swift +++ b/Frameworks/Account/FeedProvider/Twitter/TwitterStatus.swift @@ -41,16 +41,6 @@ final class TwitterStatus: Codable { return "\(userURL)/status/\(idStr)" } - var displayText: String? { - if let text = fullText, let displayRange = displayTextRange, displayRange.count > 1, - let startIndex = text.index(text.startIndex, offsetBy: displayRange[0], limitedBy: text.endIndex), - let endIndex = text.index(text.startIndex, offsetBy: displayRange[1], limitedBy: text.endIndex) { - return String(text[startIndex.. String? { let statusToRender = retweetedStatus != nil ? retweetedStatus! : self return statusToRender.displayText @@ -66,8 +56,54 @@ final class TwitterStatus: Codable { return renderAsOriginalHTML(topLevel: topLevel) } +} + +private extension TwitterStatus { + + var displayText: String? { + if let text = fullText, let displayRange = displayTextRange, displayRange.count > 1, + let startIndex = text.index(text.startIndex, offsetBy: displayRange[0], limitedBy: text.endIndex), + let endIndex = text.index(text.startIndex, offsetBy: displayRange[1], limitedBy: text.endIndex) { + return String(text[startIndex.. 1, + let displayStartIndex = text.index(text.startIndex, offsetBy: displayRange[0], limitedBy: text.endIndex), + let displayEndIndex = text.index(text.startIndex, offsetBy: displayRange[1], limitedBy: text.endIndex), + let entities = entities?.combineAndSort() { + + var html = String() + var prevIndex = displayStartIndex + + for entity in entities { + if let entityStartIndex = text.index(text.startIndex, offsetBy: entity.startIndex, limitedBy: text.endIndex), + let entityEndIndex = text.index(text.startIndex, offsetBy: entity.endIndex, limitedBy: text.endIndex) { + + if prevIndex < entityStartIndex { + html += String(text[prevIndex.. String { - var html = "
\(status.displayText ?? "")
" + var html = "
\(status.displayHTML ?? "")
" if !topLevel, let createdAt = status.createdAt { let dateFormatter = DateFormatter() diff --git a/Frameworks/Account/FeedProvider/Twitter/TwitterSymbol.swift b/Frameworks/Account/FeedProvider/Twitter/TwitterSymbol.swift index 7c90d9300..525a1a595 100644 --- a/Frameworks/Account/FeedProvider/Twitter/TwitterSymbol.swift +++ b/Frameworks/Account/FeedProvider/Twitter/TwitterSymbol.swift @@ -17,16 +17,13 @@ struct TwitterSymbol: Codable, TwitterEntity { case name = "name" case indices = "indices" } - - var startIndex: Int { - if let indices = indices, indices.count > 0 { - return indices[0] - 1 - } - return 0 - } - + func renderAsHTML() -> String { - return "" + var html = String() + if let name = name { + html += "$\(name)" + } + return html } } diff --git a/Frameworks/Account/FeedProvider/Twitter/TwitterURL.swift b/Frameworks/Account/FeedProvider/Twitter/TwitterURL.swift index 5c8c8d2da..d7f80c6b6 100644 --- a/Frameworks/Account/FeedProvider/Twitter/TwitterURL.swift +++ b/Frameworks/Account/FeedProvider/Twitter/TwitterURL.swift @@ -18,12 +18,16 @@ struct TwitterURL: Codable, TwitterEntity { enum CodingKeys: String, CodingKey { case url = "url" case indices = "indices" - case displayURL = "displayURL" - case expandedURL = "expandedURL" + case displayURL = "display_url" + case expandedURL = "expanded_url" } func renderAsHTML() -> String { - return "" + var html = String() + if let expandedURL = expandedURL, let displayURL = displayURL { + html += "\(displayURL)" + } + return html } }