1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-12-03 13:47:49 +01:00
gojekyll/plugins/template_doc.go

46 lines
954 B
Go
Raw Normal View History

2017-07-12 15:52:40 +02:00
package plugins
import (
2017-07-26 17:46:37 +02:00
"bytes"
2017-07-12 15:52:40 +02:00
"io"
2017-07-25 17:24:37 +02:00
"github.com/osteele/gojekyll/pages"
2017-07-12 15:52:40 +02:00
"github.com/osteele/liquid"
2017-07-26 17:46:37 +02:00
"github.com/tdewolff/minify"
"github.com/tdewolff/minify/html"
2017-07-12 15:52:40 +02:00
)
2017-07-25 17:24:37 +02:00
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}
}
2017-07-12 15:52:40 +02:00
type templateDoc struct {
2017-07-25 17:24:37 +02:00
pages.PageEmbed
2017-07-12 15:52:40 +02:00
site Site
tpl *liquid.Template
}
2017-08-10 15:06:53 +02:00
func (d *templateDoc) Content() string {
2017-07-12 15:52:40 +02:00
bindings := map[string]interface{}{"site": d.site}
b, err := d.tpl.Render(bindings)
if err != nil {
panic(err)
}
2017-07-26 17:46:37 +02:00
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)
}
2017-08-10 15:06:53 +02:00
return min.String()
2017-07-12 15:52:40 +02:00
}
func (d *templateDoc) Write(w io.Writer) error {
2017-08-10 15:06:53 +02:00
_, err := io.WriteString(w, d.Content())
2017-07-12 15:52:40 +02:00
return err
}