-
Notifications
You must be signed in to change notification settings - Fork 2
/
templates.go
426 lines (371 loc) · 10.5 KB
/
templates.go
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
package main
import (
"fmt"
"html/template"
"io/fs"
"log"
"net/http"
"time"
"github.com/gorilla/csrf"
"github.com/mdbot/wiki/config"
"github.com/sergi/go-diff/diffmatchpatch"
)
type Templates struct {
fs fs.FS
siteConfig *config.Site
checker *PermissionChecker
version string
sidebarProvider func() string
}
type SiteArgs struct {
SiteName string
HasMainLogo bool
HasDarkLogo bool
HasFavicon bool
CanRead bool
CanWrite bool
CanAdmin bool
WikiVersion string
}
type CommonArgs struct {
Site *SiteArgs
RequestedUrl string
PageTitle string
IsWikiPage bool
IsError bool
Error string
Notice string
ShowLinkToView bool
Sidebar template.HTML
User *config.User
LastModified *LastModifiedDetails
CsrfField template.HTML
}
type LastModifiedDetails struct {
User string
Time time.Time
}
type ViewPageArgs struct {
Common CommonArgs
PageContent template.HTML
}
func (t *Templates) RenderPage(w http.ResponseWriter, r *http.Request, title, content string, log *LastModifiedDetails) {
t.render("index.gohtml", http.StatusOK, w, &ViewPageArgs{
Common: t.populateArgs(w, r, CommonArgs{
PageTitle: title,
IsWikiPage: true,
LastModified: log,
}),
PageContent: template.HTML(content),
})
}
type EditPageArgs struct {
Common CommonArgs
PageContent string
}
func (t *Templates) RenderEditPage(w http.ResponseWriter, r *http.Request, title, content string) {
t.render("edit.gohtml", http.StatusOK, w, &EditPageArgs{
Common: t.populateArgs(w, r, CommonArgs{
PageTitle: title,
ShowLinkToView: true,
}),
PageContent: content,
})
}
type DeletePageArgs struct {
Common CommonArgs
}
func (t *Templates) RenderDeletePage(w http.ResponseWriter, r *http.Request, pageName string) {
t.render("delete.gohtml", http.StatusOK, w, &DeletePageArgs{
Common: t.populateArgs(w, r, CommonArgs{
PageTitle: pageName,
ShowLinkToView: true,
}),
})
}
type RevertPageArgs struct {
Common CommonArgs
Revision string
}
func (t *Templates) RenderRevertPage(w http.ResponseWriter, r *http.Request, pageName, revision string) {
t.render("revert.gohtml", http.StatusOK, w, &RevertPageArgs{
Common: t.populateArgs(w, r, CommonArgs{
PageTitle: pageName,
ShowLinkToView: true,
}),
Revision: revision,
})
}
type RenamePageArgs struct {
Common CommonArgs
}
func (t *Templates) RenderRenamePage(w http.ResponseWriter, r *http.Request, oldName string) {
t.render("rename.gohtml", http.StatusOK, w, &RenamePageArgs{
Common: t.populateArgs(w, r, CommonArgs{
PageTitle: oldName,
ShowLinkToView: true,
}),
})
}
type ListPagesArgs struct {
Common CommonArgs
Pages []string
}
func (t *Templates) RenderPageList(w http.ResponseWriter, r *http.Request, pages []string) {
t.render("list.gohtml", http.StatusOK, w, &ListPagesArgs{
Common: t.populateArgs(w, r, CommonArgs{
PageTitle: "Pages",
}),
Pages: pages,
})
}
type ListFilesArgs struct {
Common CommonArgs
Files []File
}
func (t *Templates) RenderFileList(w http.ResponseWriter, r *http.Request, files []File) {
t.render("listfiles.gohtml", http.StatusOK, w, &ListFilesArgs{
Common: t.populateArgs(w, r, CommonArgs{
PageTitle: "Files",
}),
Files: files,
})
}
type DeleteFileArgs struct {
Common CommonArgs
}
func (t *Templates) RenderDeleteFile(w http.ResponseWriter, r *http.Request, fileName string) {
t.render("delete_file.gohtml", http.StatusOK, w, &DeleteFileArgs{
Common: t.populateArgs(w, r, CommonArgs{
PageTitle: fileName,
}),
})
}
type UploadFileArgs struct {
Common CommonArgs
}
func (t *Templates) RenderUploadForm(w http.ResponseWriter, r *http.Request) {
t.render("upload.gohtml", http.StatusOK, w, &UploadFileArgs{
Common: t.populateArgs(w, r, CommonArgs{
PageTitle: "Upload file",
}),
})
}
type HistoryPageArgs struct {
Common CommonArgs
History []*HistoryEntry
Next string
}
type HistoryEntry struct {
ChangeId string
PreviousChangeId string
Latest bool
User string
Time time.Time
Message string
}
func (t *Templates) RenderHistory(w http.ResponseWriter, r *http.Request, title string, entries []*HistoryEntry, next string) {
t.render("history.gohtml", http.StatusOK, w, &HistoryPageArgs{
Common: t.populateArgs(w, r, CommonArgs{
PageTitle: title,
IsWikiPage: true,
ShowLinkToView: true,
}),
History: entries,
Next: next,
})
}
type RecentChangesArgs struct {
Common CommonArgs
Changes []*RecentChange
Next string
}
func (t *Templates) RenderRecentChanges(w http.ResponseWriter, r *http.Request, entries []*RecentChange, next string) {
t.render("changes.gohtml", http.StatusOK, w, &RecentChangesArgs{
Common: t.populateArgs(w, r, CommonArgs{
PageTitle: "Recent changes",
}),
Changes: entries,
Next: next,
})
}
func (t *Templates) RenderRecentChangesFeed(w http.ResponseWriter, r *http.Request, entries []*RecentChange) {
w.Header().Set("Content-Type", "application/rss+xml")
t.render("changes.goxml", http.StatusOK, w, &RecentChangesArgs{
Common: t.populateArgs(w, r, CommonArgs{
PageTitle: "Recent changes",
}),
Changes: entries,
})
}
type ManageUsersArgs struct {
Common CommonArgs
Users []UserInfo
}
type UserInfo struct {
Name string
Permissions string
}
func (t *Templates) RenderManageUsers(w http.ResponseWriter, r *http.Request, users []UserInfo) {
t.render("users.gohtml", http.StatusOK, w, &ManageUsersArgs{
Common: t.populateArgs(w, r, CommonArgs{
PageTitle: "Manage users",
}),
Users: users,
})
}
type ViewSiteArgs struct {
Common CommonArgs
}
func (t *Templates) RenderViewSiteConfig(w http.ResponseWriter, r *http.Request) {
t.render("siteconfig.gohtml", http.StatusOK, w, &ViewSiteArgs{
Common: t.populateArgs(w, r, CommonArgs{
PageTitle: "Manage site",
}),
})
}
type AccountArgs struct {
Common CommonArgs
}
func (t *Templates) RenderAccount(w http.ResponseWriter, r *http.Request) {
t.render("account.gohtml", http.StatusOK, w, &AccountArgs{
Common: t.populateArgs(w, r, CommonArgs{
PageTitle: "My account",
}),
})
}
type ErrorPageArgs struct {
Common CommonArgs
ShowLoginForm bool
OldPageTitle string
}
func (t *Templates) RenderNotFound(w http.ResponseWriter, r *http.Request, isWiki bool, pageName string) {
// The built in error handler sets text/plain, so make sure we're not passing that on
w.Header().Del("Content-type")
t.render("404.gohtml", http.StatusNotFound, w, &ErrorPageArgs{
Common: t.populateArgs(w, r, CommonArgs{
PageTitle: "Page not found",
IsWikiPage: isWiki,
IsError: true,
}),
OldPageTitle: pageName,
})
}
func (t *Templates) RenderUnauthorised(w http.ResponseWriter, r *http.Request) {
// The built in error handler sets text/plain, so make sure we're not passing that on
w.Header().Del("Content-type")
t.render("error.gohtml", http.StatusUnauthorized, w, &ErrorPageArgs{
Common: t.populateArgs(w, r, CommonArgs{
PageTitle: "Unauthorized",
IsError: true,
}),
ShowLoginForm: true,
})
}
func (t *Templates) RenderForbidden(w http.ResponseWriter, r *http.Request) {
// The built in error handler sets text/plain, so make sure we're not passing that on
w.Header().Del("Content-type")
t.render("error.gohtml", http.StatusForbidden, w, &ErrorPageArgs{
Common: t.populateArgs(w, r, CommonArgs{
PageTitle: "Forbidden",
IsError: true,
}),
})
}
func (t *Templates) RenderInternalError(w http.ResponseWriter, r *http.Request) {
// The built in error handler sets text/plain, so make sure we're not passing that on
w.Header().Del("Content-type")
t.render("error.gohtml", http.StatusInternalServerError, w, &ErrorPageArgs{
Common: t.populateArgs(w, r, CommonArgs{
PageTitle: "Server Error",
IsError: true,
}),
})
}
func (t *Templates) RenderBadRequest(w http.ResponseWriter, r *http.Request) {
// The built in error handler sets text/plain, so make sure we're not passing that on
w.Header().Del("Content-type")
t.render("error.gohtml", http.StatusInternalServerError, w, &ErrorPageArgs{
Common: t.populateArgs(w, r, CommonArgs{
PageTitle: "Bad Request",
IsError: true,
}),
})
}
type SearchPageArgs struct {
Common CommonArgs
Results []SearchResult
Pattern string
}
func (t *Templates) RenderSearch(w http.ResponseWriter, r *http.Request, pattern string, results []SearchResult) {
t.render("search.gohtml", http.StatusOK, w, &SearchPageArgs{
Common: t.populateArgs(w, r, CommonArgs{
PageTitle: "Search",
}),
Results: results,
Pattern: pattern,
})
}
type DiffPageArgs struct {
Common CommonArgs
Diff []diffmatchpatch.Diff
}
func (t *Templates) RenderDiff(w http.ResponseWriter, r *http.Request, diff []diffmatchpatch.Diff) {
t.render("diff.gohtml", http.StatusOK, w, &DiffPageArgs{
Common: t.populateArgs(w, r, CommonArgs{
PageTitle: "Diff",
}),
Diff: diff,
})
}
func (t *Templates) render(name string, statusCode int, w http.ResponseWriter, data interface{}) {
w.WriteHeader(statusCode)
tpl := template.New(name)
tpl.Funcs(map[string]interface{}{
"bytes": t.formatBytes,
"unsafeHtml": func(html string) template.HTML {
return template.HTML(html)
},
})
template.Must(tpl.ParseFS(t.fs, name, "partials/*.gohtml"))
if err := tpl.Execute(w, data); err != nil {
// TODO: We should probably send an error to the client
log.Printf("Error rendering template: %v\n", err)
}
}
func (t *Templates) formatBytes(size int64) string {
const multiple = 1024
if size < multiple {
return fmt.Sprintf("%d B", size)
}
denominator, power := int64(multiple), 0
for n := size / multiple; n >= multiple; n /= multiple {
denominator *= multiple
power++
}
return fmt.Sprintf("%.1f %ciB", float64(size)/float64(denominator), "KMGTPE"[power])
}
func (t *Templates) populateArgs(w http.ResponseWriter, r *http.Request, args CommonArgs) CommonArgs {
user := getUserForRequest(r)
args.Site = &SiteArgs{
SiteName: t.siteConfig.Name,
HasMainLogo: t.siteConfig.MainLogo != nil,
HasDarkLogo: t.siteConfig.DarkLogo != nil,
HasFavicon: t.siteConfig.Favicon != nil,
CanRead: t.checker.CanRead(user),
CanWrite: t.checker.CanWrite(user),
CanAdmin: t.checker.CanAdmin(user),
WikiVersion: t.version,
}
args.User = user
if args.Error = getErrorForRequest(r); args.Error != "" {
clearSessionKey(w, r, sessionErrorKey)
}
if args.Notice = getNoticeForRequest(r); args.Notice != "" {
clearSessionKey(w, r, sessionNoticeKey)
}
args.CsrfField = csrf.TemplateField(r)
args.RequestedUrl = r.URL.String()
args.Sidebar = template.HTML(t.sidebarProvider())
return args
}