-
Notifications
You must be signed in to change notification settings - Fork 0
/
html.gleam
219 lines (196 loc) · 5.06 KB
/
html.gleam
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
//// Experimental module used to render functions
//// into an io list and turn that list into a string
////
//// This module is experimental and may change or be removed
////
import gleam/string_builder.{StringBuilder, append, append_builder}
import gleam/list
import gleam/dynamic
import gleam/result
import gleam/string
import gleam/io
import espresso/atoms
pub type Attributes =
List(#(String, String))
pub type Element {
Text(text: String)
Element(tag_name: String, attributes: Attributes, children: Children)
Raw(text: String)
}
type Children =
List(Element)
pub fn new(
tag_name: String,
attributes: Attributes,
children: Children,
) -> Element {
Element(tag_name: tag_name, attributes: attributes, children: children)
}
pub fn render(element: Element) -> StringBuilder {
case element {
Raw(text) ->
string_builder.new()
|> append(text)
Text(text) -> escape(string_builder.new(), text)
Element(tag_name, attributes, children) -> {
string_builder.new()
|> append("<" <> tag_name)
|> render_attributes(attributes)
|> append(">")
|> render_children(children)
|> append("</" <> tag_name <> ">")
}
}
}
pub fn escape(acc: StringBuilder, text: String) -> StringBuilder {
// originally from https://gitlab.com/Nicd/glentities/-/blob/v4.0.1/src/glentities.gleam#L2327
// modified to have a StringBuilder accumulator and not deal with output string
case text {
"" -> acc
"&" <> rest -> escape(string_builder.append(acc, "&"), rest)
"<" <> rest -> escape(string_builder.append(acc, "<"), rest)
">" <> rest -> escape(string_builder.append(acc, ">"), rest)
"\"" <> rest -> escape(string_builder.append(acc, """), rest)
"'" <> rest -> escape(string_builder.append(acc, "'"), rest)
other -> {
let maybe_grapheme = string.pop_grapheme(other)
case maybe_grapheme {
Ok(#(grapheme, rest)) ->
escape(string_builder.append(acc, grapheme), rest)
Error(Nil) -> acc
}
}
}
}
fn render_children(builder: StringBuilder, children: Children) -> StringBuilder {
list.fold(
children,
builder,
fn(builder, child) { append_builder(builder, render(child)) },
)
}
fn render_attributes(builder: StringBuilder, attributes: Attributes) {
list.fold(
attributes,
builder,
fn(builder, attribute) {
let #(key, value) = attribute
builder
|> append(" ")
|> append(key)
|> append("=\"")
|> escape(value)
|> append("\"")
},
)
}
pub fn doctype() -> StringBuilder {
string_builder.from_string("<!DOCTYPE html>")
}
/// Renders an HTML element into a string of HTML
///
/// # Examples
///
/// ```gleam
/// ```
pub fn to_string(dom: Element) -> String {
dom
|> render()
|> string_builder.prepend_builder(doctype())
|> string_builder.to_string()
}
/// Creates a new HTML element with the given tag name
pub fn t(name: String) {
Element(name, [], [])
}
/// Adds an attribute of name+value to an HTML element
pub fn a(el: Element, name: String, value: String) -> Element {
case el {
Element(tag_name, attributes, children) ->
Element(tag_name, [#(name, value), ..attributes], children)
txt -> txt
}
}
/// Adds children to an element
pub fn c(el: Element, new_children: Children) -> Element {
case el {
Element(tag_name, attributes, children) ->
Element(tag_name, attributes, list.append(children, new_children))
txt -> txt
}
}
/// Adds a text node to an element
pub fn txt(text: String) -> Element {
Text(text)
}
pub fn raw(text: String) -> Element {
Raw(text)
}
fn attribute(d: dynamic.Dynamic) {
dynamic.tuple2(first: dynamic.string, second: dynamic.string)(d)
}
fn element(d: dynamic.Dynamic) {
d
|> dynamic.any(of: [
fn(x) {
x
|> dynamic.decode3(
Element,
dynamic.element(1, dynamic.string),
dynamic.element(2, dynamic.list(of: attribute)),
dynamic.element(3, dynamic.list(of: element)),
)
},
fn(x) {
case dynamic.element(0, atoms.decode)(x) {
Ok(atom) -> {
case atom {
atoms.Raw ->
x
|> dynamic.decode1(Raw, dynamic.element(1, dynamic.string))
atoms.Text ->
x
|> dynamic.decode1(Text, dynamic.element(1, dynamic.string))
_ -> Error([])
}
}
_ -> {
Error([])
}
}
},
])
}
fn decode_dyn() {
dynamic.any(of: [
fn(x) {
x
|> dynamic.string()
|> result.map(fn(text) { [Text(text)] })
},
fn(x) {
x
|> element()
|> result.map(fn(element) { [element] })
},
fn(x) {
x
|> dynamic.list(of: element)
|> result.map(fn(elements) { elements })
},
])
}
pub fn dyn(el: Element, dyn) -> Element {
let children_result =
dyn
|> dynamic.from()
|> decode_dyn()
case children_result {
Ok(children) -> c(el, children)
Error(error) -> {
io.println_error("ERROR:")
io.debug(error)
c(el, [txt("There was a problem parsing this dyn")])
}
}
}