-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocument.go
97 lines (75 loc) · 1.68 KB
/
document.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 dhtml
import (
"fmt"
"slices"
)
// HTML Document complex helper
type HtmlDocument struct {
body *Tag
head *Tag
//metadata for head
stylesheets []string
charset string
title string
icon string
}
// force interfaces implementation
var _ fmt.Stringer = (*HtmlDocument)(nil)
var _ ElementI = (*HtmlDocument)(nil)
func NewHtmlDocument() *HtmlDocument {
return &HtmlDocument{
head: NewTag("head"),
body: NewTag("body"),
charset: "utf-8",
}
}
func (d *HtmlDocument) Body() *Tag {
return d.body
}
func (d *HtmlDocument) Head() *Tag {
return d.head
}
func (d *HtmlDocument) Charset(charset string) *HtmlDocument {
d.charset = charset
return d
}
func (d *HtmlDocument) Title(title string) *HtmlDocument {
d.title = title
return d
}
func (d *HtmlDocument) Icon(icon string) *HtmlDocument {
d.icon = icon
return d
}
func (d *HtmlDocument) Stylesheet(href string) *HtmlDocument {
if d.stylesheets == nil {
d.stylesheets = make([]string, 0)
}
if !slices.Contains(d.stylesheets, href) {
d.stylesheets = append(d.stylesheets, href)
d.Head().Append(
NewTag("link").
Attribute("href", href).Attribute("rel", "stylesheet"),
)
}
return d
}
func (d *HtmlDocument) GetTags() TagList {
head := d.Head()
if d.charset != "" {
head.Append(NewTag("meta").Attribute("charset", d.charset))
}
if d.title != "" {
head.Append(NewTag("title").Text(d.title))
}
if d.icon != "" {
head.Append(NewTag("link").Attribute("rel", "icon").Attribute("href", d.icon))
}
root := NewTag("html").
Append(head).
Append(d.Body())
return TagList{root}
}
func (d *HtmlDocument) String() string {
return "<!DOCTYPE html>\n" + d.GetTags()[0].String()
}