1 | 'use strict';
|
2 |
|
3 | var jade = require('jade');
|
4 | var util = require('util');
|
5 | var BaseCompiler = jade.Compiler;
|
6 |
|
7 | function Compiler () {
|
8 | BaseCompiler.apply(this, arguments);
|
9 | }
|
10 |
|
11 | util.inherits(Compiler, BaseCompiler);
|
12 |
|
13 | Compiler.prototype.visitEach = function (each) {
|
14 | this.buf.push(''
|
15 | + '// iterate ' + each.obj + '\n'
|
16 | + ';(function(){\n'
|
17 | + ' var $$local = locals["' + each.val + '"];\n'
|
18 | + ' var $$obj = ' + each.obj + ';\n'
|
19 | + ' if (\'number\' == typeof $$obj.length) {\n');
|
20 |
|
21 | if (each.alternative) {
|
22 | this.buf.push(' if ($$obj.length) {');
|
23 | }
|
24 |
|
25 | this.buf.push(''
|
26 | + ' for (var ' + each.key + ' = 0, $$l = $$obj.length; ' + each.key + ' < $$l; ' + each.key + '++) {\n'
|
27 | + ' var ' + each.val + ' = locals["' + each.val + '"] = $$obj[' + each.key + '];\n');
|
28 |
|
29 | this.visit(each.block);
|
30 |
|
31 | this.buf.push(' }\n');
|
32 |
|
33 | if (each.alternative) {
|
34 | this.buf.push(' } else {');
|
35 | this.visit(each.alternative);
|
36 | this.buf.push(' }');
|
37 | }
|
38 |
|
39 | this.buf.push(''
|
40 | + ' } else {\n'
|
41 | + ' var $$l = 0;\n'
|
42 | + ' for (var ' + each.key + ' in $$obj) {\n'
|
43 | + ' $$l++;'
|
44 | + ' var ' + each.val + ' = locals["' + each.val + '"] = $$obj[' + each.key + '];\n');
|
45 |
|
46 | this.visit(each.block);
|
47 |
|
48 | this.buf.push(' }\n');
|
49 | if (each.alternative) {
|
50 | this.buf.push(' if ($$l === 0) {');
|
51 | this.visit(each.alternative);
|
52 | this.buf.push(' }');
|
53 | }
|
54 |
|
55 | this.buf.push(''
|
56 | + ' locals["' + each.val + '"] = $$local;\n'
|
57 | + ' }\n}).call(this);\n');
|
58 | };
|
59 |
|
60 | module.exports = Compiler;
|