-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate.go
More file actions
158 lines (138 loc) · 2.83 KB
/
Copy pathtemplate.go
File metadata and controls
158 lines (138 loc) · 2.83 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
package template
import (
"fmt"
"html/template"
"io"
"os"
"path/filepath"
"strings"
radix "github.com/armon/go-radix"
"github.com/x-mod/dir"
)
type Template struct {
name string
ext string
delimLeft string
delimRight string
fns map[string]interface{}
dir string
root *dir.Dir
nameByPath bool
rtree *radix.Tree
*template.Template
}
type Option func(*Template)
func Name(name string) Option {
return func(t *Template) {
t.name = name
}
}
func Dir(dir string) Option {
return func(t *Template) {
t.dir = dir
}
}
func Extension(ext string) Option {
return func(t *Template) {
t.ext = ext
}
}
func Delims(left string, right string) Option {
return func(t *Template) {
t.delimLeft = left
t.delimRight = right
}
}
func Function(name string, fn interface{}) Option {
return func(t *Template) {
t.fns[name] = fn
}
}
func NameByPath(flag bool) Option {
return func(t *Template) {
t.nameByPath = flag
}
}
func New(opts ...Option) *Template {
t := &Template{
dir: ".",
ext: ".tpl",
fns: make(map[string]interface{}),
rtree: radix.New(),
}
for _, opt := range opts {
opt(t)
}
//default
Function("JS", Javascript)(t)
Function("RMB", RMB)(t)
Function("chineseDate", ChineseDate)(t)
return t
}
func (t *Template) Find(uri string) (string, error) {
dst := strings.TrimPrefix(uri, "/")
if dst == "" {
return "index", nil
}
if name, _, ok := t.rtree.LongestPrefix(dst); ok {
return name, nil
}
return "", fmt.Errorf("<%s> not matched", uri)
}
func (t *Template) AddFunc(name string, fn interface{}) {
t.Template.Funcs(map[string]interface{}{
name: fn,
})
}
func (t *Template) Open() error {
root := dir.New(dir.Root(t.dir))
if err := root.Open(); err != nil {
return err
}
t.root = root
t.Template = template.New(t.name)
t.Template.Delims(t.delimLeft, t.delimRight)
t.Template.Funcs(t.fns)
files := []string{}
walk := func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
if strings.HasSuffix(path, t.ext) {
files = append(files, path)
}
return nil
}
if err := filepath.Walk(t.root.Path(), walk); err != nil {
return err
}
//!nameByPath
if !t.nameByPath {
_, err := t.Template.ParseFiles(files...)
return err
}
//nameByPath
for _, file := range files {
rel, err := filepath.Rel(t.root.Path(), file)
if err != nil {
return fmt.Errorf("filepath relative: %w", err)
}
name := filepath.ToSlash(rel)
name = strings.TrimSuffix(name, t.ext)
fd, err := os.Open(file)
if err != nil {
return err
}
bytes, err := io.ReadAll(fd)
if err != nil {
return err
}
sub := t.Template.New(name)
// sub.Funcs(t.fns)
if _, err := sub.Parse(string(bytes)); err != nil {
return fmt.Errorf("template parse <%s> : %w", file, err)
}
t.rtree.Insert(name, struct{}{})
}
return nil
}