/
template.go
75 lines (65 loc) · 1.61 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
package core
import (
"regexp"
"strings"
)
const (
startFlag = '{'
endFlag = '}'
)
// NewTemplate 解析模板,若模板内容中定义的参数多于实际提供的参数,则未提供的参数不会解析
//
// 示例1:
// args := make(map[string]string)
// args["url"] = "http://www.baidu.com"
// tmpl := "{url }/aa/bb"
// result := core.NewTemplate().Parse(tmpl, args)
// 结果1:http://www.baidu.com/aa/bb
//
// 示例2:
// args := make(map[string]string)
// args["url"] = "http://www.sina.com"
// tmpl := "{url }/11/{test}"
// result := core.NewTemplate().Parse(tmpl, args)
// 结果2:http://www.sina.com/11/{test}
func NewTemplate() *Template{
return &Template{}
}
type Template struct {
}
func (t *Template) findArgVal(args map[string]string, argName string) string{
for k,v := range args {
reg := regexp.MustCompile(`{\s*` + k + `\s*}`)
if reg.MatchString(argName) {
return v
}
}
return ""
}
// Parse 解析模板,若模板内容中定义的参数多于实际提供的参数,则为提供的参数不会解析
// tmpl: 模板内容,参数以 {a} 形式
// args: 模板参数
func (t *Template) Parse(tmpl string, args map[string]string) string{
canPush := false
argNameStack := MakeStringStack()
argMap := make(map[string]string)
for _, s := range tmpl {
if s==startFlag {
canPush = true
}
if canPush{
_, _ = argNameStack.PushByte(byte(s))
}
if s== endFlag {
canPush = false
argName, _ := argNameStack.Pop()
argMap[argName] = t.findArgVal(args, argName)
}
}
for k,v := range argMap {
if v!="" {
tmpl = strings.ReplaceAll(tmpl, k, v)
}
}
return tmpl
}