-
Notifications
You must be signed in to change notification settings - Fork 0
/
environment.go
148 lines (118 loc) · 2.74 KB
/
environment.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
package et
import (
"bytes"
"context"
"fmt"
"html/template"
"regexp"
"strconv"
"sync"
"sync/atomic"
"github.com/gowool/extends-template/internal"
)
const (
leftDelim = "{{"
rightDelim = "}}"
)
type Environment struct {
debug bool
global []string
left string
right string
loader Loader
handlers []Handler
reExtends *regexp.Regexp
reTemplate *regexp.Regexp
templates *sync.Map
funcMap template.FuncMap
hash atomic.Value
mu sync.Mutex
}
func NewEnvironment(loader Loader, handlers ...Handler) *Environment {
e := &Environment{loader: loader, handlers: handlers, funcMap: template.FuncMap{}}
return e.Delims(leftDelim, rightDelim)
}
func (e *Environment) Debug(debug bool) *Environment {
e.mu.Lock()
defer e.mu.Unlock()
if e.debug != debug {
e.debug = debug
e.updateHash()
}
return e
}
func (e *Environment) Delims(left, right string) *Environment {
e.mu.Lock()
defer e.mu.Unlock()
e.left = left
e.right = right
e.reExtends = internal.ReExtends(left, right)
e.reTemplate = internal.ReTemplate(left, right)
e.updateHash()
return e
}
func (e *Environment) Funcs(funcMap template.FuncMap) *Environment {
e.mu.Lock()
defer e.mu.Unlock()
for k, v := range funcMap {
e.funcMap[k] = v
}
e.updateHash()
return e
}
func (e *Environment) Global(global ...string) *Environment {
e.mu.Lock()
defer e.mu.Unlock()
e.global = append(make([]string, 0, len(global)), global...)
e.updateHash()
return e
}
func (e *Environment) NewHTMLTemplate(name string) *template.Template {
return template.New(name).Delims(e.left, e.right).Funcs(e.funcMap)
}
func (e *Environment) NewTemplateWrapper(name string) *TemplateWrapper {
return NewTemplateWrapper(
e.NewHTMLTemplate(name),
e.loader,
e.handlers,
e.reExtends,
e.reTemplate,
e.global...,
)
}
func (e *Environment) Load(ctx context.Context, name string) (*TemplateWrapper, error) {
e.mu.Lock()
defer e.mu.Unlock()
var wrapper *TemplateWrapper
key := e.key(name)
v, ok := e.templates.Load(key)
if ok {
wrapper = v.(*TemplateWrapper)
} else {
wrapper = e.NewTemplateWrapper(name)
e.templates.Store(key, wrapper)
}
if !ok || e.debug || !wrapper.IsFresh(ctx) {
if err := wrapper.Parse(ctx); err != nil {
return nil, err
}
}
return wrapper, nil
}
func (e *Environment) updateHash() {
var buf bytes.Buffer
buf.WriteString(e.left)
buf.WriteString(e.right)
buf.WriteString(strconv.FormatBool(e.debug))
for _, s := range e.global {
buf.WriteString(s)
}
for name := range e.funcMap {
buf.WriteString(name)
}
e.hash.Store(internal.Hash(buf.Bytes()))
e.templates = &sync.Map{}
}
func (e *Environment) key(name string) string {
return internal.Hash(internal.Bytes(fmt.Sprintf("%s:%s", name, e.hash.Load())))
}