-
Notifications
You must be signed in to change notification settings - Fork 2
/
html.go
57 lines (49 loc) · 1.34 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
package main
import (
"bytes"
"encoding/xml"
"io"
"text/template"
)
// Only do this when NOT testing
func loadBaseTemplate() {
var err error
// baseTemplate = template.New("base")
// Provide a list of custom/helper functions to the template rendering process.
// See funcs.go
baseTemplate, err = template.New("base.html").Funcs(templateFuncs).ParseFiles(baseFile)
if err != nil {
die("Failed to open base file template %s %s", baseFile, err)
}
}
// TODO might not be used anymore, should be called execTemplate really
// Will simply render the base template with {{.page}} var
// set to the contents of the current page being rendered or
// displayed localally
func renderHTML(data map[string]interface{}) []byte {
buf := &bytes.Buffer{}
err := baseTemplate.Execute(buf, data)
if err != nil {
die("Failed to render template: %s", err)
}
return buf.Bytes()
}
// TODO find some better lib for this crap srsly
func validateHTML(page []byte) error {
buf := bytes.NewBuffer(page)
dec := xml.NewDecoder(buf)
// Configure the decoder for HTML; leave off strict and autoclose for XHTML
dec.Strict = false
dec.AutoClose = xml.HTMLAutoClose
dec.Entity = xml.HTMLEntity
for {
_, err := dec.Token()
switch err {
case io.EOF:
return nil // We're done, it's valid!
case nil:
default:
return err // Oops, something wasn't right
}
}
}