-
Notifications
You must be signed in to change notification settings - Fork 3
/
html.gleam
155 lines (140 loc) · 3.72 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
import gleam/list
import gleam/string
import gleam/dynamic.{type Dynamic, field}
import gleam/option.{type Option, None, Some}
import gleam/string_builder.{type StringBuilder}
import gleam/json
import sprocket/internal/reconcile.{
type ReconciledAttribute, type ReconciledElement, ReconciledAttribute,
ReconciledComponent, ReconciledCustom, ReconciledElement, ReconciledFragment,
ReconciledIgnoreUpdate, ReconciledText,
}
import sprocket/render.{type Renderer, Renderer}
import sprocket/internal/constants
pub fn html_renderer() -> Renderer(String) {
Renderer(render: fn(el: ReconciledElement) {
render(el)
|> string_builder.to_string()
})
}
fn render(el: ReconciledElement) -> StringBuilder {
case el {
ReconciledElement(tag: tag, key: key, attrs: attrs, children: children) ->
element(tag, key, attrs, children)
ReconciledComponent(el: el, ..) -> component(el)
ReconciledFragment(children: children, ..) -> fragment(children)
ReconciledIgnoreUpdate(el) -> render(el)
ReconciledText(text: t) -> text(t)
ReconciledCustom(kind: kind, data: data) -> custom(kind, data)
}
}
fn el(
tag: String,
attrs: StringBuilder,
inner_html: StringBuilder,
) -> StringBuilder {
string_builder.concat([
string_builder.from_string("<"),
string_builder.from_string(tag),
attrs,
string_builder.from_string(">"),
inner_html,
string_builder.from_string("</"),
string_builder.from_string(tag),
string_builder.from_string(">"),
])
}
fn element(
tag: String,
key: Option(String),
attrs: List(ReconciledAttribute),
children: List(ReconciledElement),
) -> StringBuilder {
let rendered_attrs =
attrs
|> list.fold(string_builder.new(), fn(acc, attr) {
case attr {
ReconciledAttribute(name, value) -> {
string_builder.append_builder(
acc,
string_builder.from_strings([" ", name, "=\"", value, "\""]),
)
}
_ -> acc
}
})
let rendered_attrs = case key {
Some(k) ->
string_builder.append_builder(
rendered_attrs,
string_builder.from_strings([" ", constants.key_attr, "=\"", k, "\""]),
)
None -> rendered_attrs
}
let inner_html =
children
|> list.fold(string_builder.new(), fn(acc, child) {
string_builder.append_builder(acc, render(child))
})
el(tag, rendered_attrs, inner_html)
}
fn component(el: ReconciledElement) {
render(el)
}
fn fragment(children: List(ReconciledElement)) {
children
|> list.fold(string_builder.new(), fn(acc, child) {
string_builder.append_builder(acc, render(child))
})
}
fn safe_replace_char(key: String) -> String {
case key {
"&" -> "&"
"<" -> "<"
">" -> ">"
"\"" -> """
"'" -> "'"
"/" -> "/"
"`" -> "`"
"=" -> "="
_ -> key
}
}
fn escape_html(unsafe: String) {
string.to_graphemes(unsafe)
|> list.fold(string_builder.new(), fn(sb, grapheme) {
string_builder.append(sb, safe_replace_char(grapheme))
})
|> string_builder.to_string
}
fn text(t: String) -> StringBuilder {
escape_html(t)
|> string_builder.from_string()
}
fn custom(kind: String, data: String) -> StringBuilder {
case kind {
"raw" -> {
case json.decode(data, decode_raw) {
Ok(RawHtml(tag, raw_html)) ->
el(
tag,
string_builder.from_string(""),
string_builder.from_string(raw_html),
)
Error(_) -> string_builder.from_string("")
}
}
_ -> string_builder.from_string("")
}
}
type RawHtml {
RawHtml(tag: String, raw_html: String)
}
fn decode_raw(data: Dynamic) {
data
|> dynamic.decode2(
RawHtml,
field("tag", dynamic.string),
field("innerHtml", dynamic.string),
)
}