1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
package koushin
import (
"fmt"
"html/template"
"io"
"io/ioutil"
"net/url"
"os"
"github.com/labstack/echo/v4"
)
const themesDir = "public/themes"
// GlobalRenderData contains data available in all templates.
type GlobalRenderData struct {
LoggedIn bool
// if logged in
Username string
// TODO: list of mailboxes
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
Extra map[string]interface{}
}
func NewRenderData(ctx *context) *RenderData {
global := GlobalRenderData{Extra: make(map[string]interface{})}
if ctx.session != nil {
global.LoggedIn = true
global.Username = ctx.session.username
}
return &RenderData{
Global: global,
Extra: make(map[string]interface{}),
}
}
type renderer struct {
base *template.Template
themes map[string]*template.Template
defaultTheme string
}
func (r *renderer) Render(w io.Writer, name string, data interface{}, ectx echo.Context) error {
// ectx is the raw *echo.context, not our own *context
ctx := ectx.Get("context").(*context)
for _, plugin := range ctx.server.plugins {
if err := plugin.Inject(name, data); err != nil {
return fmt.Errorf("failed to run plugin '%v': %v", plugin.Name(), err)
}
}
// TODO: per-user theme selection
t := r.base
if r.defaultTheme != "" {
t = r.themes[r.defaultTheme]
}
return t.ExecuteTemplate(w, name, data)
}
func loadTheme(name string, base *template.Template) (*template.Template, error) {
theme, err := base.Clone()
if err != nil {
return nil, err
}
theme, err = theme.ParseGlob("public/themes/" + name + "/*.html")
if err != nil {
return nil, err
}
return theme, nil
}
func loadTemplates(logger echo.Logger, defaultTheme string, plugins []Plugin) (*renderer, error) {
base := template.New("").Funcs(template.FuncMap{
"tuple": func(values ...interface{}) []interface{} {
return values
},
"pathescape": func(s string) string {
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
}
themes := make(map[string]*template.Template)
files, err := ioutil.ReadDir(themesDir)
if err != nil && !os.IsNotExist(err) {
return nil, err
}
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)
}
}
return &renderer{
base: base,
themes: themes,
defaultTheme: defaultTheme,
}, nil
}
|