-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathtemplate.go
More file actions
370 lines (341 loc) · 9.44 KB
/
Copy pathtemplate.go
File metadata and controls
370 lines (341 loc) · 9.44 KB
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
//Initial code source, @see https://github.com/astaxie/beego/blob/master/template.go
package trygo
import (
"errors"
"fmt"
"html/template"
"log"
"os"
// "path"
"io"
"io/ioutil"
"path/filepath"
"regexp"
"strings"
"sync"
)
func executeTemplate(app *App, wr io.Writer, name string, data interface{}) error {
if app.Config.RunMode == DEV {
app.TemplateRegister.templatesLock.RLock()
defer app.TemplateRegister.templatesLock.RUnlock()
}
if t, ok := app.TemplateRegister.Templates[name]; ok {
var err error
if t.Lookup(name) != nil {
err = t.ExecuteTemplate(wr, name, data)
} else {
err = t.Execute(wr, data)
}
if err != nil {
log.Println("template Execute err:", err)
}
return err
}
panic("can't find templatefile in the path:" + name)
}
type TemplateRegister struct {
app *App
tplFuncMap template.FuncMap
Templates map[string]*template.Template
TemplateExt []string
templatesLock sync.RWMutex
templateEngines map[string]templatePreProcessor
}
func NewTemplateRegister(app *App) *TemplateRegister {
tr := &TemplateRegister{app: app}
tr.Templates = make(map[string]*template.Template)
tr.tplFuncMap = make(template.FuncMap)
tr.TemplateExt = make([]string, 0)
tr.TemplateExt = append(tr.TemplateExt, "tpl", "html")
tr.templateEngines = map[string]templatePreProcessor{}
//beegoTplFuncMap = make(template.FuncMap)
tr.tplFuncMap["dateformat"] = dateFormat
tr.tplFuncMap["date"] = date
tr.tplFuncMap["compare"] = compare
tr.tplFuncMap["compare_not"] = compareNot
tr.tplFuncMap["not_nil"] = notNil
tr.tplFuncMap["not_null"] = notNil
tr.tplFuncMap["substr"] = substr
tr.tplFuncMap["html2str"] = html2str
tr.tplFuncMap["str2html"] = str2html
tr.tplFuncMap["htmlquote"] = htmlquote
tr.tplFuncMap["htmlunquote"] = htmlunquote
tr.tplFuncMap["renderform"] = renderForm
tr.tplFuncMap["assets_js"] = assetsJs
tr.tplFuncMap["assets_css"] = assetsCSS
//tr.tplFuncMap["config"] = GetConfig
tr.tplFuncMap["map_get"] = mapGet
// Comparisons
tr.tplFuncMap["eq"] = eq // ==
tr.tplFuncMap["ge"] = ge // >=
tr.tplFuncMap["gt"] = gt // >
tr.tplFuncMap["le"] = le // <=
tr.tplFuncMap["lt"] = lt // <
tr.tplFuncMap["ne"] = ne // !=
return tr
}
// AddFuncMap let user to register a func in the template
func (this *TemplateRegister) AddFuncMap(key string, funname interface{}) error {
if _, ok := this.tplFuncMap[key]; ok {
return errors.New("funcmap already has the key")
}
this.tplFuncMap[key] = funname
return nil
}
type templatePreProcessor func(root, path string, funcs template.FuncMap) (*template.Template, error)
type templatefile struct {
root string
files map[string][]string
}
func (tf *templatefile) visit(tr *TemplateRegister, paths string, f os.FileInfo, err error) error {
if f == nil {
return err
}
if f.IsDir() || (f.Mode()&os.ModeSymlink) > 0 {
return nil
}
if !hasTemplateExt(tr, paths) {
return nil
}
replace := strings.NewReplacer("\\", "/")
file := strings.TrimLeft(replace.Replace(paths[len(tf.root):]), "/")
subDir := filepath.Dir(file)
tf.files[subDir] = append(tf.files[subDir], file)
return nil
}
func hasTemplateExt(tr *TemplateRegister, paths string) bool {
for _, v := range tr.TemplateExt {
if strings.HasSuffix(paths, "."+v) {
return true
}
}
return false
}
//func (self *templatefile) visit(tr *TemplateRegister, paths string, f os.FileInfo, err error) error {
// if f == nil {
// return err
// }
// if f.IsDir() {
// return nil
// } else if (f.Mode() & os.ModeSymlink) > 0 {
// return nil
// } else {
// hasExt := false
// for _, v := range tr.TemplateExt {
// if strings.HasSuffix(paths, v) {
// hasExt = true
// break
// }
// }
// if hasExt {
// replace := strings.NewReplacer("\\", "/")
// a := []byte(paths)
// a = a[len([]byte(self.root)):]
// fmt.Println("a:", string(a))
// subdir := path.Dir(strings.TrimLeft(replace.Replace(string(a)), "/"))
// fmt.Println("subdir:", (subdir))
// if _, ok := self.files[subdir]; ok {
// self.files[subdir] = append(self.files[subdir], paths)
// } else {
// m := make([]string, 1)
// m[0] = paths
// self.files[subdir] = m
// }
// }
// }
// return nil
//}
func (this *TemplateRegister) AddTemplateExt(ext string) {
for _, v := range this.TemplateExt {
if v == ext {
return
}
}
this.TemplateExt = append(this.TemplateExt, ext)
}
func (this *TemplateRegister) buildTemplate(dir string, files ...string) error {
if _, err := os.Stat(dir); err != nil {
if os.IsNotExist(err) {
return nil
}
return errors.New("dir open err")
}
self := &templatefile{
root: dir,
files: make(map[string][]string),
}
err := filepath.Walk(dir, func(path string, f os.FileInfo, err error) error {
return self.visit(this, path, f, err)
})
if err != nil {
fmt.Printf("filepath.Walk() returned %v\n", err)
return err
}
buildAllFiles := len(files) == 0
for _, v := range self.files {
for _, file := range v {
if buildAllFiles || inSlice(file, files) {
func() {
this.templatesLock.Lock()
defer this.templatesLock.Unlock()
ext := filepath.Ext(file)
var t *template.Template
if len(ext) == 0 {
t, err = this.getTemplate(self.root, file, v...)
} else if fn, ok := this.templateEngines[ext[1:]]; ok {
t, err = fn(self.root, file, this.tplFuncMap)
} else {
t, err = this.getTemplate(self.root, file, v...)
}
if err != nil {
//logs.Trace("parse template err:", file, err)
log.Println("parse template err:", file, err)
} else {
this.Templates[file] = t
}
//this.templatesLock.Unlock()
}()
}
}
}
return nil
}
//func (this *TemplateRegister) buildTemplate(dir string) error {
// if _, err := os.Stat(dir); err != nil {
// if os.IsNotExist(err) {
// return err
// } else {
// return errors.New("dir open error")
// }
// }
// self := templatefile{
// root: dir,
// files: make(map[string][]string),
// }
// err := filepath.Walk(dir, func(path string, f os.FileInfo, err error) error {
// fmt.Println(dir, path)
// return self.visit(this, path, f, err)
// })
// if err != nil {
// this.app.Logger.Error("filepath.Walk() returned %v", err)
// return err
// }
// for k, v := range self.files {
// this.Templates[k] = template.Must(template.New("template" + k).Funcs(this.tplFuncMap).ParseFiles(v...))
// }
// return nil
//}
func (this *TemplateRegister) getTemplate(root, file string, others ...string) (t *template.Template, err error) {
t = template.New(file).Delims(this.app.Config.TemplateLeft, this.app.Config.TemplateRight).Funcs(this.tplFuncMap)
var subMods [][]string
t, subMods, err = this.getTplDeep(root, file, "", t)
if err != nil {
return nil, err
}
t, err = this._getTemplate(t, root, subMods, others...)
if err != nil {
return nil, err
}
return
}
func (this *TemplateRegister) _getTemplate(t0 *template.Template, root string, subMods [][]string, others ...string) (t *template.Template, err error) {
t = t0
for _, m := range subMods {
if len(m) == 2 {
tpl := t.Lookup(m[1])
if tpl != nil {
continue
}
//first check filename
for _, otherFile := range others {
if otherFile == m[1] {
var subMods1 [][]string
t, subMods1, err = this.getTplDeep(root, otherFile, "", t)
if err != nil {
//logs.Trace("template parse file err:", err)
log.Println("template parse file err:", err)
} else if subMods1 != nil && len(subMods1) > 0 {
t, err = this._getTemplate(t, root, subMods1, others...)
}
break
}
}
//second check define
for _, otherFile := range others {
fileAbsPath := filepath.Join(root, otherFile)
data, err := ioutil.ReadFile(fileAbsPath)
if err != nil {
continue
}
reg := regexp.MustCompile(this.app.Config.TemplateLeft + "[ ]*define[ ]+\"([^\"]+)\"")
allSub := reg.FindAllStringSubmatch(string(data), -1)
for _, sub := range allSub {
if len(sub) == 2 && sub[1] == m[1] {
var subMods1 [][]string
t, subMods1, err = this.getTplDeep(root, otherFile, "", t)
if err != nil {
log.Println("template parse file err:", err)
} else if subMods1 != nil && len(subMods1) > 0 {
t, err = this._getTemplate(t, root, subMods1, others...)
}
break
}
}
}
}
}
return
}
func (this *TemplateRegister) getTplDeep(root, file, parent string, t *template.Template) (*template.Template, [][]string, error) {
var fileAbsPath string
if filepath.HasPrefix(file, "../") {
fileAbsPath = filepath.Join(root, filepath.Dir(parent), file)
} else {
fileAbsPath = filepath.Join(root, file)
}
if e := fileExists(fileAbsPath); !e {
panic("can't find template file:" + file)
}
data, err := ioutil.ReadFile(fileAbsPath)
if err != nil {
return nil, [][]string{}, err
}
t, err = t.New(file).Parse(string(data))
if err != nil {
return nil, [][]string{}, err
}
reg := regexp.MustCompile(this.app.Config.TemplateLeft + "[ ]*template[ ]+\"([^\"]+)\"")
allSub := reg.FindAllStringSubmatch(string(data), -1)
for _, m := range allSub {
if len(m) == 2 {
tl := t.Lookup(m[1])
if tl != nil {
continue
}
if !hasTemplateExt(this, m[1]) {
continue
}
t, _, err = this.getTplDeep(root, m[1], file, t)
if err != nil {
return nil, [][]string{}, err
}
}
}
return t, allSub, nil
}
//func fileExists(name string) bool {
// if _, err := os.Stat(name); err != nil {
// if os.IsNotExist(err) {
// return false
// }
// }
// return true
//}
func inSlice(v string, sl []string) bool {
for _, vv := range sl {
if vv == v {
return true
}
}
return false
}