Improve tweet formatting when they contain emoji with multiple scalars

This commit is contained in:
Maurice Parker 2020-04-20 10:21:29 -05:00
parent 492e9ae065
commit 56c8dad895
3 changed files with 36 additions and 22 deletions

View File

@ -254,10 +254,10 @@ private extension TwitterFeedProvider {
dateFormatter.dateFormat = Self.dateFormat dateFormatter.dateFormat = Self.dateFormat
decoder.dateDecodingStrategy = .formatted(dateFormatter) decoder.dateDecodingStrategy = .formatted(dateFormatter)
// let jsonString = String(data: response.data, encoding: .utf8) let jsonString = String(data: response.data, encoding: .utf8)
// let url = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent("twitter.json") let url = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent("twitter.json")
// print("******** writing to: \(url.path)") print("******** writing to: \(url.path)")
// try? jsonString?.write(toFile: url.path, atomically: true, encoding: .utf8) try? jsonString?.write(toFile: url.path, atomically: true, encoding: .utf8)
do { do {
let tweets: [TwitterStatus] let tweets: [TwitterStatus]

View File

@ -71,20 +71,34 @@ private extension TwitterStatus {
} }
var displayHTML: String? { var displayHTML: String? {
if let text = fullText, let displayRange = displayTextRange, displayRange.count > 1, if let text = fullText, let displayRange = displayTextRange, displayRange.count > 1, let entities = entities?.combineAndSort() {
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 displayStartIndex = text.index(text.startIndex, offsetBy: displayRange[0], limitedBy: text.endIndex) ?? text.startIndex
let entities = entities?.combineAndSort() { let displayEndIndex = text.index(text.startIndex, offsetBy: displayRange[1], limitedBy: text.endIndex) ?? text.endIndex
var html = String() var html = String()
var prevIndex = displayStartIndex var prevIndex = displayStartIndex
for entity in entities { 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) { // The twitter indices are messed up by emoji with more than one scalar, we are trying to adjust for that here.
var offset = 0
let emojiEndIndex = text.index(text.startIndex, offsetBy: entity.endIndex, limitedBy: text.endIndex) ?? text.endIndex
if text.startIndex < prevIndex {
let emojis = String(text[text.startIndex..<emojiEndIndex]).emojis
for emoji in emojis {
offset += emoji.unicodeScalars.count - 1
}
}
let offsetStartIndex = entity.startIndex - offset
let offsetEndIndex = entity.endIndex - offset
let entityStartIndex = text.index(text.startIndex, offsetBy: offsetStartIndex, limitedBy: text.endIndex) ?? text.startIndex
let entityEndIndex = text.index(text.startIndex, offsetBy: offsetEndIndex, limitedBy: text.endIndex) ?? text.endIndex
if prevIndex < entityStartIndex { if prevIndex < entityStartIndex {
html += String(text[prevIndex..<entityStartIndex]) html += String(text[prevIndex..<entityStartIndex]).replacingOccurrences(of: "\n", with: "<br>")
} }
// We drop off any URL which is just pointing to the quoted status. It is redundant. // We drop off any URL which is just pointing to the quoted status. It is redundant.
@ -97,7 +111,7 @@ private extension TwitterStatus {
} }
prevIndex = entityEndIndex prevIndex = entityEndIndex
}
} }
if prevIndex < displayEndIndex { if prevIndex < displayEndIndex {

@ -1 +1 @@
Subproject commit a742db73c4f4007f0d7097746c88ce2074400045 Subproject commit 3dfa570a4600690290cd946b8e122b0b99da0a13