Browse Source

Allow plugins to provide their own templates

master
Simon Ser 4 years ago
parent
commit
1b5bc568fb
No known key found for this signature in database GPG Key ID: FDE7BE0E88F5E48
4 changed files with 27 additions and 8 deletions
  1. +3
    -1
      README.md
  2. +3
    -1
      plugin.go
  3. +15
    -3
      plugin_lua.go
  4. +6
    -3
      template.go

+ 3
- 1
README.md View File

@@ -18,7 +18,7 @@ HTTP server at `themes/<name>/assets/*`.

## Plugins

Lua plugins are supported. They can be dropped in `plugins/*.lua`.
Lua plugins are supported. They can be dropped in `plugins/<name>/main.lua`.

API:

@@ -28,6 +28,8 @@ API:
* `koushin.set_route(method, path, f)`: register a new HTTP route, `f` will be
called with the HTTP context

Plugins can provide their own templates in `plugins/<name>/public/*.html`.

## Contributing

Send patches [on the mailing list](https://lists.sr.ht/~sircmpwn/koushin),


+ 3
- 1
plugin.go View File

@@ -6,9 +6,11 @@ import (
"github.com/labstack/echo/v4"
)

const pluginDir = "plugins"

type Plugin interface {
Name() string
Filters() template.FuncMap
LoadTemplate(t *template.Template) error
SetRoutes(group *echo.Group)
Inject(name string, data interface{}) error
Close() error


+ 15
- 3
plugin_lua.go View File

@@ -86,8 +86,20 @@ func (p *luaPlugin) Inject(name string, data interface{}) error {
return nil
}

func (p *luaPlugin) Filters() template.FuncMap {
return p.filters
func (p *luaPlugin) LoadTemplate(t *template.Template) error {
t.Funcs(p.filters)

paths, err := filepath.Glob(filepath.Dir(p.filename) + "/public/*.html")
if err != nil {
return err
}
if len(paths) > 0 {
if _, err := t.ParseFiles(paths...); err != nil {
return err
}
}

return nil
}

func (p *luaPlugin) SetRoutes(group *echo.Group) {
@@ -136,7 +148,7 @@ func loadLuaPlugin(filename string) (*luaPlugin, error) {
}

func loadAllLuaPlugins(log echo.Logger) ([]Plugin, error) {
filenames, err := filepath.Glob("plugins/*.lua")
filenames, err := filepath.Glob(pluginDir + "/*/main.lua")
if err != nil {
return nil, fmt.Errorf("filepath.Glob failed: %v", err)
}


+ 6
- 3
template.go View File

@@ -92,15 +92,18 @@ func loadTemplates(logger echo.Logger, defaultTheme string, plugins []Plugin) (*
return url.PathEscape(s)
},
})
for _, p := range plugins {
base = base.Funcs(p.Filters())
}

base, err := base.ParseGlob("public/*.html")
if err != nil {
return nil, err
}

for _, p := range plugins {
if err := p.LoadTemplate(base); err != nil {
return nil, fmt.Errorf("failed to load template for plugin '%v': %v", p.Name(), err)
}
}

themes := make(map[string]*template.Template)

files, err := ioutil.ReadDir(themesDir)


Loading…
Cancel
Save