-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathtemplate.go
208 lines (179 loc) · 4.85 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
package golf
import (
"fmt"
"html/template"
"io"
"io/ioutil"
"os"
"path"
"regexp"
)
// Regular Expressions to find out the extension syntax.
var reExtends = regexp.MustCompile(`{{ ?extends ["']?([^'"}']*)["']? ?}}`)
var reInclude = regexp.MustCompile(`{{ ?include ["']?([^"]*)["']? ?}}`)
var reTemplateTag = regexp.MustCompile(`{{ ?template \"([^"]*)" ?([^ ]*)? ?}}`)
var reDefineTag = regexp.MustCompile(`{{ ?define "([^"]*)" ?"?([a-zA-Z0-9]*)?"? ?}}`)
// TemplateLoader is the loader interface for templates.
type TemplateLoader interface {
LoadTemplate(string) (string, error)
}
// FileSystemLoader is an implementation of TemplateLoader that loads templates
// from file system.
type FileSystemLoader struct {
BaseDir string
}
// LoadTemplate loads a template from a file.
func (loader *FileSystemLoader) LoadTemplate(name string) (string, error) {
f, err := os.Open(path.Join(loader.BaseDir, name))
if err != nil {
return "", err
}
b, err := ioutil.ReadAll(f)
return string(b), err
}
// MapLoader is a implementation of TemplateLoader that loads templates from a
// map data structure.
type MapLoader map[string]string
// LoadTemplate loads a template from the map.
func (loader *MapLoader) LoadTemplate(name string) (string, error) {
if src, ok := (*loader)[name]; ok {
return src, nil
}
return "", Errorf("Could not find template " + name)
}
// TemplateManager handles the template loader and stores the function map.
type TemplateManager struct {
Loader TemplateLoader
FuncMap map[string]interface{} //template.FuncMap
}
// Template type stands for a template.
type Template struct {
Name string
Src string
}
// Render renders a template and writes it to the io.Writer interface.
func (t *TemplateManager) Render(w io.Writer, name string, data interface{}) error {
stack := []*Template{}
tplSrc, err := t.getSrc(name)
if err != nil {
return err
}
err = t.push(&stack, tplSrc, name)
if err != nil {
return err
}
tpl, err := t.assemble(stack)
if err != nil {
return err
}
if err != nil {
return err
}
if tpl == nil {
return Errorf("Nil template named %s", name)
}
err = tpl.Execute(w, data)
if err != nil {
return err
}
return nil
}
// RenderFromString renders a template from string.
func (t *TemplateManager) RenderFromString(w io.Writer, tplSrc string, data interface{}) error {
stack := []*Template{}
t.push(&stack, tplSrc, "_fromString")
tpl, err := t.assemble(stack)
if err != nil {
return err
}
err = tpl.Execute(w, data)
if err != nil {
return err
}
return nil
}
func (t *TemplateManager) push(stack *[]*Template, tplSrc string, name string) error {
extendsMatches := reExtends.FindStringSubmatch(tplSrc)
if len(extendsMatches) == 2 {
src, err := t.getSrc(extendsMatches[1])
t.push(stack, src, extendsMatches[1])
if err != nil {
return err
}
tplSrc = reExtends.ReplaceAllString(tplSrc, "")
}
Template := &Template{
Name: name,
Src: tplSrc,
}
*stack = append((*stack), Template)
return nil
}
func (t *TemplateManager) getSrc(name string) (string, error) {
tplSrc, err := t.Loader.LoadTemplate(name)
if err != nil {
return "", err
}
if len(tplSrc) < 1 {
return "", Errorf("Empty Template named %s", name)
}
return tplSrc, nil
}
func (t *TemplateManager) assemble(stack []*Template) (*template.Template, error) {
blocks := map[string]string{}
blockID := 0
for _, Template := range stack {
var errInReplace error
Template.Src = reInclude.ReplaceAllStringFunc(Template.Src, func(raw string) string {
parsed := reInclude.FindStringSubmatch(raw)
templatePath := parsed[1]
subTpl, err := t.Loader.LoadTemplate(templatePath)
if err != nil {
errInReplace = err
return "[error]"
}
return subTpl
})
if errInReplace != nil {
return nil, errInReplace
}
}
for _, Template := range stack {
Template.Src = reDefineTag.ReplaceAllStringFunc(Template.Src, func(raw string) string {
parsed := reDefineTag.FindStringSubmatch(raw)
blockName := fmt.Sprintf("BLOCK_%d", blockID)
blocks[parsed[1]] = blockName
blockID++
return "{{ define \"" + blockName + "\" }}"
})
}
var rootTemplate *template.Template
for i, Template := range stack {
Template.Src = reTemplateTag.ReplaceAllStringFunc(Template.Src, func(raw string) string {
parsed := reTemplateTag.FindStringSubmatch(raw)
origName := parsed[1]
replacedName, ok := blocks[origName]
dot := "."
if len(parsed) == 3 && len(parsed[2]) > 0 {
dot = parsed[2]
}
if ok {
return fmt.Sprintf(`{{ template "%s" %s }}`, replacedName, dot)
}
return ""
})
var thisTemplate *template.Template
if i == 0 {
thisTemplate = template.New(Template.Name)
rootTemplate = thisTemplate
} else {
thisTemplate = rootTemplate.New(Template.Name)
}
thisTemplate.Funcs(t.FuncMap)
_, err := thisTemplate.Parse(Template.Src)
if err != nil {
return nil, err
}
}
return rootTemplate, nil
}