-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocument.go
More file actions
52 lines (44 loc) · 1.57 KB
/
Copy pathdocument.go
File metadata and controls
52 lines (44 loc) · 1.57 KB
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
package html
import (
. "github.com/tinywasm/dom"
)
// DocumentOptions configures the document shell.
type DocumentOptions struct {
Lang string // default "en"
Title string
CSSURL string // <link rel="stylesheet">
JSURL string // <script src defer>
FaviconURL string // <link rel="icon">
Head string // extra markup for <head> (optional)
}
// Document builds the complete document shell as *Element (the <html> element).
// Does not include <!DOCTYPE html> — use DocumentString for the full output.
func Document(opts DocumentOptions, body ...Component) *Element {
if opts.Lang == "" {
opts.Lang = "en"
}
head := NewElement("head")
head.Child(NewElement("meta").NoCloseTag().Attr("charset", "utf-8"))
if opts.Title != "" {
head.Child(NewElement("title").Text(opts.Title))
}
if opts.FaviconURL != "" {
head.Child(NewElement("link").NoCloseTag().Attr("rel", "icon").Attr("href", opts.FaviconURL))
}
if opts.CSSURL != "" {
head.Child(NewElement("link").NoCloseTag().Attr("rel", "stylesheet").Attr("href", opts.CSSURL))
}
if opts.JSURL != "" {
head.Child(NewElement("script").Attr("src", opts.JSURL).Attr("defer", ""))
}
if opts.Head != "" {
head.Text(opts.Head)
}
bodyEl := NewElement("body").Child(body...)
return NewElement("html").Attr("lang", opts.Lang).Child(head, bodyEl)
}
// DocumentString renders the full HTML document including <!DOCTYPE html>.
// This is what assetmin writes to disk as index.html.
func DocumentString(opts DocumentOptions, body ...Component) string {
return "<!DOCTYPE html>" + Document(opts, body...).String()
}