1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-11-27 03:34:46 +01:00
gojekyll/plugins/plugins.go
2017-07-04 08:06:34 -04:00

81 lines
2.1 KiB
Go

// 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.
package plugins
import (
"fmt"
"github.com/osteele/liquid"
"github.com/osteele/liquid/chunks"
)
// PluginContext is the context for plugin initialization.
// Currently, the only thing a plugin can do is add filters and tags.
type PluginContext interface {
TemplateEngine() liquid.Engine
}
// Install installs a plugin from the plugin directory.
func Install(name string, ctx PluginContext) bool {
p, found := plugins[name]
if p != nil {
if err := p(ctx, pluginHelper{name, ctx}); err != nil {
panic(err)
}
}
return found
}
var plugins = map[string]func(PluginContext, pluginHelper) error{}
// registerPlugin installs a plugin in the plugin directory.
func registerPlugin(name string, fn func(PluginContext, pluginHelper) error) {
plugins[name] = fn
}
func init() {
registerPlugin("jekyll-feed", func(ctx PluginContext, h pluginHelper) error {
h.stubbed()
h.tag("feed_meta", h.makeUnimplementedTag())
return nil
})
// no warning but effect; the server runs in this mode anyway
registerPlugin("jekyll-live-reload", func(ctx PluginContext, h pluginHelper) error {
return nil
})
registerPlugin("jekyll-seo-tag", func(ctx PluginContext, h pluginHelper) error {
h.stubbed()
h.tag("seo", h.makeUnimplementedTag())
return nil
})
}
type pluginHelper struct {
name string
ctx PluginContext
}
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)
}
func (h pluginHelper) tag(name string, r liquid.Renderer) {
h.ctx.TemplateEngine().RegisterTag(name, r)
}
func (h pluginHelper) makeUnimplementedTag() liquid.Renderer {
warned := false
return func(ctx chunks.RenderContext) (string, error) {
if !warned {
fmt.Printf("The %q tag in the %q plugin has not been implemented.\n", ctx.TagName(), h.name)
warned = true
}
return "", nil
}
}