|
| 1 | +// |
| 2 | +// ActiveBuilder.swift |
| 3 | +// ActiveLabel |
| 4 | +// |
| 5 | +// Created by Pol Quintana on 04/09/16. |
| 6 | +// Copyright © 2016 Optonaut. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import Foundation |
| 10 | + |
| 11 | +typealias ActiveFilterPredicate = ((String) -> Bool) |
| 12 | + |
| 13 | +struct ActiveBuilder { |
| 14 | + |
| 15 | + static func createElements(type: ActiveType, from text: String, range: NSRange, filterPredicate: ActiveFilterPredicate?) -> [ElementTuple] { |
| 16 | + switch type { |
| 17 | + case .mention, .hashtag: |
| 18 | + return createElementsIgnoringFirstCharacter(from: text, for: type, range: range, filterPredicate: filterPredicate) |
| 19 | + case .url: |
| 20 | + return createElements(from: text, for: type, range: range, filterPredicate: filterPredicate) |
| 21 | + case .custom: |
| 22 | + return createElements(from: text, for: type, range: range, minLength: 1, filterPredicate: filterPredicate) |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + static func createURLElements(from text: String, range: NSRange, maxChar: Int?) -> ([ElementTuple], String) { |
| 27 | + let type = ActiveType.url |
| 28 | + var text = text |
| 29 | + let matches = RegexParser.getElements(from: text, with: type.pattern, range: range) |
| 30 | + let nsstring = text as NSString |
| 31 | + var elements: [ElementTuple] = [] |
| 32 | + |
| 33 | + for match in matches where match.range.length > 2 { |
| 34 | + let word = nsstring.substring(with: match.range) |
| 35 | + .trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) |
| 36 | + |
| 37 | + guard let maxChar = maxChar, word.characters.count > maxChar else { |
| 38 | + let element = ActiveElement.create(with: type, text: word) |
| 39 | + elements.append((match.range, element, type)) |
| 40 | + continue |
| 41 | + } |
| 42 | + |
| 43 | + let trimmedWord = word.trim(to: maxChar) |
| 44 | + text = text.replacingOccurrences(of: word, with: trimmedWord) |
| 45 | + let element = ActiveElement.url(original: word, trimmed: trimmedWord) |
| 46 | + |
| 47 | + let newRange = NSRange(location: match.range.location, length: trimmedWord.characters.count + 1) |
| 48 | + elements.append((newRange, element, type)) |
| 49 | + } |
| 50 | + return (elements, text) |
| 51 | + } |
| 52 | + |
| 53 | + private static func createElements(from text: String, |
| 54 | + for type: ActiveType, |
| 55 | + range: NSRange, |
| 56 | + minLength: Int = 2, |
| 57 | + filterPredicate: ActiveFilterPredicate?) -> [ElementTuple] { |
| 58 | + |
| 59 | + let matches = RegexParser.getElements(from: text, with: type.pattern, range: range) |
| 60 | + let nsstring = text as NSString |
| 61 | + var elements: [ElementTuple] = [] |
| 62 | + |
| 63 | + for match in matches where match.range.length > minLength { |
| 64 | + let word = nsstring.substring(with: match.range) |
| 65 | + .trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) |
| 66 | + if filterPredicate?(word) ?? true { |
| 67 | + let element = ActiveElement.create(with: type, text: word) |
| 68 | + elements.append((match.range, element, type)) |
| 69 | + } |
| 70 | + } |
| 71 | + return elements |
| 72 | + } |
| 73 | + |
| 74 | + private static func createElementsIgnoringFirstCharacter(from text: String, |
| 75 | + for type: ActiveType, |
| 76 | + range: NSRange, |
| 77 | + filterPredicate: ActiveFilterPredicate?) -> [ElementTuple] { |
| 78 | + let matches = RegexParser.getElements(from: text, with: type.pattern, range: range) |
| 79 | + let nsstring = text as NSString |
| 80 | + var elements: [ElementTuple] = [] |
| 81 | + |
| 82 | + for match in matches where match.range.length > 2 { |
| 83 | + let range = NSRange(location: match.range.location + 1, length: match.range.length - 1) |
| 84 | + var word = nsstring.substring(with: range) |
| 85 | + if word.hasPrefix("@") { |
| 86 | + word.remove(at: word.startIndex) |
| 87 | + } |
| 88 | + else if word.hasPrefix("#") { |
| 89 | + word.remove(at: word.startIndex) |
| 90 | + } |
| 91 | + |
| 92 | + if filterPredicate?(word) ?? true { |
| 93 | + let element = ActiveElement.create(with: type, text: word) |
| 94 | + elements.append((match.range, element, type)) |
| 95 | + } |
| 96 | + } |
| 97 | + return elements |
| 98 | + } |
| 99 | +} |
0 commit comments