1 | var Mock = require('./mock'),
|
2 | Random = require('./random'),
|
3 | Util = require('./util'),
|
4 | Handlebars = require('handlebars');
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | (function(undefined) {
|
15 | var Mock4Tpl = {
|
16 | version: '0.0.1'
|
17 | }
|
18 |
|
19 | if (!this.Mock) module.exports = Mock4Tpl
|
20 |
|
21 | Mock.tpl = function(input, options, helpers, partials) {
|
22 | return Mock4Tpl.mock(input, options, helpers, partials)
|
23 | }
|
24 | Mock.parse = function(input) {
|
25 | return Handlebars.parse(input)
|
26 | }
|
27 |
|
28 | |
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | Mock4Tpl.mock = function(input, options, helpers, partials) {
|
35 | helpers = helpers ? Util.extend({}, helpers, Handlebars.helpers) :
|
36 | Handlebars.helpers
|
37 | partials = partials ? Util.extend({}, partials, Handlebars.partials) :
|
38 | Handlebars.partials
|
39 | return Handle.gen(input, null, options, helpers, partials)
|
40 | }
|
41 |
|
42 | var Handle = {
|
43 | debug: Mock4Tpl.debug || false,
|
44 | extend: Util.extend
|
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 | Handle.gen = function(node, context, options, helpers, partials) {
|
86 | if (Util.isString(node)) {
|
87 | var ast = Handlebars.parse(node)
|
88 | options = Handle.parseOptions(node, options)
|
89 | var data = Handle.gen(ast, context, options, helpers, partials)
|
90 | return data
|
91 | }
|
92 |
|
93 | context = context || [{}]
|
94 | options = options || {}
|
95 |
|
96 | if (this[node.type] === Util.noop) return
|
97 |
|
98 | options.__path = options.__path || []
|
99 |
|
100 | if (Mock4Tpl.debug || Handle.debug) {
|
101 | console.log()
|
102 | console.group('[' + node.type + ']', JSON.stringify(node))
|
103 |
|
104 | console.log('[options]', options.__path.length, JSON.stringify(options))
|
105 | }
|
106 |
|
107 | var preLength = options.__path.length
|
108 | this[node.type](node, context, options, helpers, partials)
|
109 | options.__path.splice(preLength)
|
110 |
|
111 | if (Mock4Tpl.debug || Handle.debug) {
|
112 |
|
113 | console.groupEnd()
|
114 | }
|
115 |
|
116 | return context[context.length - 1]
|
117 | }
|
118 |
|
119 | Handle.parseOptions = function(input, options) {
|
120 | var rComment = /<!--\s*\n*Mock\s*\n*([\w\W]+?)\s*\n*-->/g;
|
121 | var comments = input.match(rComment),
|
122 | ret = {}, i, ma, option;
|
123 | for (i = 0; comments && i < comments.length; i++) {
|
124 | rComment.lastIndex = 0
|
125 | ma = rComment.exec(comments[i])
|
126 | if (ma) {
|
127 |
|
128 | option = new Function('return ' + ma[1])
|
129 | option = option()
|
130 | Util.extend(ret, option)
|
131 | }
|
132 | }
|
133 | return Util.extend(ret, options)
|
134 | }
|
135 |
|
136 | |
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 | Handle.val = function(name, options, context, def) {
|
143 | if (name !== options.__path[options.__path.length - 1]) throw new Error(name + '!==' + options.__path)
|
144 | if (Mock4Tpl.debug || Handle.debug) console.log('[options]', name, options.__path);
|
145 | if (def !== undefined) def = Mock.mock(def)
|
146 | if (options) {
|
147 | var mocked = Mock.mock(options)
|
148 | if (Util.isString(mocked)) return mocked
|
149 | if (name in mocked) {
|
150 | return mocked[name]
|
151 | }
|
152 | }
|
153 | if (Util.isArray(context[0])) return {}
|
154 | return def !== undefined ? def : (name) || Random.word()
|
155 | }
|
156 |
|
157 |
|
158 | |
159 |
|
160 |
|
161 |
|
162 | Handle.program = function(node, context, options, helpers, partials) {
|
163 | for (var i = 0; i < node.statements.length; i++) {
|
164 | this.gen(node.statements[i], context, options, helpers, partials)
|
165 | }
|
166 |
|
167 | }
|
168 |
|
169 | Handle.mustache = function(node, context, options, helpers, partials) {
|
170 | var i,
|
171 | currentContext = context[0],
|
172 | contextLength = context.length;
|
173 |
|
174 | if (Util.type(currentContext) === 'array') {
|
175 | currentContext.push({})
|
176 | currentContext = currentContext[currentContext.length - 1]
|
177 | context.unshift(currentContext)
|
178 | }
|
179 |
|
180 |
|
181 |
|
182 | if (node.isHelper || helpers && helpers[node.id.string]) {
|
183 |
|
184 | if (node.params.length === 0) {
|
185 |
|
186 | } else {
|
187 | for (i = 0; i < node.params.length; i++) {
|
188 | this.gen(node.params[i], context, options, helpers, partials)
|
189 | }
|
190 | }
|
191 |
|
192 | if (node.hash) this.gen(node.hash, context, options, helpers, partials)
|
193 | } else {
|
194 |
|
195 | this.gen(node.id, context, options, helpers, partials)
|
196 | |
197 |
|
198 |
|
199 |
|
200 | }
|
201 | if (context.length > contextLength) context.splice(0, context.length - contextLength)
|
202 | }
|
203 |
|
204 | Handle.block = function(node, context, options, helpers, partials) {
|
205 | var parts = node.mustache.id.parts,
|
206 | i, len, cur, val, type,
|
207 | currentContext = context[0],
|
208 | contextLength = context.length;
|
209 |
|
210 | if (node.inverse) {}
|
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 | if (node.mustache.isHelper ||
|
241 | helpers && helpers[node.mustache.id.string]) {
|
242 | type = parts[0]
|
243 |
|
244 | val = (Helpers[type] || Helpers.custom).apply(this, arguments)
|
245 | currentContext = context[0]
|
246 | } else {
|
247 | for (i = 0; i < parts.length; i++) {
|
248 | options.__path.push(parts[i])
|
249 |
|
250 | cur = parts[i]
|
251 |
|
252 | val = this.val(cur, options, context, {})
|
253 | currentContext[cur] = Util.isArray(val) && [] || val
|
254 |
|
255 | type = Util.type(currentContext[cur])
|
256 | if (type === 'object' || type === 'array') {
|
257 | currentContext = currentContext[cur]
|
258 | context.unshift(currentContext)
|
259 | }
|
260 | }
|
261 | }
|
262 |
|
263 |
|
264 | if (node.program) {
|
265 | if (Util.type(currentContext) === 'array') {
|
266 | len = val.length || Random.integer(3, 7)
|
267 |
|
268 | for (i = 0; i < len; i++) {
|
269 | currentContext.push(typeof val[i] !== 'undefined' ? val[i] : {})
|
270 |
|
271 | options.__path.push('[]')
|
272 | context.unshift(currentContext[currentContext.length - 1])
|
273 | this.gen(node.program, context, options, helpers, partials)
|
274 | options.__path.pop()
|
275 | context.shift()
|
276 | }
|
277 | } else this.gen(node.program, context, options, helpers, partials)
|
278 | }
|
279 |
|
280 | if (context.length > contextLength) context.splice(0, context.length - contextLength)
|
281 | }
|
282 |
|
283 | Handle.hash = function(node, context, options, helpers, partials) {
|
284 | var pairs = node.pairs,
|
285 | pair, i, j;
|
286 | for (i = 0; i < pairs.length; i++) {
|
287 | pair = pairs[i]
|
288 | for (j = 1; j < pair.length; j++) {
|
289 | this.gen(pair[j], context, options, helpers, partials)
|
290 | }
|
291 | }
|
292 | }
|
293 |
|
294 | Handle.ID = function(node, context, options) {
|
295 | var parts = node.parts,
|
296 | i, len, cur, prev, def, val, type, valType, preOptions,
|
297 | currentContext = context[node.depth],
|
298 | contextLength = context.length;
|
299 |
|
300 | if (Util.isArray(currentContext)) currentContext = context[node.depth + 1]
|
301 |
|
302 | if (!parts.length) {
|
303 |
|
304 | } else {
|
305 | for (i = 0, len = parts.length; i < len; i++) {
|
306 | options.__path.push(parts[i])
|
307 |
|
308 | cur = parts[i]
|
309 | prev = parts[i - 1]
|
310 | preOptions = options[prev]
|
311 |
|
312 | def = i === len - 1 ? currentContext[cur] : {}
|
313 | val = this.val(cur, options, context, def)
|
314 |
|
315 | type = Util.type(currentContext[cur])
|
316 | valType = Util.type(val)
|
317 | if (type === 'undefined') {
|
318 |
|
319 | if (i < len - 1 && valType !== 'object' && valType !== 'array') {
|
320 | currentContext[cur] = {}
|
321 | } else {
|
322 | currentContext[cur] = Util.isArray(val) && [] || val
|
323 | }
|
324 | } else {
|
325 |
|
326 |
|
327 | if (i < len - 1 && type !== 'object' && type !== 'array') {
|
328 | currentContext[cur] = Util.isArray(val) && [] || {}
|
329 | }
|
330 | }
|
331 |
|
332 | type = Util.type(currentContext[cur])
|
333 | if (type === 'object' || type === 'array') {
|
334 | currentContext = currentContext[cur]
|
335 | context.unshift(currentContext)
|
336 | }
|
337 | }
|
338 | }
|
339 | if (context.length > contextLength) context.splice(0, context.length - contextLength)
|
340 | }
|
341 |
|
342 | Handle.partial = function(node, context, options, helpers, partials) {
|
343 | var name = node.partialName.name,
|
344 | partial = partials && partials[name],
|
345 | contextLength = context.length;
|
346 |
|
347 | if (partial) Handle.gen(partial, context, options, helpers, partials)
|
348 |
|
349 | if (context.length > contextLength) context.splice(0, context.length - contextLength)
|
350 | }
|
351 | Handle.content = Util.noop
|
352 | Handle.PARTIAL_NAME = Util.noop
|
353 | Handle.DATA = Util.noop
|
354 | Handle.STRING = Util.noop
|
355 | Handle.INTEGER = Util.noop
|
356 | Handle.BOOLEAN = Util.noop
|
357 | Handle.comment = Util.noop
|
358 |
|
359 | var Helpers = {}
|
360 |
|
361 | Helpers.each = function(node, context, options) {
|
362 | var i, len, cur, val, parts, def, type,
|
363 | currentContext = context[0];
|
364 |
|
365 | parts = node.mustache.params[0].parts
|
366 | for (i = 0, len = parts.length; i < len; i++) {
|
367 | options.__path.push(parts[i])
|
368 |
|
369 | cur = parts[i]
|
370 | def = i === len - 1 ? [] : {}
|
371 |
|
372 | val = this.val(cur, options, context, def)
|
373 |
|
374 | currentContext[cur] = Util.isArray(val) && [] || val
|
375 |
|
376 | type = Util.type(currentContext[cur])
|
377 | if (type === 'object' || type === 'array') {
|
378 | currentContext = currentContext[cur]
|
379 | context.unshift(currentContext)
|
380 | }
|
381 | }
|
382 |
|
383 | return val
|
384 | }
|
385 |
|
386 | Helpers['if'] = Helpers.unless = function(node, context, options) {
|
387 | var params = node.mustache.params,
|
388 | i, j, cur, val, parts, def, type,
|
389 | currentContext = context[0];
|
390 |
|
391 | for (i = 0; i < params.length; i++) {
|
392 | parts = params[i].parts
|
393 | for (j = 0; j < parts.length; j++) {
|
394 | if (i === 0) options.__path.push(parts[j])
|
395 |
|
396 | cur = parts[j]
|
397 | def = j === parts.length - 1 ? '@BOOL(2,1,true)' : {}
|
398 |
|
399 | val = this.val(cur, options, context, def)
|
400 | if (j === parts.length - 1) {
|
401 | val = val === 'true' ? true :
|
402 | val === 'false' ? false : val
|
403 | }
|
404 |
|
405 | currentContext[cur] = Util.isArray(val) ? [] : val
|
406 |
|
407 | type = Util.type(currentContext[cur])
|
408 | if (type === 'object' || type === 'array') {
|
409 | currentContext = currentContext[cur]
|
410 | context.unshift(currentContext)
|
411 | }
|
412 | }
|
413 | }
|
414 | return val
|
415 | }
|
416 |
|
417 | Helpers['with'] = function(node, context, options) {
|
418 | var i, cur, val, parts, def,
|
419 | currentContext = context[0];
|
420 |
|
421 | parts = node.mustache.params[0].parts
|
422 | for (i = 0; i < parts.length; i++) {
|
423 | options.__path.push(parts[i])
|
424 |
|
425 | cur = parts[i]
|
426 | def = {}
|
427 |
|
428 | val = this.val(cur, options, context, def)
|
429 |
|
430 | currentContext = currentContext[cur] = val
|
431 | context.unshift(currentContext)
|
432 | }
|
433 | return val
|
434 | }
|
435 |
|
436 | Helpers.log = function() {
|
437 |
|
438 | }
|
439 |
|
440 | Helpers.custom = function(node, context, options) {
|
441 | var i, len, cur, val, parts, def, type,
|
442 | currentContext = context[0];
|
443 |
|
444 |
|
445 |
|
446 | if (node.mustache.params.length === 0) {
|
447 | return
|
448 |
|
449 |
|
450 |
|
451 | options.__path.push(node.mustache.id.string)
|
452 |
|
453 | cur = node.mustache.id.string
|
454 | def = '@BOOL(2,1,true)'
|
455 |
|
456 | val = this.val(cur, options, context, def)
|
457 |
|
458 | currentContext[cur] = Util.isArray(val) && [] || val
|
459 |
|
460 | type = Util.type(currentContext[cur])
|
461 | if (type === 'object' || type === 'array') {
|
462 | currentContext = currentContext[cur]
|
463 | context.unshift(currentContext)
|
464 | }
|
465 |
|
466 | } else {
|
467 | parts = node.mustache.params[0].parts
|
468 | for (i = 0, len = parts.length; i < len; i++) {
|
469 | options.__path.push(parts[i])
|
470 |
|
471 | cur = parts[i]
|
472 | def = i === len - 1 ? [] : {}
|
473 |
|
474 | val = this.val(cur, options, context, def)
|
475 |
|
476 | currentContext[cur] = Util.isArray(val) && [] || val
|
477 |
|
478 | type = Util.type(currentContext[cur])
|
479 | if (type === 'object' || type === 'array') {
|
480 | currentContext = currentContext[cur]
|
481 | context.unshift(currentContext)
|
482 | }
|
483 | }
|
484 | }
|
485 | return val
|
486 | }
|
487 |
|
488 | }).call(this);
|
489 |
|
\ | No newline at end of file |