-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.go
44 lines (35 loc) · 1.04 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
package reader
import (
"html/template"
"path/filepath"
"strings"
"github.com/bannzai/constructor/structure"
)
type Template struct{}
var functions = template.FuncMap{
"upperCamelCase": upperCamelCase,
"parameterCase": lowerCamelCase,
"argumentCase": lowerCamelCase,
"escapeReservedWord": escapeReservedWord,
}
func escapeReservedWord(target string) string {
for _, reservedWord := range structure.ReservedWords {
if reservedWord == target {
return "_" + target
}
}
return target
}
func lowerCamelCase(target string) string {
firstString := strings.ToLower(target[:1])
dropedFirstString := target[1:]
return escapeReservedWord(firstString + dropedFirstString)
}
func upperCamelCase(target string) string {
firstString := strings.ToUpper(target[:1])
dropedFirstString := target[1:]
return escapeReservedWord(firstString + dropedFirstString)
}
func (impl Template) Read(filePath structure.Path) *template.Template {
return template.Must(template.New(filepath.Base(filePath)).Funcs(functions).ParseFiles(filePath))
}