-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtemplate.go
161 lines (139 loc) · 3.83 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
package astikit
import (
"fmt"
"os"
"path/filepath"
"strings"
"sync"
"text/template"
)
// Templater represents an object capable of storing and parsing templates
type Templater struct {
layouts []string
m sync.Mutex
templates map[string]*template.Template
}
// NewTemplater creates a new templater
func NewTemplater() *Templater {
return &Templater{templates: make(map[string]*template.Template)}
}
// AddLayoutsFromDir walks through a dir and add files as layouts
func (t *Templater) AddLayoutsFromDir(dirPath, ext string) (err error) {
// Make sure to clean dir path so that we get consistent path separator with filepath.Walk
dirPath = filepath.Clean(dirPath)
// Get layouts
if err = filepath.Walk(dirPath, func(path string, info os.FileInfo, e error) (err error) {
// Check input error
if e != nil {
err = fmt.Errorf("astikit: walking layouts has an input error for path %s: %w", path, e)
return
}
// Only process files
if info.IsDir() {
return
}
// Check extension
if ext != "" && filepath.Ext(path) != ext {
return
}
// Read layout
var b []byte
if b, err = os.ReadFile(path); err != nil {
err = fmt.Errorf("astikit: reading %s failed: %w", path, err)
return
}
// Add layout
t.AddLayout(string(b))
return
}); err != nil {
err = fmt.Errorf("astikit: walking layouts in %s failed: %w", dirPath, err)
return
}
return
}
// AddTemplatesFromDir walks through a dir and add files as templates
func (t *Templater) AddTemplatesFromDir(dirPath, ext string) (err error) {
// Make sure to clean dir path so that we get consistent path separator with filepath.Walk
dirPath = filepath.Clean(dirPath)
// Loop through templates
if err = filepath.Walk(dirPath, func(path string, info os.FileInfo, e error) (err error) {
// Check input error
if e != nil {
err = fmt.Errorf("astikit: walking templates has an input error for path %s: %w", path, e)
return
}
// Only process files
if info.IsDir() {
return
}
// Check extension
if ext != "" && filepath.Ext(path) != ext {
return
}
// Read file
var b []byte
if b, err = os.ReadFile(path); err != nil {
err = fmt.Errorf("astikit: reading template content of %s failed: %w", path, err)
return
}
// Add template
// We use ToSlash to homogenize Windows path
if err = t.AddTemplate(filepath.ToSlash(strings.TrimPrefix(path, dirPath)), string(b)); err != nil {
err = fmt.Errorf("astikit: adding template failed: %w", err)
return
}
return
}); err != nil {
err = fmt.Errorf("astikit: walking templates in %s failed: %w", dirPath, err)
return
}
return
}
// AddLayout adds a new layout
func (t *Templater) AddLayout(c string) {
t.layouts = append(t.layouts, c)
}
// AddTemplate adds a new template
func (t *Templater) AddTemplate(path, content string) (err error) {
// Parse
var tpl *template.Template
if tpl, err = t.Parse(content); err != nil {
err = fmt.Errorf("astikit: parsing template for path %s failed: %w", path, err)
return
}
// Add template
t.m.Lock()
t.templates[path] = tpl
t.m.Unlock()
return
}
// DelTemplate deletes a template
func (t *Templater) DelTemplate(path string) {
t.m.Lock()
defer t.m.Unlock()
delete(t.templates, path)
}
// Template retrieves a templates
func (t *Templater) Template(path string) (tpl *template.Template, ok bool) {
t.m.Lock()
defer t.m.Unlock()
tpl, ok = t.templates[path]
return
}
// Parse parses the content of a template
func (t *Templater) Parse(content string) (o *template.Template, err error) {
// Parse content
o = template.New("root")
if o, err = o.Parse(content); err != nil {
err = fmt.Errorf("astikit: parsing template content failed: %w", err)
return
}
// Parse layouts
for idx, l := range t.layouts {
if o, err = o.Parse(l); err != nil {
err = fmt.Errorf("astikit: parsing layout #%d failed: %w", idx+1, err)
return
}
}
return
}