-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlebars_process.js
335 lines (295 loc) · 9.36 KB
/
handlebars_process.js
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
// args:
// - handlebars template file
// - json data file
var handlebars = require('handlebars');
var fs = require('fs');
var helpers = require('handlebars-helpers');
// helpers.object();
// helpers.array();
// helpers.comparison();
var template_file = process.argv[2];
var data_file = process.argv[3];
// {{underscore value}}
handlebars.registerHelper('underscore', function(text) {
return text.replace(/\.?([A-Z])/g, function (x,y){return "_" + y.toLowerCase()}).replace(/^_/, "");
})
// {{kebabcase value}}
handlebars.registerHelper('kebabcase', function(text) {
return text.replace(/\.?([A-Z])/g, function (x,y){return "-" + y.toLowerCase()}).replace(/^-/, "");
})
// {{cap value}}
handlebars.registerHelper('cap', function(text) {
var words = text.split(' ');
for (var i = 0; i < words.length; i++) {
if (words[i].length > 1) {
words[i] = words[i][0].toUpperCase() + words[i].substr(1);
}
}
return words.join(' ');
})
// {{uncap value}}
handlebars.registerHelper('uncap', function(text) {
var words = text.split(' ');
for (var i = 0; i < words.length; i++) {
if (words[i].length > 1) {
words[i] = words[i][0].toLowerCase() + words[i].substr(1);
}
}
return words.join(' ');
})
// {{downcase value}}
handlebars.registerHelper('downcase', function(text) {
return text.toLowerCase();
})
// {{indent " " value}}
handlebars.registerHelper('indent', function(indent, text) {
var lines = text.split('\n');
var result = []
for (var i = 0; i < lines.length; i++) {
if (lines[i].length != 0)
result.push(indent + lines[i]);
}
return result.join('\n');
})
// {{indentProp " " "a.b"}}
handlebars.registerHelper('indentProp', function(indent, options) {
var text = helpers.utils.get(this, options.hash.prop);
var lines = text.split('\n');
var result = []
for (var i = 0; i < lines.length; i++) {
if (lines[i].length != 0)
result.push(indent + lines[i]);
}
return result.join('\n');
})
// {{#each list}}
// {{list_value}}{{commaDelim}}
// {{/each}}
handlebars.registerHelper('commaDelim', function(context) {
return context.data.last ? "" : ",";
})
// {{#each list}}
// {{list_value}}{{curryArrowDelim}}
// {{/each}}
handlebars.registerHelper('curryArrowSpaceDelim', function(context) {
return context.data.last ? "" : " -> ";
})
// {{#each list}}
// {{list_value}}{{commaSpaceDelim}}
// {{/each}}
handlebars.registerHelper('commaSpaceDelim', function(context) {
return context.data.last ? "" : ", ";
})
// {{#each list}}
// {{list_value}}{{applFmapDelimSuffix}}
// {{/each}}
handlebars.registerHelper('applFmapDelimSuffix', function(context) {
return context.data.last ? "" : " <*>";
})
// {{#each list}}
// {{list_value}}{{applFmapDelimPrefix}}
// {{/each}}
handlebars.registerHelper('applFmapDelimPrefix', function(context) {
return context.data.first ? "<$> " : "<*> ";
})
// {{#equal unicorns ponies }}
// That's amazing, unicorns are actually undercover ponies
// {{/equal}}
handlebars.registerHelper('equal', function(lvalue, rvalue, options) {
if (arguments.length < 3)
throw new Error("Handlebars Helper equal needs 2 parameters");
if(lvalue != rvalue) {
return options.inverse(this);
} else {
return options.fn(this);
}
});
// {{#compare unicorns ponies operator="<"}}
// I knew it, unicorns are just low-quality ponies!
// {{/compare}}
handlebars.registerHelper('compare', function(lvalue, rvalue, options) {
if (arguments.length < 3)
throw new Error("Handlerbars Helper 'compare' needs 2 parameters");
var operator = options.hash.operator || "==";
var operators = {
'==': function(l,r) { return l == r; },
'===': function(l,r) { return l === r; },
'!=': function(l,r) { return l != r; },
'<': function(l,r) { return l < r; },
'>': function(l,r) { return l > r; },
'<=': function(l,r) { return l <= r; },
'>=': function(l,r) { return l >= r; },
'typeof': function(l,r) { return typeof l == r; }
}
if (!operators[operator])
throw new Error("Handlerbars Helper 'compare' doesn't know the operator "+operator);
var result = operators[operator](lvalue,rvalue);
if( result ) {
return options.fn(this);
} else {
return options.inverse(this);
}
});
// {{#ifValueEquals type equals="image"}}
// {{title}}
// {{/ifValueEquals}}
handlebars.registerHelper("ifValueEquals", function(conditional, options) {
if (conditional == options.hash.equals) {
return options.fn(this);
} else {
return options.inverse(this);
}
});
// {{valueProp "a.b"}}
handlebars.registerHelper("valueProp", function(prop) {
var value = helpers.utils.get(this, prop);
return value;
});
// {{#ifPropEquals prop="a.b" equals="image"}}
// {{title}}
// {{/ifPropEquals}}
handlebars.registerHelper("ifPropEquals", function(options) {
var conditional = helpers.utils.get(this, options.hash.prop);
// console.log(options.hash.prop + " " + conditional + " " + options.hash.equals)
if (conditional == options.hash.equals) {
return options.fn(this);
} else {
return options.inverse(this);
}
});
// {{#ifProp "a.b"}}
// {{title}}
// {{/ifProp}}
handlebars.registerHelper("ifProp", function(options) {
var conditional = helpers.utils.get(this, options.hash.prop);
if (conditional) {
return options.fn(this);
} else {
return options.inverse(this);
}
});
// {{#ifPropNameIsForeignKey propName}}
// ...
// {{/ifPropNameIsForeignKey}}
handlebars.registerHelper("ifPropNameIsForeignKey", function(propName, options) {
if (propName.endsWith("Id")) {
return options.fn(this);
} else {
return options.inverse(this);
}
});
// {{#ifPropIsForeignKey propObj}}
// ...
// {{/ifPropIsForeignKey}}
// handlebars.registerHelper("ifPropIsForeignKey", function(propObj, options) {
// if (propObj["propName"].endsWith("Id")) {
// return options.fn(this);
// } else {
// return options.inverse(this);
// }
// });
handlebars.registerHelper("truncateTwoCharsAtEnd", function(str, options) {
return str.substring(0, str.length-2);
});
// {{shakespVariable}}{{name}}{{shakespVariable}}
handlebars.registerHelper('shakespVariable', function(options) {
return new handlebars.SafeString('#{' + options.fn(this) + '}');
});
// {{shakespRoute}}{{name}}{{shakespRoute}}
handlebars.registerHelper('shakespRoute', function(options) {
return new handlebars.SafeString('@{' + options.fn(this) + '}');
});
handlebars.registerHelper('eachForeignKeyProp', function(array, options) {
var tmpArr = []
for (var j = 0; j < array.length; j++) {
var o = array[j];
if (o["propType"].endsWith("Id"))
tmpArr.push(o)
}
var aLen = tmpArr.length;
var i = -1;
var buffer = '';
while (++i < aLen) {
var item = tmpArr[i];
item.index = i + 1;
item.total = aLen;
item.isFirst = i === 0;
item.isLast = i === (aLen - 1);
data.index = i;
data.total = item.total;
data.first = item.isFirst;
data.last = item.isLast;
buffer += options.fn(item, {data: data});
}
return buffer;
});
handlebars.registerHelper('filterByPropValues', function(selectPropName, values, array, options) {
var vLen = values.length;
var buffer = '';
var i = -1;
var m = {};
for (var j = 0; j < array.length; j++) {
var o = array[j];
m[o[selectPropName]] = o;
}
while (++i < vLen) {
var item = m[values[i]];
item.index = i + 1;
item.total = vLen;
item.isFirst = i === 0;
item.isLast = i === (vLen - 1);
data.index = i;
data.total = item.total;
data.first = item.isFirst;
data.last = item.isLast;
buffer += options.fn(item, {data: data});
}
return buffer;
});
handlebars.registerHelper('filterByPropValuesIfTrue', function(selectPropName, values, array, checkIfPropName, options) {
var vLen = values.length;
var buffer = '';
var i = -1;
var m = {};
for (var j = 0; j < array.length; j++) {
var o = array[j];
m[o[selectPropName]] = o;
}
var array2 = []
while (++i < vLen) {
var item = m[values[i]];
if (item[checkIfPropName])
array2.push(item)
}
i = -1;
vLen = array2.length
while (++i < vLen) {
var item = array2[i];
item.index = i + 1;
item.total = vLen;
item.isFirst = i === 0;
item.isLast = i === (vLen - 1);
data.index = i;
data.total = item.total;
data.first = item.isFirst;
data.last = item.isLast;
buffer += options.fn(item, {data: data});
}
return buffer;
});
var partialsDir = 'templates';
var filenames = fs.readdirSync(partialsDir);
filenames.forEach(function (filename) {
var matches = /^_([^.]+).hbs$/.exec(filename);
if (!matches) {
return;
}
var name = matches[1];
var template = fs.readFileSync(partialsDir + '/' + filename, 'utf8');
handlebars.registerPartial(name, template);
});
var template_source = fs.readFileSync(template_file, 'utf-8')
var data = JSON.parse(fs.readFileSync(data_file, 'utf-8').toString());
var template = handlebars.compile(template_source);
var html = template(data);
console.log(html)