-
Notifications
You must be signed in to change notification settings - Fork 146
/
Copy pathAttributedStringSerializer.swift
201 lines (157 loc) · 7.09 KB
/
AttributedStringSerializer.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import Foundation
import UIKit
protocol AttributedStringSerializerCustomizer {
func converter(for: ElementNode) -> ElementConverter?
}
/// Composes an attributed string from an HTML tree.
///
class AttributedStringSerializer {
private let customizer: AttributedStringSerializerCustomizer?
// MARK: - Element Converters
private static let defaultElementConverters: [Element: ElementConverter] = [
.br: BRElementConverter(),
.cite: CiteElementConverter(),
.figcaption: FigcaptionElementConverter(),
.figure: FigureElementConverter(),
.hr: HRElementConverter(),
.img: ImageElementConverter(),
.video: VideoElementConverter(),
.li: LIElementConverter()
]
// MARK: - Attributes Converter
let attributesConverter = MainAttributesConverter([
BoldElementAttributesConverter(),
ItalicElementAttributesConverter(),
UnderlineElementAttributesConverter(),
ForegroundColorElementAttributesConverter(),
]
)
// MARK: - Initializers
required init(
customizer: AttributedStringSerializerCustomizer? = nil) {
self.customizer = customizer
}
// MARK: - Conversion
/// Serializes an attributed string with the specified node hierarchy.
///
/// - Parameters:
/// - node: the head of the tree to compose into an attributed string.
///
/// - Returns: the requested attributed string.
///
func serialize(_ node: Node, defaultAttributes: [NSAttributedString.Key: Any] = [:]) -> NSAttributedString {
return serialize(node, inheriting: defaultAttributes)
}
/// Recursive serialization method.
///
/// - Parameters:
/// - node: the node to convert to `NSAttributedString`.
/// - attributes: the inherited attributes from parent nodes.
///
/// - Returns: the converted node as an `NSAttributedString`.
///
func serialize(_ node: Node, inheriting attributes: [NSAttributedString.Key: Any]) -> NSAttributedString {
switch node {
case let textNode as TextNode:
return serialize(textNode, inheriting: attributes)
case let commentNode as CommentNode:
return serialize(commentNode, inheriting: attributes)
case let elementNode as ElementNode:
return serialize(elementNode, inheriting: attributes)
default:
fatalError("Nodes can be either text, comment or element nodes.")
}
}
/// Serializes a `TextNode`.
///
/// - Parameters:
/// - node: the node to convert to `NSAttributedString`.
/// - attributes: the inherited attributes from parent nodes.
///
/// - Returns: the converted node as an `NSAttributedString`.
///
fileprivate func serialize(_ node: TextNode, inheriting attributes: [NSAttributedString.Key: Any]) -> NSAttributedString {
let text = node.sanitizedText()
guard text.count > 0 else {
return NSAttributedString()
}
let string = NSAttributedString(string: text, attributes: attributes)
guard !node.needsClosingParagraphSeparator() else {
return appendParagraphSeparator(to: string, inheriting: attributes)
}
return string
}
/// Serializes a `CommentNode`.
///
/// - Parameters:
/// - node: the node to convert to `NSAttributedString`.
/// - attributes: the inherited attributes from parent nodes.
///
/// - Returns: the converted node as an `NSAttributedString`.
///
fileprivate func serialize(_ node: CommentNode, inheriting attributes: [NSAttributedString.Key: Any]) -> NSAttributedString {
let attachment = CommentAttachment()
attachment.text = node.comment
let content = NSMutableAttributedString(attachment: attachment, attributes: attributes, aztec: ())
guard !node.needsClosingParagraphSeparator() else {
return appendParagraphSeparator(to: content, inheriting: attributes)
}
return content
}
/// Serializes an `ElementNode`.
///
/// - Parameters:
/// - element: the node to convert to `NSAttributedString`.
/// - attributes: the inherited attributes from parent nodes.
///
/// - Returns: the converted node as an `NSAttributedString`.
///
fileprivate func serialize(_ element: ElementNode, inheriting attributes: [NSAttributedString.Key: Any]) -> NSAttributedString {
let content = NSMutableAttributedString()
let attributes = attributesConverter.convert(element.attributes, inheriting: attributes)
let converter = self.converter(for: element)
let convertedString = converter.convert(element, inheriting: attributes, contentSerializer: contentSerializer)
content.append(convertedString)
return content
}
// MARK: - Paragraph Separator
private func appendParagraphSeparator(to string: NSAttributedString, inheriting inheritedAttributes: [NSAttributedString.Key: Any]) -> NSAttributedString {
let stringWithSeparator = NSMutableAttributedString(attributedString: string)
stringWithSeparator.append(NSAttributedString(.paragraphSeparator, attributes: inheritedAttributes))
return NSAttributedString(attributedString: stringWithSeparator)
}
// MARK: - Built-in element converter instances
// This converter should not be added to `elementFormattersMap`. This converter is returned
// whenever the map doesn't find a proper match.
//
private(set) lazy var genericElementConverter = GenericElementConverter()
lazy var contentSerializer: ElementConverter.ContentSerializer = { [unowned self] (elementNode, intrinsicRepresentation, attributes, intrinsicRepresentationBeforeChildren) in
let content = NSMutableAttributedString()
if let intrinsicRepresentation = intrinsicRepresentation, intrinsicRepresentationBeforeChildren {
content.append(intrinsicRepresentation)
}
for child in elementNode.children {
let nodeString = self.serialize(child, inheriting: attributes)
content.append(nodeString)
}
if let intrinsicRepresentation = intrinsicRepresentation, !intrinsicRepresentationBeforeChildren {
content.append(intrinsicRepresentation)
}
guard !elementNode.needsClosingParagraphSeparator() else {
return self.appendParagraphSeparator(to: content, inheriting: attributes)
}
return content
}
}
private extension AttributedStringSerializer {
// MARK: - Element Converters
/// Some element types have an implicit representation that doesn't really follow the standard conversion logic.
/// Element Converters take care of that.
///
func converter(for element: ElementNode) -> ElementConverter {
if let converter = customizer?.converter(for: element) {
return converter
}
return AttributedStringSerializer.defaultElementConverters[element.type] ?? genericElementConverter
}
}