1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-12-02 23:17:46 +01:00
gojekyll/plugins/plugins.go
2017-07-02 13:34:43 -04:00

82 lines
2.2 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"
"io"
"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, t liquid.TagDefinition) {
h.ctx.TemplateEngine().RegisterTag(name, t)
}
func (h pluginHelper) makeUnimplementedTag() liquid.TagDefinition {
warned := false
return func(_ io.Writer, ctx chunks.RenderContext) 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
}
}