-
Notifications
You must be signed in to change notification settings - Fork 19
/
template.go
executable file
·417 lines (397 loc) · 10.6 KB
/
template.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
package main
import (
"fmt"
"html"
"os"
"path"
"reflect"
"strconv"
"strings"
"text/template"
x_html "golang.org/x/net/html"
)
var funcMap = template.FuncMap{
// Arithmetic functions
"add": func(a, b int) int {
return a + b
},
"subtract": func(a, b int) int {
return a - b
},
// Comparison functions (some copied from text/template for compatibility)
"ge": func(a int, b int) bool {
return a >= b
},
"gt": func(a int, b int) bool {
return a > b
},
"le": func(a int, b int) bool {
return a <= b
},
"lt": func(a int, b int) bool {
return a < b
},
"intEq": func(a, b int) bool {
return a == b
},
"isNil": func(i interface{}) bool {
return i == nil
},
// Array functions
"getSlice": func(arr []interface{}, start, length int) []interface{} {
if start < 0 {
start = 0
}
if length > len(arr) {
length = len(arr)
}
return arr[start:length]
},
"len": func(arr []interface{}) int {
return len(arr)
},
// String functions
"arrToString": arrToString,
"intToString": strconv.Itoa,
"escapeString": func(a string) string {
return html.EscapeString(a)
},
"formatFilesize": func(sizeInt int) string {
size := float32(sizeInt)
if size < 1000 {
return fmt.Sprintf("%d B", sizeInt)
} else if size <= 100000 {
return fmt.Sprintf("%0.1f KB", size/1024)
} else if size <= 100000000 {
return fmt.Sprintf("%0.2f MB", size/1024/1024)
}
return fmt.Sprintf("%0.2f GB", size/1024/1024/1024)
},
"formatTimestamp": humanReadableTime,
"printf": func(v int, format string, a ...interface{}) string {
printf(v, format, a...)
return ""
},
"println": func(v int, i ...interface{}) string {
println(v, i...)
return ""
},
"stringAppend": func(strings ...string) string {
var appended string
for _, str := range strings {
appended += str
}
return appended
},
"truncateMessage": func(msg string, limit int, maxLines int) string {
var truncated bool
split := strings.SplitN(msg, "<br />", -1)
if len(split) > maxLines {
split = split[:maxLines]
msg = strings.Join(split, "<br />")
truncated = true
}
if len(msg) < limit {
if truncated {
msg = msg + "..."
}
return msg
} else {
msg = msg[:limit]
truncated = true
}
if truncated {
msg = msg + "..."
}
return msg
},
"stripHTML": func(htmlStr string) string {
dom := x_html.NewTokenizer(strings.NewReader(htmlStr))
for tokenType := dom.Next(); tokenType != x_html.ErrorToken; {
if tokenType != x_html.TextToken {
tokenType = dom.Next()
continue
}
txtContent := strings.TrimSpace(x_html.UnescapeString(string(dom.Text())))
if len(txtContent) > 0 {
return x_html.EscapeString(txtContent)
}
tokenType = dom.Next()
}
return ""
},
"truncateString": func(msg string, limit int, ellipsis bool) string {
if len(msg) > limit {
if ellipsis {
return msg[:limit] + "..."
}
return msg[:limit]
}
return msg
},
// Imageboard functions
"bannedForever": bannedForever,
"getCatalogThumbnail": func(img string) string {
return getThumbnailPath("catalog", img)
},
"getThreadID": func(postInterface interface{}) (thread int) {
post, ok := postInterface.(Post)
if !ok {
thread = 0
} else if post.ParentID == 0 {
thread = post.ID
} else {
thread = post.ParentID
}
return
},
"getPostURL": func(postInterface interface{}, typeOf string, withDomain bool) (postURL string) {
if withDomain {
postURL = config.SiteDomain
}
postURL += config.SiteWebfolder
if typeOf == "recent" {
post, ok := postInterface.(*RecentPost)
if !ok {
return
}
postURL = post.GetURL(withDomain)
} else {
post, ok := postInterface.(*Post)
if !ok {
return
}
postURL = post.GetURL(withDomain)
}
return
},
"getThreadThumbnail": func(img string) string {
return getThumbnailPath("thread", img)
},
"getUploadType": func(name string) string {
extension := getFileExtension(name)
var uploadType string
switch extension {
case "":
case "deleted":
uploadType = ""
case "webm":
case "jpg":
case "gif":
uploadType = "jpg"
case "png":
uploadType = "png"
}
return uploadType
},
"imageToThumbnailPath": func(thumbType string, img string) string {
filetype := strings.ToLower(img[strings.LastIndex(img, ".")+1:])
if filetype == "gif" || filetype == "webm" {
filetype = "jpg"
}
index := strings.LastIndex(img, ".")
if index < 0 || index > len(img) {
return ""
}
thumbSuffix := "t." + filetype
if thumbType == "catalog" {
thumbSuffix = "c." + filetype
}
return img[0:index] + thumbSuffix
},
"isBanned": isBanned,
"numReplies": numReplies,
"getBoardDir": func(id int) string {
var board Board
if err := board.PopulateData(id, ""); err != nil {
return ""
}
return board.Dir
},
// Template convenience functions
"makeLoop": func(n int, offset int) []int {
loopArr := make([]int, n)
for i := range loopArr {
loopArr[i] = i + offset
}
return loopArr
},
"generateConfigTable": func() string {
configType := reflect.TypeOf(config)
tableOut := "<table style=\"border-collapse: collapse;\" id=\"config\"><tr><th>Field name</th><th>Value</th><th>Type</th><th>Description</th></tr>\n"
numFields := configType.NumField()
for f := 17; f < numFields-2; f++ {
// starting at Lockdown because the earlier fields can't be safely edited from a web interface
field := configType.Field(f)
if field.Tag.Get("critical") != "" {
continue
}
name := field.Name
tableOut += "<tr><th>" + name + "</th><td>"
f := reflect.Indirect(reflect.ValueOf(config)).FieldByName(name)
kind := f.Kind()
switch kind {
case reflect.Int:
tableOut += "<input name=\"" + name + "\" type=\"number\" value=\"" + html.EscapeString(fmt.Sprintf("%v", f)) + "\" class=\"config-text\"/>"
case reflect.String:
tableOut += "<input name=\"" + name + "\" type=\"text\" value=\"" + html.EscapeString(fmt.Sprintf("%v", f)) + "\" class=\"config-text\"/>"
case reflect.Bool:
checked := ""
if f.Bool() {
checked = "checked"
}
tableOut += "<input name=\"" + name + "\" type=\"checkbox\" " + checked + " />"
case reflect.Slice:
tableOut += "<textarea name=\"" + name + "\" rows=\"4\" cols=\"28\">"
arrLength := f.Len()
for s := 0; s < arrLength; s++ {
newLine := "\n"
if s == arrLength-1 {
newLine = ""
}
tableOut += html.EscapeString(f.Slice(s, s+1).Index(0).String()) + newLine
}
tableOut += "</textarea>"
default:
tableOut += fmt.Sprintf("%v", kind)
}
tableOut += "</td><td>" + kind.String() + "</td><td>"
defaultTag := field.Tag.Get("default")
var defaultTagHTML string
if defaultTag != "" {
defaultTagHTML = " <b>Default: " + defaultTag + "</b>"
}
tableOut += field.Tag.Get("description") + defaultTagHTML + "</td>"
tableOut += "</tr>\n"
}
tableOut += "</table>\n"
return tableOut
},
"isStyleDefault": func(style string) bool {
return style == config.DefaultStyle
},
// Version functions
"version": func() string {
return version.String()
},
}
var (
banpageTmpl *template.Template
captchaTmpl *template.Template
catalogTmpl *template.Template
errorpageTmpl *template.Template
frontPageTmpl *template.Template
boardpageTmpl *template.Template
threadpageTmpl *template.Template
postEditTmpl *template.Template
manageBansTmpl *template.Template
manageBoardsTmpl *template.Template
manageConfigTmpl *template.Template
manageHeaderTmpl *template.Template
jsTmpl *template.Template
)
func loadTemplate(files ...string) (*template.Template, error) {
var templates []string
for i, file := range files {
templates = append(templates, file)
tmplPath := path.Join(config.TemplateDir, "override", file)
if _, err := os.Stat(tmplPath); !os.IsNotExist(err) {
files[i] = tmplPath
} else {
files[i] = path.Join(config.TemplateDir, file)
}
}
return template.New(templates[0]).Funcs(funcMap).ParseFiles(files...)
}
func templateError(name string, err error) error {
if err == nil {
return nil
}
return fmt.Errorf("Failed loading template '%s/%s': %s", config.TemplateDir, name, err.Error())
}
func initTemplates(which ...string) error {
var err error
buildAll := len(which) == 0 || which[0] == "all"
resetBoardSectionArrays()
for _, t := range which {
if buildAll || t == "banpage" {
banpageTmpl, err = loadTemplate("banpage.html", "page_footer.html")
if err != nil {
return templateError("banpage.html", err)
}
}
if buildAll || t == "captcha" {
captchaTmpl, err = loadTemplate("captcha.html")
if err != nil {
return templateError("captcha.html", err)
}
}
if buildAll || t == "catalog" {
catalogTmpl, err = loadTemplate("catalog.html", "page_header.html", "page_footer.html")
if err != nil {
return templateError("catalog.html", err)
}
}
if buildAll || t == "error" {
errorpageTmpl, err = loadTemplate("error.html")
if err != nil {
return templateError("error.html", err)
}
}
if buildAll || t == "front" {
frontPageTmpl, err = loadTemplate("front.html", "front_intro.html", "page_header.html", "page_footer.html")
if err != nil {
return templateError("front.html", err)
}
}
if buildAll || t == "boardpage" {
boardpageTmpl, err = loadTemplate("boardpage.html", "page_header.html", "postbox.html", "page_footer.html")
if err != nil {
return templateError("boardpage.html", err)
}
}
if buildAll || t == "threadpage" {
threadpageTmpl, err = loadTemplate("threadpage.html", "page_header.html", "postbox.html", "page_footer.html")
if err != nil {
return templateError("threadpage.html", err)
}
}
if buildAll || t == "postedit" {
postEditTmpl, err = loadTemplate("post_edit.html", "page_header.html", "page_footer.html")
if err != nil {
return templateError("threadpage.html", err)
}
}
if buildAll || t == "managebans" {
manageBansTmpl, err = loadTemplate("manage_bans.html")
if err != nil {
return templateError("manage_bans.html", err)
}
}
if buildAll || t == "manageboards" {
manageBoardsTmpl, err = loadTemplate("manage_boards.html")
if err != nil {
return templateError("manage_boards.html", err)
}
}
if buildAll || t == "manageconfig" {
manageConfigTmpl, err = loadTemplate("manage_config.html")
if err != nil {
return templateError("manage_config.html", err)
}
}
if buildAll || t == "manageheader" {
manageHeaderTmpl, err = loadTemplate("manage_header.html")
if err != nil {
return templateError("manage_header.html", err)
}
}
if buildAll || t == "js" {
jsTmpl, err = loadTemplate("consts.js")
if err != nil {
return templateError("consts.js", err)
}
}
}
return nil
}