jcc-express-mvc 1.0.11 → 1.0.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/templating-engine/index.js +54 -21
package/package.json
CHANGED
|
@@ -11,6 +11,8 @@ class Template {
|
|
|
11
11
|
ternary:
|
|
12
12
|
/@ternary\((?<condition>[a-zA-Z0-9_.-]+)\s*\?\s*(?<trueData>[\s\S]+?)\s*:\s*(?<falseData>[\s\S]+?)\)/,
|
|
13
13
|
variable: /{{\s*(?<param>[a-zA-Z0-9_.-]+)\S*}}/,
|
|
14
|
+
auth: /@auth(?:(\((?<authData>[^)]*)\)))?(?<authBody>[\s\S]*?)@endauth/,
|
|
15
|
+
guest: /@guest(?<guestBody>[\s\S]*?)@endguest/,
|
|
14
16
|
};
|
|
15
17
|
|
|
16
18
|
constructor() {
|
|
@@ -58,9 +60,7 @@ class Template {
|
|
|
58
60
|
|
|
59
61
|
#ternary(content, match, param) {
|
|
60
62
|
let result = "";
|
|
61
|
-
|
|
62
63
|
const value = Util.accessNestedProperty(param, match?.groups?.condition);
|
|
63
|
-
|
|
64
64
|
if (value) {
|
|
65
65
|
result = this.#parser(match.groups?.trueData, param);
|
|
66
66
|
} else {
|
|
@@ -95,6 +95,24 @@ class Template {
|
|
|
95
95
|
return content;
|
|
96
96
|
}
|
|
97
97
|
|
|
98
|
+
#auth(content, match, param) {
|
|
99
|
+
let result = "";
|
|
100
|
+
if (Object.values(param?.Auth).length > 1) {
|
|
101
|
+
result = match?.groups?.authBody;
|
|
102
|
+
}
|
|
103
|
+
const { beforMatch, afterMatch } = Util.content(content, match);
|
|
104
|
+
return this.#parser(beforMatch + result + afterMatch, param);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
#guest(content, match, param) {
|
|
108
|
+
let result = "";
|
|
109
|
+
if (Object.values(param?.Auth).length <= 0) {
|
|
110
|
+
result = match?.groups?.guestBody;
|
|
111
|
+
}
|
|
112
|
+
const { beforMatch, afterMatch } = Util.content(content, match);
|
|
113
|
+
return this.#parser(beforMatch + result + afterMatch, param);
|
|
114
|
+
}
|
|
115
|
+
|
|
98
116
|
#parser(content, param = this.option) {
|
|
99
117
|
const match = content.match(
|
|
100
118
|
new RegExp(
|
|
@@ -104,36 +122,51 @@ class Template {
|
|
|
104
122
|
)
|
|
105
123
|
);
|
|
106
124
|
if (!match) return content;
|
|
125
|
+
switch (true) {
|
|
126
|
+
case match[0].startsWith("@include"):
|
|
127
|
+
return this.#includes(content, match, param);
|
|
107
128
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
}
|
|
117
|
-
if (match[0].startsWith("@ternary")) {
|
|
118
|
-
return this.#ternary(content, match, param);
|
|
119
|
-
}
|
|
129
|
+
case match[0].startsWith("@foreach"):
|
|
130
|
+
return this.#handleLoop(content, match, param);
|
|
131
|
+
|
|
132
|
+
case match[0].startsWith("@auth"):
|
|
133
|
+
return this.#auth(content, match, param);
|
|
134
|
+
|
|
135
|
+
case match[0].startsWith("@guest"):
|
|
136
|
+
return this.#guest(content, match, param);
|
|
120
137
|
|
|
121
|
-
|
|
138
|
+
case match[0].startsWith("@if"):
|
|
139
|
+
return this.#handleCondition(content, match, param);
|
|
140
|
+
|
|
141
|
+
case match[0].startsWith("@ternary"):
|
|
142
|
+
return this.#ternary(content, match, param);
|
|
143
|
+
|
|
144
|
+
case match[0].startsWith("{{"):
|
|
145
|
+
return this.#handleVariable(content, match, param);
|
|
146
|
+
default:
|
|
147
|
+
return content;
|
|
148
|
+
}
|
|
122
149
|
}
|
|
123
150
|
|
|
124
151
|
#compiler(content) {
|
|
125
|
-
const newContent = this.#extends(content)
|
|
152
|
+
const newContent = this.#extends(content)
|
|
153
|
+
.replace(/(\{\{\-\-)/g, "<!--")
|
|
154
|
+
.replace(/(\-\-\}\})/g, "-->");
|
|
126
155
|
const finalContent = this.#parser(newContent);
|
|
127
156
|
return finalContent
|
|
128
157
|
.replace(/(\{\{\-\-)/g, "<!--")
|
|
129
158
|
.replace(/(\-\-\}\})/g, "-->");
|
|
130
159
|
}
|
|
131
160
|
render(filePath, locals, callback) {
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
161
|
+
try {
|
|
162
|
+
return fs.readFile(filePath, "utf-8", (err, content) => {
|
|
163
|
+
if (err) return callback(err);
|
|
164
|
+
this.option = locals;
|
|
165
|
+
return callback(null, this.#compiler(content));
|
|
166
|
+
});
|
|
167
|
+
} catch (error) {
|
|
168
|
+
throw new Error(error?.message);
|
|
169
|
+
}
|
|
137
170
|
}
|
|
138
171
|
}
|
|
139
172
|
|