1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-11-27 05:54:44 +01:00
gojekyll/plugins/plugins.go

81 lines
2.1 KiB
Go
Raw Normal View History

2017-07-01 05:10:58 +02:00
// Package plugins holds emulated Jekyll plugins.
//
// Unlike Jekyll, these are baked into the executable -- both because as of 2017.07 package "plugin' currently
// works only on Linux, but also because the gojekyll implementation is immature and any possible interfaces
// are far from baked.
2017-06-30 18:53:34 +02:00
package plugins
import (
"fmt"
2017-07-01 01:37:31 +02:00
"github.com/osteele/liquid"
2017-06-30 18:53:34 +02:00
"github.com/osteele/liquid/chunks"
)
2017-07-01 05:10:58 +02:00
// PluginContext is the context for plugin initialization.
// Currently, the only thing a plugin can do is add filters and tags.
2017-06-30 18:53:34 +02:00
type PluginContext interface {
TemplateEngine() liquid.Engine
}
2017-07-01 05:10:58 +02:00
// Install installs a plugin from the plugin directory.
2017-06-30 18:53:34 +02:00
func Install(name string, ctx PluginContext) bool {
p, found := plugins[name]
if p != nil {
2017-07-02 05:06:47 +02:00
if err := p(ctx, pluginHelper{name, ctx}); err != nil {
2017-06-30 18:53:34 +02:00
panic(err)
}
}
return found
}
2017-07-01 16:37:11 +02:00
var plugins = map[string]func(PluginContext, pluginHelper) error{}
2017-06-30 18:53:34 +02:00
2017-07-01 05:10:58 +02:00
// registerPlugin installs a plugin in the plugin directory.
2017-07-01 16:37:11 +02:00
func registerPlugin(name string, fn func(PluginContext, pluginHelper) error) {
2017-06-30 18:53:34 +02:00
plugins[name] = fn
}
func init() {
2017-07-01 16:37:11 +02:00
registerPlugin("jekyll-feed", func(ctx PluginContext, h pluginHelper) error {
h.stubbed()
2017-07-02 05:06:47 +02:00
h.tag("feed_meta", h.makeUnimplementedTag())
2017-06-30 18:53:34 +02:00
return nil
})
2017-07-02 05:06:47 +02:00
// no warning but effect; the server runs in this mode anyway
2017-07-01 19:34:32 +02:00
registerPlugin("jekyll-live-reload", func(ctx PluginContext, h pluginHelper) error {
return nil
})
2017-07-01 16:37:11 +02:00
registerPlugin("jekyll-seo-tag", func(ctx PluginContext, h pluginHelper) error {
h.stubbed()
2017-07-02 05:06:47 +02:00
h.tag("seo", h.makeUnimplementedTag())
2017-06-30 18:53:34 +02:00
return nil
})
}
2017-07-01 16:37:11 +02:00
2017-07-02 05:06:47 +02:00
type pluginHelper struct {
name string
ctx PluginContext
}
2017-07-01 16:37:11 +02:00
func (h pluginHelper) stubbed() {
fmt.Printf("warning: gojekyll does not emulate the %s plugin. Some tags have been stubbed to prevent errors.\n", h.name)
}
2017-07-04 14:06:34 +02:00
func (h pluginHelper) tag(name string, r liquid.Renderer) {
h.ctx.TemplateEngine().RegisterTag(name, r)
2017-07-02 05:06:47 +02:00
}
2017-07-04 14:06:34 +02:00
func (h pluginHelper) makeUnimplementedTag() liquid.Renderer {
2017-07-01 16:37:11 +02:00
warned := false
2017-07-04 14:06:34 +02:00
return func(ctx chunks.RenderContext) (string, error) {
2017-07-01 16:37:11 +02:00
if !warned {
fmt.Printf("The %q tag in the %q plugin has not been implemented.\n", ctx.TagName(), h.name)
warned = true
}
2017-07-04 14:06:34 +02:00
return "", nil
2017-07-01 16:37:11 +02:00
}
}