-
Notifications
You must be signed in to change notification settings - Fork 13
/
template_doc.go
45 lines (39 loc) · 943 Bytes
/
template_doc.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
package plugins
import (
"bytes"
"io"
"github.com/osteele/gojekyll/pages"
"github.com/osteele/liquid"
"github.com/tdewolff/minify"
"github.com/tdewolff/minify/html"
)
func newTemplateDoc(s Site, path, src string) pages.Document {
tpl, err := s.TemplateEngine().ParseTemplate([]byte(src))
if err != nil {
panic(err)
}
return &templateDoc{pages.PageEmbed{Path: path}, s, tpl}
}
type templateDoc struct {
pages.PageEmbed
site Site
tpl *liquid.Template
}
func (d *templateDoc) Content() []byte {
bindings := map[string]interface{}{"site": d.site}
b, err := d.tpl.Render(bindings)
if err != nil {
panic(err)
}
m := minify.New()
m.AddFunc("text/html", html.Minify)
min := bytes.NewBuffer(make([]byte, 0, len(b)))
if err := m.Minify("text/html", min, bytes.NewBuffer(b)); err != nil {
panic(err)
}
return min.Bytes()
}
func (d *templateDoc) Write(w io.Writer) error {
_, err := w.Write(d.Content())
return err
}