2018-10-01 19:14:09 +00:00
|
|
|
//
|
|
|
|
// ContentLabel.swift
|
|
|
|
// Tusker
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 10/1/18.
|
|
|
|
// Copyright © 2018 Shadowfacts. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
2019-01-19 19:31:31 +00:00
|
|
|
import SafariServices
|
2018-10-01 19:14:09 +00:00
|
|
|
import TTTAttributedLabel
|
|
|
|
import Pachyderm
|
|
|
|
import SwiftSoup
|
|
|
|
|
2019-02-03 16:39:27 +00:00
|
|
|
class ContentLabel: LinkLabel {
|
2018-10-01 19:14:09 +00:00
|
|
|
|
2018-10-12 01:20:58 +00:00
|
|
|
var navigationDelegate: TuskerNavigationDelegate?
|
2018-10-01 19:14:09 +00:00
|
|
|
|
|
|
|
// MARK: - HTML Parsing
|
|
|
|
func setTextFromHtml(_ html: String) {
|
|
|
|
let doc = try! SwiftSoup.parse(html)
|
|
|
|
let body = doc.body()!
|
|
|
|
|
|
|
|
let (attributedText, links) = attributedTextForHTMLNode(body)
|
|
|
|
let mutAttrString = NSMutableAttributedString(attributedString: attributedText)
|
2019-01-14 00:46:32 +00:00
|
|
|
|
|
|
|
// only trailing whitespace can be trimmed here
|
|
|
|
// when posting an attachment without any text, pleromafe includes U+200B ZERO WIDTH SPACE at the beginning
|
|
|
|
// this would get trimmed and cause range out of bounds crashes
|
|
|
|
mutAttrString.trimTrailingCharactersInSet(.whitespacesAndNewlines)
|
|
|
|
|
2018-10-01 19:14:09 +00:00
|
|
|
mutAttrString.addAttribute(.font, value: font, range: NSRange(location: 0, length: mutAttrString.length))
|
2019-02-03 16:39:27 +00:00
|
|
|
|
|
|
|
let linkAttributes: [NSAttributedString.Key: Any] = [
|
|
|
|
.foregroundColor: UIColor.blue,
|
|
|
|
]
|
|
|
|
|
2018-10-01 19:14:09 +00:00
|
|
|
for (range, url) in links {
|
2019-02-03 16:39:27 +00:00
|
|
|
mutAttrString.addAttributes(linkAttributes, range: range)
|
|
|
|
self.links.append(Link(range: range, url: url))
|
2018-10-01 19:14:09 +00:00
|
|
|
}
|
|
|
|
|
2019-02-03 16:39:27 +00:00
|
|
|
self.attributedText = mutAttrString
|
2018-10-01 19:14:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private func attributedTextForHTMLNode(_ node: Node) -> (NSAttributedString, [NSRange: URL]) {
|
|
|
|
switch node {
|
|
|
|
case let node as TextNode:
|
|
|
|
return (NSAttributedString(string: node.text()), [:])
|
|
|
|
case let node as Element:
|
|
|
|
var links = [NSRange: URL]()
|
|
|
|
let attributed = NSMutableAttributedString()
|
|
|
|
for child in node.getChildNodes() {
|
|
|
|
let (text, childLinks) = attributedTextForHTMLNode(child)
|
|
|
|
for (range, url) in childLinks {
|
|
|
|
let newRange = NSRange(location: range.location + attributed.length, length: range.length)
|
|
|
|
links[newRange] = url
|
|
|
|
}
|
|
|
|
attributed.append(text)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch node.tagName() {
|
|
|
|
case "br":
|
|
|
|
attributed.append(NSAttributedString(string: "\n"))
|
|
|
|
case "a":
|
|
|
|
if let link = try? node.attr("href"),
|
|
|
|
let url = URL(string: link) {
|
|
|
|
let linkRange = NSRange(location: 0, length: attributed.length)
|
|
|
|
links[linkRange] = url
|
|
|
|
}
|
|
|
|
case "p":
|
|
|
|
attributed.append(NSAttributedString(string: "\n\n"))
|
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
return (attributed, links)
|
|
|
|
default:
|
|
|
|
fatalError("Unexpected node type: \(type(of: node))")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-03 16:39:27 +00:00
|
|
|
func getViewController(forLink url: URL, inRange range: NSRange) -> UIViewController {
|
|
|
|
let text = (self.text! as NSString).substring(with: range)
|
|
|
|
|
2018-10-12 01:20:58 +00:00
|
|
|
if let mention = getMention(for: url, text: text) {
|
2019-01-19 19:31:31 +00:00
|
|
|
return ProfileTableViewController(accountID: mention.id)
|
2018-10-12 01:20:58 +00:00
|
|
|
} else if let tag = getHashtag(for: url, text: text) {
|
2019-01-19 19:31:31 +00:00
|
|
|
return TimelineTableViewController(for: .tag(hashtag: tag.name))
|
2018-10-12 01:20:58 +00:00
|
|
|
} else {
|
2019-01-19 19:31:31 +00:00
|
|
|
return SFSafariViewController(url: url)
|
2018-10-12 01:20:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-03 16:39:27 +00:00
|
|
|
func getViewController(forLinkAt point: CGPoint) -> UIViewController? {
|
|
|
|
guard let link = getLink(atPoint: point) else {
|
|
|
|
return nil
|
2018-10-12 01:20:58 +00:00
|
|
|
}
|
2019-02-03 16:39:27 +00:00
|
|
|
return getViewController(forLink: link.url, inRange: link.range)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Interaction
|
|
|
|
|
|
|
|
override func linkTapped(_ link: LinkLabel.Link) {
|
|
|
|
let text = (self.text! as NSString).substring(with: link.range)
|
|
|
|
|
|
|
|
if let mention = getMention(for: link.url, text: text) {
|
|
|
|
navigationDelegate?.selected(mention: mention)
|
|
|
|
} else if let tag = getHashtag(for: link.url, text: text) {
|
|
|
|
navigationDelegate?.selected(tag: tag)
|
2018-10-12 01:20:58 +00:00
|
|
|
} else {
|
2019-02-03 16:39:27 +00:00
|
|
|
navigationDelegate?.selected(url: link.url)
|
2018-10-01 19:14:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-03 16:39:27 +00:00
|
|
|
override func linkLongPressed(_ link: LinkLabel.Link) {
|
|
|
|
navigationDelegate?.showMoreOptions(forURL: link.url)
|
2018-10-01 19:14:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Navigation
|
|
|
|
func getMention(for url: URL, text: String) -> Mention? {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getHashtag(for url: URL, text: String) -> Hashtag? {
|
|
|
|
if text.starts(with: "#") {
|
|
|
|
let tag = String(text.dropFirst())
|
|
|
|
return Hashtag(name: tag, url: url)
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|