-
Notifications
You must be signed in to change notification settings - Fork 1
/
template_func.go
executable file
·134 lines (117 loc) · 3.48 KB
/
template_func.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
package render
import (
"fmt"
"html/template"
"regexp"
"strings"
"time"
)
func init() {
templateFuncMap["str2html"] = Str2html
templateFuncMap["html2str"] = HTML2str
templateFuncMap["datetimeformat"] = DateTimeFormat
templateFuncMap["date"] = DateFormat
templateFuncMap["int_datetimeformat"] = IntDateTimeFormat
templateFuncMap["int_datetime"] = IntDateTime
templateFuncMap["int_date"] = IntDate
templateFuncMap["substr"] = Substr
templateFuncMap["assets_js"] = AssetsJs
templateFuncMap["assets_css"] = AssetsCSS
}
//Substr Substr
func Substr(s string, start, length int) string {
bt := []rune(s)
if start < 0 {
start = 0
}
if start > len(bt) {
start = start % len(bt)
}
var end int
if (start + length) > (len(bt) - 1) {
end = len(bt)
} else {
end = start + length
}
return string(bt[start:end])
}
// Str2html str2 to html code
func Str2html(str string) template.HTML {
return template.HTML(str)
}
// HTML2str html to str
func HTML2str(html string) string {
re := regexp.MustCompile(`\<[\S\s]+?\>`)
html = re.ReplaceAllStringFunc(html, strings.ToLower)
//remove STYLE
re = regexp.MustCompile(`\<style[\S\s]+?\</style\>`)
html = re.ReplaceAllString(html, "")
//remove SCRIPT
re = regexp.MustCompile(`\<script[\S\s]+?\</script\>`)
html = re.ReplaceAllString(html, "")
re = regexp.MustCompile(`\<[\S\s]+?\>`)
html = re.ReplaceAllString(html, "\n")
re = regexp.MustCompile(`\s{2,}`)
html = re.ReplaceAllString(html, "\n")
return strings.TrimSpace(html)
}
// IntDateTime return datetime string
// format ="YYYY-MM-DD HH:mm:ss"
func IntDateTime(val int64) (ret string) {
if val < 1 {
return
}
ret = IntDateTimeFormat(val, "YYYY-MM-DD HH:mm:ss")
return
}
// IntDate 时间戳的日期格式化
func IntDate(val int64) (ret string) {
if val < 1 {
return
}
ret = IntDateTimeFormat(val, "YYYY-MM-DD")
return
}
//DateFormat 日期格式化
func DateFormat(t time.Time) string {
return DateTimeFormat(t, "YYYY-MM-DD")
}
// IntDateTimeFormat return datetime string
// format ="YYYY-MM-DD HH:mm:ss" or format ="YYYY-MM-DD HH:mm"
func IntDateTimeFormat(val int64, format string) (ret string) {
if val < 1 {
return
}
tm := time.Unix(val, 0)
ret = DateTimeFormat(tm, format)
return
}
// DateTimeFormat 格式化时间显示
// format = "YYYY-MM-DD HH:mm:ss" or "YYYY年MM月DD日"
func DateTimeFormat(t time.Time, format string) string {
res := strings.Replace(format, "MM", t.Format("01"), -1)
res = strings.Replace(res, "M", t.Format("1"), -1)
res = strings.Replace(res, "DD", t.Format("02"), -1)
res = strings.Replace(res, "D", t.Format("2"), -1)
res = strings.Replace(res, "YYYY", t.Format("2006"), -1)
res = strings.Replace(res, "YY", t.Format("06"), -1)
res = strings.Replace(res, "HH", fmt.Sprintf("%02d", t.Hour()), -1)
res = strings.Replace(res, "H", fmt.Sprintf("%d", t.Hour()), -1)
res = strings.Replace(res, "hh", t.Format("03"), -1)
res = strings.Replace(res, "h", t.Format("3"), -1)
res = strings.Replace(res, "mm", t.Format("04"), -1)
res = strings.Replace(res, "m", t.Format("4"), -1)
res = strings.Replace(res, "ss", t.Format("05"), -1)
res = strings.Replace(res, "s", t.Format("5"), -1)
return res
}
// AssetsJs returns script tag with src string.
func AssetsJs(text string) template.HTML {
text = "<script src=\"" + text + "\"></script>"
return template.HTML(text)
}
// AssetsCSS returns stylesheet link tag with src string.
func AssetsCSS(text string) template.HTML {
text = "<link href=\"" + text + "\" rel=\"stylesheet\" />"
return template.HTML(text)
}