-
Notifications
You must be signed in to change notification settings - Fork 0
/
gtf.go
745 lines (648 loc) · 16 KB
/
gtf.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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
package gtf
import (
"encoding/json"
"fmt"
"html/template"
htmlTemplate "html/template"
"math"
"math/rand"
"net/http"
"net/url"
"reflect"
"regexp"
"strings"
textTemplate "text/template"
"time"
humanize "github.com/dustin/go-humanize"
blackfriday "github.com/russross/blackfriday/v2"
"github.com/gomarkdown/markdown"
"github.com/gomarkdown/markdown/parser"
"github.com/xeonx/timeago"
"go.mongodb.org/mongo-driver/bson/primitive"
)
var striptagsRegexp = regexp.MustCompile("<[^>]*?>")
// recovery will silently swallow all unexpected panics.
func recovery() {
recover()
}
var GtfTextFuncMap = textTemplate.FuncMap{
"toValue": func(value interface{}) interface{} {
defer recover()
// convert primitive.ObjectID to string
switch value.(type) {
case primitive.ObjectID:
return value.(primitive.ObjectID).Hex()
default:
return value
}
},
"timeIn": func(t time.Time, locName string) string {
defer recovery()
if t.IsZero() {
return ""
}
if locName == "" {
locName = "Asia/Shanghai"
}
loc, err := time.LoadLocation(locName)
if err == nil && loc != nil {
t = t.In(loc)
}
return t.Format("2006-01-02 15:04 -07")
},
"funcMap": func(v ...interface{}) []interface{} {
defer recovery()
return v
},
"asQuery": func(query string) string {
defer recovery()
return url.QueryEscape(query)
},
"asURL": func(query string) template.URL {
defer recovery()
return template.URL(query)
},
"isChecked": func(values interface{}, option interface{}) string {
defer recovery()
list := []string{}
switch values.(type) {
case string:
return ""
case []string:
list = values.([]string)
}
//for checkbox mark check value
for _, item := range list {
if item == option.(string) {
return "checked"
}
}
return ""
},
"objectId": func(value interface{}) string {
defer recovery()
switch value.(type) {
case primitive.ObjectID:
return value.(primitive.ObjectID).Hex()
default:
return value.(string)
}
},
"parseUrl": func(path string, r *http.Request) string {
defer recovery()
link, _ := url.ParseRequestURI(r.RequestURI)
link.Path = path
return link.String()
},
"duration": func(start, stop time.Time) float64 {
defer recovery()
loading := stop.Sub(start).Seconds()
return loading
},
"existobjectid": func(values []primitive.ObjectID, id string) bool {
defer recovery()
for _, value := range values {
if value.Hex() == id {
return true
}
}
return false
},
"sameobjectid": func(value interface{}, id string) bool {
defer recovery()
switch value.(type) {
case primitive.ObjectID:
return value.(primitive.ObjectID).Hex() == id
default:
return fmt.Sprintf("%v", value) == id
}
},
"minus": func(value interface{}, i int) int {
defer recovery()
v := reflect.ValueOf(value)
switch v.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return value.(int) - i
default:
return 0
}
},
"delQuery": func(r *http.Request, k string) string {
defer recovery()
link, _ := url.ParseRequestURI(r.RequestURI)
values := link.Query()
values.Del(k)
link.RawQuery = values.Encode()
return link.String()
},
"setQuery": func(r *http.Request, k, v string) string {
defer recovery()
link, _ := url.ParseRequestURI(r.RequestURI)
values := link.Query()
values.Set(k, v)
link.RawQuery = values.Encode()
return link.String()
},
"getQuery": func(r *http.Request, k string) string {
defer recovery()
link, _ := url.ParseRequestURI(r.RequestURI)
values := link.Query()
return values.Get(k)
},
"repeat": func(count int, str string) string {
defer recovery()
return strings.Repeat(str, count)
},
"getInt": func(value interface{}) int {
defer recovery()
if value == nil {
return 0
}
switch value.(type) {
case *int:
return *value.(*int)
default:
return value.(int)
}
},
"istrue": func(value interface{}) bool {
defer recovery()
//for *bool type
v := value.(*bool)
if v == nil || *v == false {
return false
}
return true
},
"humanizeSize": func(size interface{}) string {
defer recovery()
switch v := size.(type) {
case float64:
out := uint64(int64(v))
return humanize.Bytes(out)
case int64:
return humanize.Bytes(uint64(v))
}
return "NA"
},
"isblank": func(value string) bool {
defer recovery()
s := strings.TrimSpace(value)
if s == "" {
return true
}
return false
},
"renderTime": func(value interface{}) string {
defer recovery()
switch value.(type) {
case time.Time:
startTime := value.(time.Time)
loading := time.Now().Sub(startTime).Seconds() * 1000
return fmt.Sprintf("%.2fms", loading)
default:
return ""
}
},
"markdown2": func(value string) template.HTML {
extensions := parser.CommonExtensions | parser.AutoHeadingIDs
parser := parser.NewWithExtensions(extensions)
md := []byte(value)
output := markdown.ToHTML(md, parser, nil)
return template.HTML(output)
},
"markdown": func(value string) template.HTML {
defer recovery()
md := []byte(value)
output := blackfriday.Run(md)
return template.HTML(string(output))
},
"timeago": func(value time.Time) string {
defer recovery()
return timeago.English.Format(value)
},
"asHTMLAttr": func(value string) template.HTMLAttr {
defer recovery()
return template.HTMLAttr(value)
},
"asCSS": func(value string) template.CSS {
defer recovery()
return template.CSS(value)
},
"asHTML": func(value string) template.HTML {
defer recovery()
return template.HTML(value)
},
"asJS": func(value string) template.JS {
defer recovery()
return template.JS(value)
},
"existin": func(list interface{}, value string) bool {
defer recovery()
if list == nil {
return false
}
var nlist []string
switch list.(type) {
case *[]string:
nlist = *list.(*[]string)
default:
nlist = list.([]string)
}
for _, item := range nlist {
if item == value {
return true
}
}
return false
},
"tojson": func(value interface{}) template.JS {
defer recovery()
out, _ := json.Marshal(value)
return template.JS(string(out))
},
"gettitle": func(value string) string {
defer recovery()
list := strings.Split(value, ".")
return list[len(list)-1]
},
"replace": func(s1 string, s2 string) string {
defer recovery()
return strings.Replace(s2, s1, "", -1)
},
"findreplace": func(s1 string, s2 string, s3 string) string {
defer recovery()
return strings.Replace(s3, s1, s2, -1)
},
"title": func(s string) string {
defer recovery()
return strings.Title(s)
},
"default": func(arg interface{}, value interface{}) interface{} {
defer recovery()
v := reflect.ValueOf(value)
switch v.Kind() {
case reflect.String, reflect.Slice, reflect.Array, reflect.Map:
if v.Len() == 0 {
return arg
}
case reflect.Bool:
if !v.Bool() {
return arg
}
default:
return value
}
return value
},
"length": func(value interface{}) int {
defer recovery()
v := reflect.ValueOf(value)
switch v.Kind() {
case reflect.Slice, reflect.Array, reflect.Map:
return v.Len()
case reflect.String:
return len([]rune(v.String()))
case reflect.Ptr:
return v.Elem().Len()
}
return 0
},
"lower": func(s string) string {
defer recovery()
return strings.ToLower(s)
},
"upper": func(s string) string {
defer recovery()
return strings.ToUpper(s)
},
"truncatechars": func(n int, s string) string {
defer recovery()
if n < 0 {
return s
}
r := []rune(s)
rLength := len(r)
if n >= rLength {
return s
}
if n > 3 && rLength > 3 {
return string(r[:n-3]) + "..."
}
return string(r[:n])
},
"urlencode": func(s string) string {
defer recovery()
return url.QueryEscape(s)
},
"wordcount": func(s string) int {
defer recovery()
return len(strings.Fields(s))
},
"divisibleby": func(arg interface{}, value interface{}) bool {
defer recovery()
var v float64
switch value.(type) {
case int, int8, int16, int32, int64:
v = float64(reflect.ValueOf(value).Int())
case uint, uint8, uint16, uint32, uint64:
v = float64(reflect.ValueOf(value).Uint())
case float32, float64:
v = reflect.ValueOf(value).Float()
default:
return false
}
var a float64
switch arg.(type) {
case int, int8, int16, int32, int64:
a = float64(reflect.ValueOf(arg).Int())
case uint, uint8, uint16, uint32, uint64:
a = float64(reflect.ValueOf(arg).Uint())
case float32, float64:
a = reflect.ValueOf(arg).Float()
default:
return false
}
return math.Mod(v, a) == 0
},
"lengthis": func(arg int, value interface{}) bool {
defer recovery()
v := reflect.ValueOf(value)
switch v.Kind() {
case reflect.Slice, reflect.Array, reflect.Map:
return v.Len() == arg
case reflect.String:
return len([]rune(v.String())) == arg
}
return false
},
"trim": func(s string) string {
defer recovery()
return strings.TrimSpace(s)
},
"capfirst": func(s string) string {
defer recovery()
return strings.ToUpper(string(s[0])) + s[1:]
},
"pluralize": func(arg string, value interface{}) string {
defer recovery()
flag := false
v := reflect.ValueOf(value)
switch v.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
flag = v.Int() == 1
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
flag = v.Uint() == 1
default:
return ""
}
if !strings.Contains(arg, ",") {
arg = "," + arg
}
bits := strings.Split(arg, ",")
if len(bits) > 2 {
return ""
}
if flag {
return bits[0]
}
return bits[1]
},
"yesno": func(yes string, no string, value bool) string {
defer recovery()
if value {
return yes
}
return no
},
"rjust": func(arg int, value string) string {
defer recovery()
n := arg - len([]rune(value))
if n > 0 {
value = strings.Repeat(" ", n) + value
}
return value
},
"ljust": func(arg int, value string) string {
defer recovery()
n := arg - len([]rune(value))
if n > 0 {
value = value + strings.Repeat(" ", n)
}
return value
},
"center": func(arg int, value string) string {
defer recovery()
n := arg - len([]rune(value))
if n > 0 {
left := n / 2
right := n - left
value = strings.Repeat(" ", left) + value + strings.Repeat(" ", right)
}
return value
},
"filesizeformat": func(value interface{}) string {
defer recovery()
var size float64
v := reflect.ValueOf(value)
switch v.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
size = float64(v.Int())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
size = float64(v.Uint())
case reflect.Float32, reflect.Float64:
size = v.Float()
default:
return ""
}
var KB float64 = 1 << 10
var MB float64 = 1 << 20
var GB float64 = 1 << 30
var TB float64 = 1 << 40
var PB float64 = 1 << 50
filesizeFormat := func(filesize float64, suffix string) string {
return strings.Replace(fmt.Sprintf("%.1f %s", filesize, suffix), ".0", "", -1)
}
var result string
if size < KB {
result = filesizeFormat(size, "bytes")
} else if size < MB {
result = filesizeFormat(size/KB, "KB")
} else if size < GB {
result = filesizeFormat(size/MB, "MB")
} else if size < TB {
result = filesizeFormat(size/GB, "GB")
} else if size < PB {
result = filesizeFormat(size/TB, "TB")
} else {
result = filesizeFormat(size/PB, "PB")
}
return result
},
"apnumber": func(value interface{}) interface{} {
defer recovery()
name := [10]string{"one", "two", "three", "four", "five",
"six", "seven", "eight", "nine"}
v := reflect.ValueOf(value)
switch v.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if v.Int() < 10 {
return name[v.Int()-1]
}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
if v.Uint() < 10 {
return name[v.Uint()-1]
}
}
return value
},
"intcomma": func(value interface{}) string {
defer recovery()
v := reflect.ValueOf(value)
var x uint
minus := false
switch v.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if v.Int() < 0 {
minus = true
x = uint(-v.Int())
} else {
x = uint(v.Int())
}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
x = uint(v.Uint())
default:
return ""
}
var result string
for x >= 1000 {
result = fmt.Sprintf(",%03d%s", x%1000, result)
x /= 1000
}
result = fmt.Sprintf("%d%s", x, result)
if minus {
result = "-" + result
}
return result
},
"ordinal": func(value interface{}) string {
defer recovery()
v := reflect.ValueOf(value)
var x uint
switch v.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if v.Int() < 0 {
return ""
}
x = uint(v.Int())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
x = uint(v.Uint())
default:
return ""
}
suffixes := [10]string{"th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th"}
switch x % 100 {
case 11, 12, 13:
return fmt.Sprintf("%d%s", x, suffixes[0])
}
return fmt.Sprintf("%d%s", x, suffixes[x%10])
},
"first": func(value interface{}) interface{} {
defer recovery()
v := reflect.ValueOf(value)
switch v.Kind() {
case reflect.String:
return string([]rune(v.String())[0])
case reflect.Slice, reflect.Array:
return v.Index(0).Interface()
}
return ""
},
"last": func(value interface{}) interface{} {
defer recovery()
v := reflect.ValueOf(value)
switch v.Kind() {
case reflect.String:
str := []rune(v.String())
return string(str[len(str)-1])
case reflect.Slice, reflect.Array:
return v.Index(v.Len() - 1).Interface()
}
return ""
},
"join": func(arg string, value []string) string {
defer recovery()
return strings.Join(value, arg)
},
"slice": func(start int, end int, value interface{}) interface{} {
defer recovery()
v := reflect.ValueOf(value)
if start < 0 {
start = 0
}
switch v.Kind() {
case reflect.String:
str := []rune(v.String())
if end > len(str) {
end = len(str)
}
return string(str[start:end])
case reflect.Slice:
return v.Slice(start, end).Interface()
}
return ""
},
"random": func(value interface{}) interface{} {
defer recovery()
rand.Seed(time.Now().UTC().UnixNano())
v := reflect.ValueOf(value)
switch v.Kind() {
case reflect.String:
str := []rune(v.String())
return string(str[rand.Intn(len(str))])
case reflect.Slice, reflect.Array:
return v.Index(rand.Intn(v.Len())).Interface()
}
return ""
},
"randomintrange": func(min, max int, value interface{}) int {
defer recovery()
rand.Seed(time.Now().UTC().UnixNano())
return rand.Intn(max-min) + min
},
"striptags": func(s string) string {
return strings.TrimSpace(striptagsRegexp.ReplaceAllString(s, ""))
},
}
var GtfFuncMap = htmlTemplate.FuncMap(GtfTextFuncMap)
// gtf.New is a wrapper function of template.New(https://golang.org/pkg/html/template/#New).
// It automatically adds the gtf functions to the template's function map
// and returns template.Template(http://golang.org/pkg/html/template/#Template).
func New(name string) *htmlTemplate.Template {
return htmlTemplate.New(name).Funcs(GtfFuncMap)
}
// gtf.Inject injects gtf functions into the passed FuncMap.
// It does not overwrite the original function which have same name as a gtf function.
func Inject(funcs map[string]interface{}) {
for k, v := range GtfFuncMap {
if _, ok := funcs[k]; !ok {
funcs[k] = v
}
}
}
// gtf.ForceInject injects gtf functions into the passed FuncMap.
// It overwrites the original function which have same name as a gtf function.
func ForceInject(funcs map[string]interface{}) {
for k, v := range GtfFuncMap {
funcs[k] = v
}
}
// gtf.Inject injects gtf functions into the passed FuncMap.
// It prefixes the gtf functions with the specified prefix.
// If there are many function which have same names as the gtf functions,
// you can use this function to prefix the gtf functions.
func InjectWithPrefix(funcs map[string]interface{}, prefix string) {
for k, v := range GtfFuncMap {
funcs[prefix+k] = v
}
}