2019-12-02 14:31:00 +00:00
|
|
|
package koushin
|
|
|
|
|
|
|
|
import (
|
2019-12-09 15:02:12 +00:00
|
|
|
"fmt"
|
2019-12-02 14:31:00 +00:00
|
|
|
"html/template"
|
|
|
|
"io"
|
2019-12-10 15:41:56 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2019-12-02 14:31:00 +00:00
|
|
|
|
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
)
|
|
|
|
|
2019-12-16 11:57:30 +00:00
|
|
|
const themesDir = "themes"
|
2019-12-10 15:41:56 +00:00
|
|
|
|
2019-12-10 16:36:21 +00:00
|
|
|
// GlobalRenderData contains data available in all templates.
|
|
|
|
type GlobalRenderData struct {
|
|
|
|
LoggedIn bool
|
|
|
|
|
|
|
|
// if logged in
|
|
|
|
Username string
|
|
|
|
// TODO: list of mailboxes
|
|
|
|
|
2019-12-11 14:24:39 +00:00
|
|
|
// Additional plugin-specific data
|
2019-12-10 16:36:21 +00:00
|
|
|
Extra map[string]interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RenderData is the base type for templates. It should be extended with new
|
|
|
|
// template-specific fields.
|
|
|
|
type RenderData struct {
|
|
|
|
Global GlobalRenderData
|
2019-12-11 14:24:39 +00:00
|
|
|
// Additional plugin-specific data
|
|
|
|
Extra map[string]interface{}
|
2019-12-10 16:36:21 +00:00
|
|
|
}
|
|
|
|
|
2019-12-11 11:48:18 +00:00
|
|
|
func NewRenderData(ctx *Context) *RenderData {
|
2019-12-10 16:36:21 +00:00
|
|
|
global := GlobalRenderData{Extra: make(map[string]interface{})}
|
|
|
|
|
2019-12-11 11:48:18 +00:00
|
|
|
if ctx.Session != nil {
|
2019-12-10 16:36:21 +00:00
|
|
|
global.LoggedIn = true
|
2019-12-11 11:48:18 +00:00
|
|
|
global.Username = ctx.Session.username
|
2019-12-10 16:36:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &RenderData{
|
|
|
|
Global: global,
|
2019-12-10 16:39:42 +00:00
|
|
|
Extra: make(map[string]interface{}),
|
2019-12-10 16:36:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-10 15:41:56 +00:00
|
|
|
type renderer struct {
|
2019-12-10 16:39:42 +00:00
|
|
|
base *template.Template
|
|
|
|
themes map[string]*template.Template
|
2019-12-10 15:41:56 +00:00
|
|
|
defaultTheme string
|
2019-12-02 14:31:00 +00:00
|
|
|
}
|
|
|
|
|
2019-12-10 15:41:56 +00:00
|
|
|
func (r *renderer) Render(w io.Writer, name string, data interface{}, ectx echo.Context) error {
|
2019-12-11 11:48:18 +00:00
|
|
|
// ectx is the raw *echo.context, not our own *Context
|
|
|
|
ctx := ectx.Get("context").(*Context)
|
2019-12-09 15:02:12 +00:00
|
|
|
|
2019-12-11 12:03:31 +00:00
|
|
|
for _, plugin := range ctx.Server.Plugins {
|
2019-12-10 15:07:02 +00:00
|
|
|
if err := plugin.Inject(name, data); err != nil {
|
2019-12-09 15:02:12 +00:00
|
|
|
return fmt.Errorf("failed to run plugin '%v': %v", plugin.Name(), err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-10 15:41:56 +00:00
|
|
|
// TODO: per-user theme selection
|
|
|
|
t := r.base
|
|
|
|
if r.defaultTheme != "" {
|
|
|
|
t = r.themes[r.defaultTheme]
|
|
|
|
}
|
|
|
|
return t.ExecuteTemplate(w, name, data)
|
2019-12-02 14:31:00 +00:00
|
|
|
}
|
|
|
|
|
2019-12-10 15:41:56 +00:00
|
|
|
func loadTheme(name string, base *template.Template) (*template.Template, error) {
|
|
|
|
theme, err := base.Clone()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-12-16 11:57:30 +00:00
|
|
|
theme, err = theme.ParseGlob(themesDir + "/" + name + "/*.html")
|
2019-12-10 15:41:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return theme, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadTemplates(logger echo.Logger, defaultTheme string, plugins []Plugin) (*renderer, error) {
|
2019-12-16 11:51:42 +00:00
|
|
|
base := template.New("")
|
2019-12-04 17:30:01 +00:00
|
|
|
|
2019-12-11 14:08:31 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-10 15:41:56 +00:00
|
|
|
themes := make(map[string]*template.Template)
|
|
|
|
|
|
|
|
files, err := ioutil.ReadDir(themesDir)
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
2019-12-04 17:30:01 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-12-10 15:41:56 +00:00
|
|
|
for _, fi := range files {
|
|
|
|
if !fi.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Printf("Loading theme '%v'", fi.Name())
|
|
|
|
var err error
|
|
|
|
if themes[fi.Name()], err = loadTheme(fi.Name(), base); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to load theme '%v': %v", fi.Name(), err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if defaultTheme != "" {
|
|
|
|
if _, ok := themes[defaultTheme]; !ok {
|
|
|
|
return nil, fmt.Errorf("failed to find default theme '%v'", defaultTheme)
|
2019-12-04 17:30:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-10 15:41:56 +00:00
|
|
|
return &renderer{
|
2019-12-10 16:39:42 +00:00
|
|
|
base: base,
|
|
|
|
themes: themes,
|
2019-12-10 15:41:56 +00:00
|
|
|
defaultTheme: defaultTheme,
|
|
|
|
}, nil
|
2019-12-02 14:31:00 +00:00
|
|
|
}
|