forked from JohnSundell/Plot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNode.swift
231 lines (199 loc) · 8.61 KB
/
Node.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/**
* Plot
* Copyright (c) John Sundell 2019
* MIT license, see LICENSE file for details
*/
import Foundation
/// A representation of a node within a document's hierarchy.
///
/// Plot treats all elements and attributes that a document contains
/// as nodes. When using the Plot DSL, each time you create a new
/// element, or add an attribute to an existing one, you are creating
/// a node. Nodes can also contain just text, which can either be
/// escaped or treated as raw, pre-processed text. Groups can also be
/// created to form components.
public struct Node<Context> {
private let rendering: (inout Renderer) -> Void
}
public extension Node {
/// An empty node, which won't be rendered.
static var empty: Node { Node { _ in } }
/// Create a node from a raw piece of text that should be rendered as-is.
/// - parameter text: The raw text that the node should contain.
static func raw(_ text: String) -> Node {
Node { $0.renderRawText(text) }
}
/// Create a node from a piece of free-form text that should be escaped.
/// - parameter text: The text that the node should contain.
static func text(_ text: String) -> Node {
Node { $0.renderText(text) }
}
/// Create a node representing an element
/// - parameter element: The element that the node should contain.
static func element(_ element: Element<Context>) -> Node {
Node { $0.renderElement(element) }
}
/// Create a custom element with a given name.
/// - parameter name: The name of the element to create.
static func element(named name: String) -> Node {
.element(Element(name: name, nodes: []))
}
/// Create a custom element with a given name and an array of child nodes.
/// - parameter name: The name of the element to create.
/// - parameter nodes: The nodes (child elements + attributes) to add to the element.
static func element(named name: String, nodes: [Node]) -> Node {
.element(Element(name: name, nodes: nodes))
}
/// Create a custom element with a given name and an array of child nodes.
/// - parameter name: The name of the element to create.
/// - parameter nodes: The nodes (child elements + attributes) to add to the element.
static func element<C>(named name: String, nodes: [Node<C>]) -> Node {
.element(Element(name: name, nodes: nodes))
}
/// Create a custom element with a given name and text content.
/// - parameter name: The name of the element to create.
/// - parameter text: The text to use as the node's content.
static func element(named name: String, text: String) -> Node {
.element(Element(name: name, nodes: [Node.text(text)]))
}
/// Create a custom element with a given name and an array of attributes.
/// - parameter name: The name of the element to create.
/// - parameter attributes: The attributes to add to the element.
static func element<C>(named name: String, attributes: [Attribute<C>]) -> Node {
.element(Element(name: name, nodes: attributes.map(\.node)))
}
/// Create a custom element with a given name and an array of attributes.
/// - parameter name: The name of the element to create.
/// - parameter attributes: The attributes to add to the element.
static func element(named name: String, attributes: [Attribute<Context>]) -> Node {
.element(Element(name: name, nodes: attributes.map(\.node)))
}
/// Create a custom self-closed element with a given name.
/// - parameter name: The name of the element to create.
static func selfClosedElement(named name: String) -> Node {
.element(Element(name: name, closingMode: .selfClosing, nodes: []))
}
/// Create a custom self-closed element with a given name and an array of attributes.
/// - parameter name: The name of the element to create.
/// - parameter attributes: The attributes to add to the element.
static func selfClosedElement<C>(named name: String, attributes: [Attribute<C>]) -> Node {
.element(Element(name: name, closingMode: .selfClosing, nodes: attributes.map(\.node)))
}
/// Create a custom self-closed element with a given name and an array of attributes.
/// - parameter name: The name of the element to create.
/// - parameter attributes: The attributes to add to the element.
static func selfClosedElement(named name: String, attributes: [Attribute<Context>]) -> Node {
.element(Element(name: name, closingMode: .selfClosing, nodes: attributes.map(\.node)))
}
/// Create a node that represents an attribute.
/// - parameter attribute: The attribute that the node should contain.
static func attribute(_ attribute: Attribute<Context>) -> Node {
Node { $0.renderAttribute(attribute) }
}
/// Create a custom attribute with a given name.
/// - parameter name: The name of the attribute to create.
static func attribute(named name: String) -> Node {
.attribute(Attribute(
name: name,
value: nil,
ignoreIfValueIsEmpty: false
))
}
/// Create a custom attribute with a given name and value.
/// - parameter name: The name of the attribute to create.
/// - parameter value: The attribute's value.
/// - parameter ignoreIfValueIsEmpty: Whether the attribute should be ignored if
/// its value is empty (default: true).
static func attribute(named name: String,
value: String?,
ignoreIfValueIsEmpty: Bool = true) -> Node {
.attribute(Attribute(
name: name,
value: value,
ignoreIfValueIsEmpty: ignoreIfValueIsEmpty
))
}
/// Create a group of nodes from an array.
/// - parameter members: The nodes that should be included in the group.
static func group(_ members: [Node]) -> Node {
Node { renderer in
members.forEach { $0.render(into: &renderer) }
}
}
/// Create a group of nodes using variadic parameter syntax.
/// - parameter members: The nodes that should be included in the group.
static func group(_ members: Node...) -> Node {
.group(members)
}
/// Create a node that wraps a `Component`. You can use this API to
/// integrate a component into a `Node`-based hierarchy.
/// - parameter component: The component that should be wrapped.
static func component(_ component: Component) -> Node {
Node { $0.renderComponent(component) }
}
/// Create a node that wraps a set of components defined within a closure. You
/// can use this API to integrate a group of components into a `Node`-based hierarchy.
/// - parameter content: A closure that creates a group of components.
static func components(@ComponentBuilder _ content: () -> Component) -> Node {
.component(content())
}
}
internal extension Node where Context: DocumentFormat {
static func document(_ document: Document<Context>) -> Node {
Node { renderer in
document.elements.forEach {
renderer.renderElement($0)
}
}
}
}
internal extension Node where Context == Any {
static func modifiedComponent(_ component: ModifiedComponent) -> Node {
Node { renderer in
renderer.renderComponent(component.base,
deferredAttributes: component.deferredAttributes + renderer.deferredAttributes,
environmentOverrides: component.environmentOverrides
)
}
}
static func components(_ components: [Component]) -> Node {
Node { renderer in
components.forEach {
renderer.renderComponent($0,
deferredAttributes: renderer.deferredAttributes
)
}
}
}
static func wrappingComponent(
_ component: Component,
using wrapper: ElementWrapper
) -> Node {
Node { renderer in
var wrapper = wrapper
wrapper.deferredAttributes = renderer.deferredAttributes
renderer.renderComponent(component,
elementWrapper: wrapper
)
}
}
}
extension Node: NodeConvertible {
public var node: Self { self }
public func render(indentedBy indentationKind: Indentation.Kind?) -> String {
Renderer.render(self, indentedBy: indentationKind)
}
}
extension Node: Component {
public var body: Component { self }
}
extension Node: ExpressibleByStringInterpolation {
public init(stringLiteral value: String) {
self = .text(value)
}
}
extension Node: AnyNode {
func render(into renderer: inout Renderer) {
rendering(&renderer)
}
}