This repository has been archived by the owner on Jun 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bogus.js
175 lines (142 loc) · 3.89 KB
/
bogus.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
(function(Hogan) {
// Get a hold of Underscore.js.
var _ = null;
if (typeof window !== 'undefined') {
_ = window._;
}
if (!_) {
_ = require('underscore');
}
// Empty constructor.
var ctor = function() {};
// Create a Bogus namespace that inherits from Hogan.
ctor.prototype = Hogan;
var Bogus = new ctor();
if (typeof window !== 'undefined') {
this.Bogus = Bogus;
} else {
module.exports = Bogus;
}
// Override the compiler's generate method to create Bogus templates.
Bogus.generate = function (tree, text, options) {
var code = Hogan.generate(tree, text, { asString: true });
if (options.asString) {
return code;
}
// FIXME: This is hacky, but otherwise requires support from Hogan.js.
code = code.slice(16, -2);
return new Bogus.Template(new Function('c', 'p', 'i', code),
text, Bogus, options);
};
// Template class that inherits from Hogan.Template.
Bogus.Template = function() {
Hogan.Template.apply(this, arguments);
};
ctor.prototype = Hogan.Template.prototype;
var proto = Bogus.Template.prototype = new ctor();
// Truthy includes all JS truthy values, and the empty string.
var truthy = function(val) {
return (val === '') || !!val;
};
// Value helper.
var getValue = function(obj, key) {
var val;
if (obj && typeof obj === 'object') {
if (obj.attributes && truthy(val = obj.attributes[key])) {
return { val: val, found: true };
}
if (truthy(val = obj[key])) {
return { val: val, found: true };
}
}
return { val: false, found: false };
};
// render a section
proto.rs = function(context, partials, section) {
var _this = this,
tail = context[context.length - 1];
function iter(obj) {
context.push(obj);
section(context, partials, _this);
context.pop();
}
if (tail.forEach) {
tail.forEach(iter);
}
else if (_.isArray(tail)) {
_.each(tail, iter);
}
else {
section(context, partials, this);
}
};
// maybe start a section
proto.s = function(val, ctx, partials, inverted, start, end, tags) {
var pass;
if (typeof val === 'object' && val.length === 0) {
return false;
}
if (typeof val === 'function') {
val = this.ls(val, ctx, partials, inverted, start, end, tags);
}
pass = truthy(val);
if (!inverted && pass && ctx) {
ctx.push((typeof val === 'object') ? val : ctx[ctx.length - 1]);
}
return pass;
};
// find values with dotted names
proto.d = function(key, ctx, partials, returnFound) {
var names = null,
val = null,
result = null,
cx = null;
if (key === '.') {
return ctx[ctx.length - 1];
}
names = key.split('.');
val = this.f(names[0], ctx, partials, returnFound);
for (var i = 1; i < names.length; i++) {
result = getValue(val, names[i]);
if (result.found) {
cx = val;
val = result.val;
}
else {
val = '';
break;
}
}
if (returnFound && !val) {
return false;
}
if (!returnFound && typeof val === 'function') {
ctx.push(cx);
val = this.lv(val, ctx, partials);
ctx.pop();
}
return val;
};
// find values with normal names
proto.f = function(key, ctx, partials, returnFound) {
var val = false,
result = null,
found = false;
for (var i = ctx.length - 1; i >= 0; i--) {
result = getValue(ctx[i], key);
if (result.found) {
found = true;
val = result.val;
break;
}
}
if (!found) {
return (returnFound) ? false : "";
}
if (!returnFound && typeof val === 'function') {
val = this.lv(val, ctx, partials);
}
return val;
};
// Require Hogan.js if we need to.
})(typeof require === 'function' ? require('hogan.js') : Hogan);