Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80738 views
1
define(
2
["./visitor","exports"],
3
function(__dependency1__, __exports__) {
4
"use strict";
5
var Visitor = __dependency1__["default"];
6
7
function print(ast) {
8
return new PrintVisitor().accept(ast);
9
}
10
11
__exports__.print = print;function PrintVisitor() {
12
this.padding = 0;
13
}
14
15
__exports__.PrintVisitor = PrintVisitor;PrintVisitor.prototype = new Visitor();
16
17
PrintVisitor.prototype.pad = function(string) {
18
var out = "";
19
20
for(var i=0,l=this.padding; i<l; i++) {
21
out = out + " ";
22
}
23
24
out = out + string + "\n";
25
return out;
26
};
27
28
PrintVisitor.prototype.Program = function(program) {
29
var out = '',
30
body = program.body,
31
i, l;
32
33
if (program.blockParams) {
34
var blockParams = 'BLOCK PARAMS: [';
35
for(i=0, l=program.blockParams.length; i<l; i++) {
36
blockParams += ' ' + program.blockParams[i];
37
}
38
blockParams += ' ]';
39
out += this.pad(blockParams);
40
}
41
42
for(i=0, l=body.length; i<l; i++) {
43
out = out + this.accept(body[i]);
44
}
45
46
this.padding--;
47
48
return out;
49
};
50
51
PrintVisitor.prototype.MustacheStatement = function(mustache) {
52
return this.pad('{{ ' + this.SubExpression(mustache) + ' }}');
53
};
54
55
PrintVisitor.prototype.BlockStatement = function(block) {
56
var out = "";
57
58
out = out + this.pad('BLOCK:');
59
this.padding++;
60
out = out + this.pad(this.SubExpression(block));
61
if (block.program) {
62
out = out + this.pad('PROGRAM:');
63
this.padding++;
64
out = out + this.accept(block.program);
65
this.padding--;
66
}
67
if (block.inverse) {
68
if (block.program) { this.padding++; }
69
out = out + this.pad('{{^}}');
70
this.padding++;
71
out = out + this.accept(block.inverse);
72
this.padding--;
73
if (block.program) { this.padding--; }
74
}
75
this.padding--;
76
77
return out;
78
};
79
80
PrintVisitor.prototype.PartialStatement = function(partial) {
81
var content = 'PARTIAL:' + partial.name.original;
82
if(partial.params[0]) {
83
content += ' ' + this.accept(partial.params[0]);
84
}
85
if (partial.hash) {
86
content += ' ' + this.accept(partial.hash);
87
}
88
return this.pad('{{> ' + content + ' }}');
89
};
90
91
PrintVisitor.prototype.ContentStatement = function(content) {
92
return this.pad("CONTENT[ '" + content.value + "' ]");
93
};
94
95
PrintVisitor.prototype.CommentStatement = function(comment) {
96
return this.pad("{{! '" + comment.value + "' }}");
97
};
98
99
PrintVisitor.prototype.SubExpression = function(sexpr) {
100
var params = sexpr.params, paramStrings = [], hash;
101
102
for(var i=0, l=params.length; i<l; i++) {
103
paramStrings.push(this.accept(params[i]));
104
}
105
106
params = "[" + paramStrings.join(", ") + "]";
107
108
hash = sexpr.hash ? " " + this.accept(sexpr.hash) : "";
109
110
return this.accept(sexpr.path) + " " + params + hash;
111
};
112
113
PrintVisitor.prototype.PathExpression = function(id) {
114
var path = id.parts.join('/');
115
return (id.data ? '@' : '') + 'PATH:' + path;
116
};
117
118
119
PrintVisitor.prototype.StringLiteral = function(string) {
120
return '"' + string.value + '"';
121
};
122
123
PrintVisitor.prototype.NumberLiteral = function(number) {
124
return "NUMBER{" + number.value + "}";
125
};
126
127
PrintVisitor.prototype.BooleanLiteral = function(bool) {
128
return "BOOLEAN{" + bool.value + "}";
129
};
130
131
PrintVisitor.prototype.Hash = function(hash) {
132
var pairs = hash.pairs;
133
var joinedPairs = [];
134
135
for (var i=0, l=pairs.length; i<l; i++) {
136
joinedPairs.push(this.accept(pairs[i]));
137
}
138
139
return 'HASH{' + joinedPairs.join(', ') + '}';
140
};
141
PrintVisitor.prototype.HashPair = function(pair) {
142
return pair.key + '=' + this.accept(pair.value);
143
};
144
});
145