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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
package alps
import (
"fmt"
"html/template"
"io"
"io/ioutil"
"net/url"
"os"
"strings"
"github.com/labstack/echo/v4"
)
// GlobalRenderData contains data available in all templates.
type GlobalRenderData struct {
Path []string
URL *url.URL
LoggedIn bool
// if logged in
Username string
Title string
HavePlugin func(name string) bool
Notice string
// additional plugin-specific data
Extra map[string]interface{}
}
// BaseRenderData is the base type for templates. It should be extended with
// additional template-specific fields:
//
// type MyRenderData struct {
// BaseRenderData
// // add additional fields here
// }
type BaseRenderData struct {
GlobalData GlobalRenderData
// additional plugin-specific data
Extra map[string]interface{}
}
// Global implements RenderData.
func (brd *BaseRenderData) Global() *GlobalRenderData {
return &brd.GlobalData
}
// RenderData is implemented by template data structs. It can be used to inject
// additional data to all templates.
type RenderData interface {
// GlobalData returns a pointer to the global render data.
Global() *GlobalRenderData
}
// NewBaseRenderData initializes a new BaseRenderData.
//
// It can be used by routes to pre-fill the base data:
//
// type MyRenderData struct {
// BaseRenderData
// // add additional fields here
// }
//
// data := &MyRenderData{
// BaseRenderData: *alps.NewBaseRenderData(ctx),
// // other fields...
// }
func NewBaseRenderData(ectx echo.Context) *BaseRenderData {
ctx, isactx := ectx.(*Context)
global := GlobalRenderData{
Extra: make(map[string]interface{}),
Path: strings.Split(ectx.Request().URL.Path, "/")[1:],
Title: "Webmail",
URL: ectx.Request().URL,
HavePlugin: func(name string) bool {
if !isactx {
return false
}
for _, plugin := range ctx.Server.plugins {
if plugin.Name() == name {
return true
}
}
return false
},
}
if isactx && ctx.Session != nil {
global.LoggedIn = true
global.Username = ctx.Session.username
global.Notice = ctx.Session.PopNotice()
}
return &BaseRenderData{
GlobalData: global,
Extra: make(map[string]interface{}),
}
}
func (brd *BaseRenderData) WithTitle(title string) *BaseRenderData {
brd.GlobalData.Title = title
return brd
}
type renderer struct {
logger echo.Logger
themesPath string
defaultTheme string
base *template.Template
themes map[string]*template.Template
}
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)
var renderData RenderData
if data == nil {
renderData = &struct{ BaseRenderData }{*NewBaseRenderData(ctx)}
} else {
var ok bool
renderData, ok = data.(RenderData)
if !ok {
return fmt.Errorf("data passed to template %q doesn't implement RenderData", name)
}
}
for _, plugin := range ctx.Server.plugins {
if err := plugin.Inject(ctx, name, renderData); err != nil {
return fmt.Errorf("failed to run plugin %q: %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(themesPath string, name string, base *template.Template) (*template.Template, error) {
theme, err := base.Clone()
if err != nil {
return nil, err
}
theme, err = theme.ParseGlob(themesPath + "/" + name + "/*.html")
if err != nil {
return nil, err
}
return theme, nil
}
func (r *renderer) Load(plugins []Plugin) error {
base := template.New("")
for _, p := range plugins {
if err := p.LoadTemplate(base); err != nil {
return fmt.Errorf("failed to load template for plugin %q: %v", p.Name(), err)
}
}
themes := make(map[string]*template.Template)
files, err := ioutil.ReadDir(r.themesPath)
if err != nil && !os.IsNotExist(err) {
return err
}
for _, fi := range files {
if !fi.IsDir() {
continue
}
r.logger.Printf("Loading theme %q", fi.Name())
var err error
if themes[fi.Name()], err = loadTheme(r.themesPath, fi.Name(), base); err != nil {
return fmt.Errorf("failed to load theme %q: %v", fi.Name(), err)
}
}
if r.defaultTheme != "" {
if _, ok := themes[r.defaultTheme]; !ok {
return fmt.Errorf("failed to find default theme %q", r.defaultTheme)
}
}
r.base = base
r.themes = themes
return nil
}
func newRenderer(logger echo.Logger, themesPath string, defaultTheme string) *renderer {
return &renderer{
logger: logger,
defaultTheme: defaultTheme,
themesPath: themesPath,
}
}
|