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