-
Notifications
You must be signed in to change notification settings - Fork 1
/
document.go
51 lines (45 loc) · 1.09 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
package domino
import (
"bytes"
)
// Document is a convenience type to help build a standard HTML5 document. It
// provides a doctype, and header/title/body structure.
type Document struct {
Html *DomNode
Head *DomNode
Body *DomNode
title *DomNode
}
// NewDocument creates a new HTML5 document with a title using the text provided
func NewDocument(title string) *Document {
d := &Document{
Html: Html(),
Head: Head(),
Body: Body(),
title: Title(),
}
d.Html.Add(d.Head)
d.Html.Add(d.Body)
d.Head.Add(d.title)
d.SetTitle(title)
return d
}
// SetTitle sets the title of the document.
func (d *Document) SetTitle(title string) {
d.title.Clear()
d.title.Text(title)
}
// String returns the HTML for the document.
func (d *Document) String() string {
b := &bytes.Buffer{}
b.WriteString("<!DOCTYPE html>\n")
d.Html.StringBuild(b, false, 0)
return b.String()
}
// IndentString returns the HTML for the document with proper formatting.
func (d *Document) IndentString() string {
b := &bytes.Buffer{}
b.WriteString("<!DOCTYPE html>\n")
d.Html.StringBuild(b, true, 0)
return b.String()
}