-
Notifications
You must be signed in to change notification settings - Fork 14
/
plugins.go
83 lines (75 loc) · 1.95 KB
/
plugins.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
package site
import (
"bytes"
"io"
"github.com/osteele/gojekyll/pages"
"github.com/osteele/gojekyll/plugins"
"github.com/osteele/gojekyll/utils"
"github.com/osteele/liquid"
"github.com/tdewolff/minify"
"github.com/tdewolff/minify/html"
)
// AddHTMLPage is in the plugins.Site interface.
func (s *Site) AddHTMLPage(url string, src string, fm pages.FrontMatter) {
tpl, err := s.TemplateEngine().ParseTemplate([]byte(src))
if err != nil {
panic(err)
}
d := &templateDoc{pages.PageEmbed{Path: url}, s, tpl}
s.AddDocument(d, true)
}
func (s *Site) installPlugins() error {
s.plugins = s.cfg.Plugins
installed := utils.StringSet{}
// Install plugins and call their ModifyPluginList methods.
// Repeat until no plugins have been added.
for len(s.plugins) > len(installed) {
// Collect plugins into a list instead of map, in order to preserve order
pending := utils.StringList(s.plugins).Reject(installed.Contains)
if err := plugins.Install(pending, s); err != nil {
return err
}
for _, name := range pending {
p, ok := plugins.Lookup(name)
if ok {
s.plugins = p.ModifyPluginList(s.plugins)
}
}
installed.AddStrings(pending)
}
return nil
}
func (s *Site) runHooks(h func(plugins.Plugin) error) error {
for _, name := range s.plugins {
p, ok := plugins.Lookup(name)
if ok {
if err := h(p); err != nil {
return utils.WrapError(err, "running plugin")
}
}
}
return nil
}
type templateDoc struct {
pages.PageEmbed
site *Site
tpl *liquid.Template
}
func (d *templateDoc) Content() string {
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.String()
}
func (d *templateDoc) Write(w io.Writer) error {
_, err := io.WriteString(w, d.Content())
return err
}