-
Notifications
You must be signed in to change notification settings - Fork 1
/
html.go
97 lines (83 loc) · 2.09 KB
/
html.go
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
package bulma
import (
e "github.com/willoma/gomplements"
"maragu.dev/gomponents"
"maragu.dev/gomponents/components"
"maragu.dev/gomponents/html"
)
const cdnPath = "https://cdn.jsdelivr.net/npm/bulma@1.0.2/css/bulma.min.css"
// CSSPath, when provided to HTML, sets the path to the Bulma CSS
type CSSPath string
// Language, when provided to HTML, sets the page lang attribute
type Language string
// HTitle, when provided to HTML, sets the page title
type HTitle string
// Description, when provided to HTML, sets the description metadata
type Description string
// Head identifies children as part of the head section
func Head(children ...gomponents.Node) head {
return head(children)
}
type head []gomponents.Node
// HTML returns a gomponents.Node which represents a whole HTML page.
//
// http://willoma.github.io/bulma-gomponents/elements.html#document-root
func HTML(children ...any) gomponents.Node {
var (
cssPath string
language string
title string
description string
headSection []gomponents.Node
body = e.Elem(html.Body)
)
for _, c := range children {
switch c := c.(type) {
case CSSPath:
cssPath = string(c)
case HTitle:
title = string(c)
case Language:
language = string(c)
case Description:
description = string(c)
case head:
headSection = append(headSection, c...)
default:
body.With(c)
}
}
if cssPath == "" {
cssPath = cdnPath
}
return components.HTML5(components.HTML5Props{
Title: title,
Description: description,
Language: language,
Head: append(
headSection,
html.Link(
html.Rel("stylesheet"),
html.Href(cssPath),
),
),
Body: body.GetNodes(),
})
}
// Stylesheet returns a head node which makes the browser load the provided path
// as a CSS stylesheet.
func Stylesheet(path string) head {
return head([]gomponents.Node{
html.Link(
html.Rel("stylesheet"),
html.Href(path),
),
})
}
// Script returns a head node which makes the browser load the provided path
// as a JS script.
func Script(path string) head {
return head([]gomponents.Node{
html.Script(html.Src(path)),
})
}