-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
rendering_html.gleam
70 lines (64 loc) · 1.78 KB
/
rendering_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
//// # Rendering HTML
////
//// Lustre is often used for complex stateful HTML applications, but it also
//// makes a great type-safe HTML templating system. It works on all targets.
////
//// ## Dependencies
////
//// - https://hex.pm/packages/lustre
import gleam/string
import gleeunit/should
import lustre/attribute
import lustre/element
import lustre/element/html
pub fn main_test() {
// Lustre provides functions for HTML elements and attributes that can be used
// to construct an HTML document.
//
let document =
html.html([], [
html.head([], [html.title([], "Greetings!")]),
html.body([], [
html.h1([attribute.id("greeting")], [
html.text("Hello, Joe! Hello, Mike!"),
]),
]),
])
// The document can be converted to a string.
document
|> element.to_document_string
|> should.equal(normalise_test_html(
"
<!doctype html>
<html>
<head>
<title>Greetings!</title>
</head>
<body>
<h1 id=\"greeting\">Hello, Joe! Hello, Mike!</h1>
</body>
</html>
",
))
// Or you can render a fragment without a doctype.
html.input([
attribute.for("name"),
attribute.type_("text"),
attribute.value("Lucy"),
])
|> element.to_string
|> should.equal("<input for=\"name\" type=\"text\" value=\"Lucy\">")
}
//
// Lustre has excellent documentation. Check it out for more information
// https://hexdocs.pm/lustre/
//
fn normalise_test_html(html: String) -> String {
// We write the text expectations in a pretty-printed format to make it easier
// to understand, but Lustre produces minified HTML for performance and
// compactness, so this functional normalises it so they match.
html
|> string.replace(" ", "")
|> string.replace("\n", "")
|> string.replace("<html>", "\n<html>")
}