/
str_render.go
167 lines (154 loc) · 3.95 KB
/
str_render.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
package smn_str_rendering
import (
"github.com/ProtossGenius/SureMoonNet/basis/smn_data"
"github.com/ProtossGenius/SureMoonNet/basis/smn_file"
"github.com/ProtossGenius/SureMoonNet/basis/smn_func"
"fmt"
"io"
"os"
"strings"
"text/template"
"unicode"
)
type CFWriter struct {
IsWriteToConsole bool
file io.Writer
}
func (this *CFWriter) Write(p []byte) (n int, err error) {
if this.IsWriteToConsole {
os.Stdout.Write(p) //d
}
return this.file.Write(p)
}
type StrRender struct {
tplData string // template file content
tpl *template.Template
err error
IsWriteToConsole bool
IsParsed bool
}
func (this *StrRender) noError() bool {
return this.err == nil
}
func (this *StrRender) inError() bool {
return this.err != nil
}
func (this *StrRender) readFile(path string) []byte {
Nil := make([]byte, 0)
if this.err != nil {
return Nil
}
bytes, err := smn_file.FileReadAll(path)
if this.noError() {
return bytes
}
this.err = err
return Nil
}
func (this *StrRender) readTplFile(tplFile string) *StrRender {
this.tplData = string(this.readFile(tplFile))
return this
}
func (this *StrRender) ParseFileData(dataFile, outFile string) error {
bytes := this.readFile(dataFile)
if this.inError() {
return this.err
}
dataMap, err := smn_data.GetDataMapFromStr(string(bytes))
if iserr(err) {
return err
}
return this.ParseData(dataMap, outFile)
}
func (this *StrRender) ParseData(data interface{}, outFile string) error {
out, err := smn_file.CreateNewFile(outFile)
defer out.Close()
if iserr(err) {
return err
}
return this.ParseDataToWritter(data, out)
}
func (this *StrRender) ParseDataToWritter(data interface{}, writer io.Writer) error {
if !this.IsParsed {
if this.inError() {
return this.err
}
this.tpl, this.err = this.tpl.Parse(this.tplData)
if this.inError() {
return this.err
}
}
return this.tpl.Execute(&CFWriter{file: writer, IsWriteToConsole: this.IsWriteToConsole}, data)
}
func (this *StrRender) getFuncList(jsContent, funcList string) (res map[string]int) {
res = make(map[string]int)
//read from funcList
if smn_file.IsFileExist(funcList) {
for _, str := range strings.Split(string(this.readFile(funcList)), "\n") {
if str = strings.TrimSpace(str); str != "" {
res[str] = 1
}
}
}
for _, str := range strings.Split(jsContent, "\n") {
if str = strings.TrimSpace(str); strings.HasPrefix(str, "function") {
str = strings.TrimSpace(str[8:])
strlen := len(str)
endIdx := 0
dflag := false // contains $
for ; endIdx < strlen; endIdx++ {
if !unicode.IsLetter(rune(str[endIdx])) && !unicode.IsNumber(rune(str[endIdx])) && str[endIdx] != '_' {
break
}
if str[endIdx] == '$' {
dflag = true
break
}
}
if !dflag {
str = str[:endIdx]
res[str] = 1
}
}
}
return res
}
func (this *StrRender) ReadJsFuncs(jsPath, funcList string) error {
if !smn_file.IsFileExist(jsPath) {
return fmt.Errorf(ERR_FILE_NOT_FOUND, jsPath)
}
jsByte := this.readFile(jsPath)
if this.inError() {
return this.err
}
jff, err := smn_func.NewJsFuncFactory(string(jsByte))
if iserr(err) {
return err
}
fl := this.getFuncList(string(jsByte), funcList)
fMap := template.FuncMap{}
for nm := range fl {
fMap[nm] = jff.ProductFunc(nm)
}
this.tpl.Funcs(fMap)
return nil
}
/**
* tplFile[0] is template file path
* tplFile[1] is template data.
*/
func NewStrRender(name string, tplFile ...string) (res *StrRender, err error) {
res = &StrRender{IsWriteToConsole: true, IsParsed: false}
tpl := template.New(name)
ictr := &Counter{vs: make(map[string]int)}
//built in function.
tpl.Funcs(template.FuncMap{"iadd": ictr.Iadd, "imult": ictr.Imult, "idiv": ictr.Idiv, "Iset": ictr.Iset, "itrue": ictr.Inot0, "iset": ictr.Iset,
"isadd": ictr.Isadd, "ismult": ictr.Ismult, "isdiv": ictr.Isdiv})
if len(tplFile) == 1 {
res.readTplFile(tplFile[0])
} else if len(tplFile) == 2 {
res.tplData = tplFile[1]
}
res.tpl = tpl
return res, res.err
}