-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpage_command.go
executable file
·245 lines (203 loc) · 6.39 KB
/
page_command.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package webutil
import (
"bytes"
"encoding/json"
"fmt"
"reflect"
"strings"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
// parseJsonIntoSingleLine trimes json string into a single line.
func (h *HTTP) parseJsonIntoSingleLine(bx []byte) []byte {
b := bx
m := make([]map[string]interface{}, 0)
json.Unmarshal(bx, &m)
if len(m) > 0 {
b, _ = json.Marshal(m)
}
return b
}
// parsePageCommand processes command directives inside a page.
func (h *HTTP) parsePageCommand(b []byte, cmd string) ([]byte, []error) {
var errArry []error
delm := "}}"
delmByte := []byte(delm)
leftSearchLeafe := []byte(fmt.Sprintf("{{.%s", cmd))
v := bytes.Split(b, leftSearchLeafe)
if cmd == PageCmdLoadFile {
// Example:
// <div style="border:none">
// {{.$LoadFile:/web/html/index.html}}
// </div>
// Loop thru the page and process all occurance of {{.$LoadFile
for i := 0; i < len(v); i++ {
if !bytes.Contains(v[i], delmByte) {
continue
}
v := bytes.Split(v[i], delmByte)
if len(v) < 2 {
continue
}
v = bytes.Split(v[0], []byte(":"))
if len(v) < 2 {
continue
}
relPath := v[1]
pysPath := fmt.Sprintf("%s/%s", h.RootPhysicalPath, relPath)
pysPath = strings.ReplaceAll(pysPath, "//", "/")
bx, err := ReadFile(pysPath)
if err != nil {
errArry = append(errArry, err)
continue
}
// try to decrypt the file
if h.DecryptFile != nil {
h.DecryptFile(pysPath, h.EncryptionKeyPhrase)
bx, err = ReadFile(pysPath)
if err != nil {
errArry = append(errArry, err)
continue
}
}
bx = h.parseJsonIntoSingleLine(bx)
phrase := fmt.Sprintf("%s:%s%s", string(leftSearchLeafe), relPath, delm)
phraseByte := []byte(phrase)
b = bytes.ReplaceAll(b, phraseByte, bx)
}
} else if cmd == "$RenderJSONToHTMLControl" {
v := strings.Split(string(b), string(leftSearchLeafe))
for i := 0; i < len(v); i++ {
v[i] = strings.TrimSpace(strings.ReplaceAll(v[i], "\n", ""))
v[i] = strings.TrimSpace(strings.ReplaceAll(v[i], "\r", ""))
v[i] = strings.TrimSpace(strings.ReplaceAll(v[i], "\t", ""))
if !strings.Contains(v[i], delm) {
continue
}
if !strings.HasPrefix(v[i], `:[{"`) {
continue
}
jsn := v[i]
jsn = jsn[1:] // Minus :
pos := strings.Index(jsn, "}}") // find the end
jsn = jsn[:pos]
if len(jsn) < 5 {
continue
}
bJsn := []byte(jsn)
bJsn = h.parseJsonIntoSingleLine(bJsn)
m := make([]map[string]interface{}, 0)
json.Unmarshal(bJsn, &m)
// Expect bootstrap to be installed
html := ""
hcheck := `
<div class="form-check mb-2 mt-2">
<input class="form-check-input" type="checkbox" value="" id="#CHECK_ID#" #CHECKED#>
<label class="form-check-label" for="#CHECK_ID#">#LABEL_VALUE#</label>
</div>`
h := `
<div class="input-group mb-3">
<span class="input-group-text" id="#LABEL_ID#">#LABEL#</span>
<input type="text" class="form-control" value="#VALUE#" id="#INPT_ID#" placeholder="#LABEL#" aria-label="#LABEL#" aria-describedby="#LABEL_ID#">
</div>`
for _, v := range m {
for kk, vv := range v {
key := kk
if strings.Contains(key, "-") {
// remove spaces
key = strings.Title(strings.ReplaceAll(key, " ", ""))
} else {
// caps first letter
key = strings.Title(key)
// and then remove space
key = strings.Title(strings.ReplaceAll(key, " ", ""))
}
if reflect.TypeOf(vv).Kind() == reflect.String {
lbl := strings.Title(strings.ReplaceAll(key, "-", " "))
cx := cases.Title(language.English)
lbl = cx.String(lbl)
lblID := strings.ToLower(fmt.Sprintf("label-%s", key))
inptID := strings.ToLower(fmt.Sprintf("input-%s", key))
value := fmt.Sprintf("%v", vv)
hx := strings.ReplaceAll(h, "#LABEL#", lbl)
hx = strings.ReplaceAll(hx, "#VALUE#", value)
hx = strings.ReplaceAll(hx, "#LABEL_ID#", lblID)
hx = strings.ReplaceAll(hx, "#INPT_ID#", inptID)
html = fmt.Sprintf("%s%s", html, hx)
} else if reflect.TypeOf(vv).Kind() == reflect.Slice {
ifarry := vv.([]interface{})
isString := reflect.TypeOf(ifarry[0]).Kind() == reflect.String
kk = strings.Title(strings.ReplaceAll(kk, "-", " "))
cx := cases.Title(language.English)
kk = cx.String(kk)
html = fmt.Sprintf(`%s<dl><dt class="mb-1">%s</dt>`, html, kk)
if isString {
lst := `<ul class="list-group" style="border:none">`
for j := 0; j < len(ifarry); j++ {
lst = fmt.Sprintf(`%s<dd><input type="text" class="form-control" value="%s"></dd>`, lst, ifarry[j])
}
html = fmt.Sprintf(`%s%s`, html, lst)
}
html = fmt.Sprintf(`%s</dl>`, html)
} else if reflect.TypeOf(vv).Kind() == reflect.Bool {
chkVal := strings.ToLower(fmt.Sprintf("%v", vv))
cx := cases.Title(language.English)
chkLbl := cx.String(kk)
chk := strings.ReplaceAll(hcheck, "#LABEL_VALUE#", chkLbl)
chkID := fmt.Sprintf("chk-%s", strings.ToLower(strings.Title(strings.ReplaceAll(key, "-", " "))))
chk = strings.ReplaceAll(chk, "#CHECK_ID#", chkID)
if chkVal == "true" {
chk = strings.ReplaceAll(chk, "#LABEL_VALUE#", "checked")
} else {
chk = strings.ReplaceAll(chk, "#LABEL_VALUE#", "")
}
html = fmt.Sprintf(`%s%s`, html, chk)
}
}
}
toReplace := fmt.Sprintf("%s:%s}}", string(leftSearchLeafe), jsn)
b = bytes.ReplaceAll(b, []byte(toReplace), []byte(html))
}
}
return b, errArry
}
// ProcessPageCommands replaces command directive blocks with
// their results. In the following example the content of
// the file /web/html/index.html will be placed inside the
// div tag
//
// <div style="border:none">
// {{.$LoadFile:/web/html/index.html}}
// </div>
func (h *HTTP) ProcessPageCommands(b []byte) ([]byte, []error) {
var allErrors []error
var cmdArry = []string{
PageCmdLoadFile,
"$RenderJSONToHTMLControl",
}
for i := 0; i < len(cmdArry); i++ {
var err []error
b, err = h.parsePageCommand(b, cmdArry[i])
for j := 0; j < len(err); j++ {
allErrors = append(allErrors, err[j])
}
}
// prevent Infinite loop
if len(allErrors) > 0 {
return b, allErrors
}
// load more files if the target file is loading other files.
lstBSz := 0
for {
if bytes.Contains(b, []byte("{{."+PageCmdLoadFile)) {
b, _ = h.ProcessPageCommands(b)
if lstBSz > 0 && len(b) == lstBSz {
break
}
lstBSz = len(b)
} else {
break
}
}
return b, allErrors
}