-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
275 lines (216 loc) · 6.56 KB
/
index.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
define([
'text',
'mustache',
'text!./lib/template.mustache'
], function (text, mustache, template) {
'use strict';
var PARTIAL_REG = '{{>\\s*([^\\s}]+)\\s*}}';
var PARTIAL_REG_ALL = new RegExp(PARTIAL_REG, 'igm');
var PARTIAL_REG_NAME = new RegExp(PARTIAL_REG, 'i');
var DEFAULTS = {
resolve: function (name) { return name; }
};
var id = 0;
var refs = [];
var written = [];
/**
* Asyncronously and recursively load template and nestled partials
* @param {String} name Template name
* @param {Function} parent Parent require instance
* @param {Function} onload Callback
* @param {Object} config RequireJS config object
* @return {void}
*/
function load(name, parent, onload, config) {
var options = config.mustache;
/**
* Extend config with default settings
*/
Object.keys(DEFAULTS).forEach(function (key) {
if (!options.hasOwnProperty(key)) {
options[key] = DEFAULTS[key];
}
});
/**
* Process the entry file and exit when done
*/
process(name, function (contents, partials) {
function render(data) {
return mustache.render(contents, (data || {}), partials);
}
/**
* Expose the underlying template string and partials
*/
render.text = contents;
render.partials = partials;
/**
* Expose render function as default export
*/
onload(render);
});
/**
* Load and parse given file for partials
* @param {String} name File name to load
* @param {Function} callback Callback when done
* @return {void}
*/
function process(name, callback) {
/**
* Load file using text plugin
*/
text.get(parent.toUrl(name), function (contents) {
var partials = {};
var all = dedupe(contents.match(PARTIAL_REG_ALL));
var queue = (all && all.length);
function done(contents, partials) {
var isParsed = refs.some(function (ref) { return ref.name === name; });
/**
* Save a reference for a later write if it is building
*/
if (config.isBuild && !isParsed) {
refs.push({
name: name,
id: ('partial_' + (id += 1)),
text: escapeTemplate(contents),
partials: Object.keys(partials).map(function (key, index, list) {
return {
name: options.resolve(key),
partial: key,
id: ('partial_' + (id += 1)),
is_last: (index === (list.length - 1)),
partials: false
};
})
});
}
callback(contents, partials);
}
if (!queue) {
done(contents, {});
return;
}
all.forEach(function (partial) {
/**
* Identify partial name
*/
var match = partial.match(PARTIAL_REG_NAME);
var name = (match && match[1]);
/**
* Exit if it couldn't be found
*/
if (!name) { return; }
/**
* Recursively parse partial
*/
process(options.resolve(name), function (child, childParts) {
partials[name] = child;
/**
* Extend this scope's `partials` with child's
*/
if (!config.isBuild) {
Object.keys(childParts).forEach(function (key) {
partials[key] = childParts[key];
});
}
/**
* Decrement the queue and exit if done
*/
queue -= 1;
if (queue === 0) {
done(contents, partials);
}
});
});
});
}
}
/**
* Return a new array without duplicates
* @param {Array} arr List of items to compare
* @return {Array} List without duplicates
*/
function dedupe(arr) {
if (!arr) { return arr; }
var uniq = [];
arr.forEach(function (item) {
if (uniq.indexOf(item) === -1) { uniq.push(item); }
});
return uniq;
}
/**
* Write template as module during build
* @param {String} pluginName Namespace for this plugin
* @param {String} moduleName Name of module being built
* @param {Function} write Callback for writing to file
* @return {void}
*/
function write(pluginName, moduleName, write) {
var exists = refs.some(function (ref) { return ref.name === moduleName; });
if (!exists) { return; }
return write(refs.reduce(function (str, ref) {
/**
* Expose plugin name to ref
*/
ref.plugin = pluginName;
/**
* Exit if this ref has already been written
*/
if (written.indexOf(ref.name) !== -1) {
return str;
}
/**
* Remember that it has now been written
*/
written.push(ref.name);
/**
* Render ref module and add it to the output string
*/
return (str + mustache.render(template, ref));
}, ''));
}
function escapeTemplate(text) {
return text.replace(/(['\\])/g, '\\$1')
.replace(/[\f]/g, '\\f')
.replace(/[\b]/g, '\\b')
.replace(/[\n]/g, '\\n')
.replace(/[\t]/g, '\\t')
.replace(/[\r]/g, '\\r')
.replace(/[\u2028]/g, '\\u2028')
.replace(/[\u2029]/g, '\\u2029');
}
/**
* Compiles a templates partials recursively
* Used by built templates to alleviate code bloat
* @param {Object} source Hash of partials (name/value<string|object>)
* @return {Object} All nestled partials in a flat name/value hash
*/
function compile(source) {
var partials = {};
/**
* Inherit partial from child partials
* @param {Object} obj Hash of partial (name/value<string|object>)
* @return {void}
*/
function inherit(obj) {
Object.keys(obj).forEach(function (key) {
var partial = obj[key];
/**
* Children that have already gone through this process
* exposes only a string as partial. Unprocessed templates exposes
* the render method with a template string attached to it. We'll need
* tot ake that intp concideration.
*/
partials[key] = (partial.text || partial);
/**
* Recursively parse partials
*/
if (partial.hasOwnProperty('partials')) {
inherit(partial.partials);
}
});
}
inherit(source);
return partials;
}
return { load: load, write: write, compile: compile };
});