-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
477 lines (430 loc) · 13.1 KB
/
index.js
File metadata and controls
477 lines (430 loc) · 13.1 KB
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
var _, anymatch, fs, handlebars, log, minify, path, yaml;
minify = require('html-minifier').minify;
yaml = require('yaml-front-matter');
handlebars = require('handlebars');
anymatch = require('anymatch');
path = require('path');
log = require('util').debuglog('htmlbrunch');
fs = require('fs');
_ = {
merge: require('lodash.merge')
};
var BasePartial;
BasePartial = class BasePartial {
constructor(filename, template, context, options) {
var ref;
this.filename = filename;
this.template = template;
this.context = context;
this.options = options;
this.dependencies = [];
if ((ref = this.options) != null ? ref.partials : void 0) {
this.dependencies = this.dependencies.concat(this.options.partials);
}
this.partials = [];
this.partialsCompiled = false;
}
addPartial(partial) {
return this.partials.push(partial);
}
compilePartials(htmlBrunchStatic, hbs, callback) {
var count, done, i, len, partial, ref, results;
if (this.partialsCompiled || this.partials.length === 0) {
callback();
return;
}
count = this.partials.length;
done = (err, content, dependencies) => {
if (err) {
count = -1;
callback(err);
}
if (count < 0) {
return;
}
// partial's compiler may add some dependencies
if (dependencies && dependencies.constructor === Array) {
this.dependencies = this.dependencies.concat(dependencies);
}
if (--count === 0) {
this.partialsCompiled = true;
return callback();
}
};
ref = this.partials;
results = [];
for (i = 0, len = ref.length; i < len; i++) {
partial = ref[i];
results.push(partial.compile(htmlBrunchStatic, hbs, done));
}
return results;
}
};
var HandlebarsBrunchStatic;
HandlebarsBrunchStatic = (function() {
class HandlebarsBrunchStatic {
constructor(config) {
if (config.constructor !== Boolean) {
if (config.constructor === Object) {
if (config.fileMatch) {
this.handles = config.fileMatch;
}
if (config.fileTransform) {
this.transformPath = config.fileTransform;
}
} else {
// deprecated functionality
this.handles = config;
}
}
}
transformPath(filename) {
return filename.replace(/\.static\.\w+$/, '.html');
}
compile(data, filename, options, callback) {
// do nothing because html-brunch-static processes through handlebars already
return callback(null, data);
}
};
HandlebarsBrunchStatic.prototype.handles = /\.static\.(?:hbs|handlebars)$/;
return HandlebarsBrunchStatic;
}).call(this);
var Partial;
Partial = class Partial extends BasePartial {
templateName() {
return path.basename(this.filename);
}
registerPartial(hbs) {
var ext, name, results;
name = this.templateName();
hbs.registerPartial(name, this.compiledPartial);
results = [];
while ((ext = path.extname(name)).length > 0) {
name = path.basename(name, ext);
results.push(hbs.registerPartial(name, this.compiledPartial));
}
return results;
}
compile(htmlBrunchStatic, hbs, callback) {
log(`COMPILING PARTIAL ${this.filename}`);
if (this.compiledPartial) {
this.registerPartial(hbs);
callback(null, this.compiledPartial, this.dependencies);
return;
}
return this.compilePartials(htmlBrunchStatic, hbs, (err) => {
var afterCompile, processor;
if (err) {
callback(err);
return;
}
processor = htmlBrunchStatic.getProcessor(this.filename);
if (!processor) {
processor = PassthruProcessor;
}
try {
afterCompile = (err, content, dependencies) => {
if (err) {
callback(err);
return;
}
// compiler may add dependencies
if (dependencies && dependencies.constructor === Array) {
this.dependencies = this.dependencies.concat(dependencies);
}
this.compiledPartial = content;
this.registerPartial(hbs);
return callback(null, content, this.dependencies);
};
if (processor.acceptsContext) {
return processor.compile(this.template, this.filename, this.options, {}, afterCompile);
} else {
return processor.compile(this.template, this.filename, this.options, afterCompile);
}
} catch (error) {
err = error;
return callback(err);
}
});
}
};
var PassthruProcessor;
PassthruProcessor = {
compile: function(data, filename, options, callback) {
return callback(null, data);
}
};
var Template;
Template = class Template extends BasePartial {
constructor(filename, template, context, options) {
var ref;
super(filename, template, context, options);
if ((ref = this.options) != null ? ref.layout : void 0) {
this.dependencies.push(this.options.layout);
}
}
// layouts have a content template
setContent(template) {
this.content = template;
// merge context
this.context = _.merge({}, template.context, this.context);
// merge dependencies
return this.dependencies = this.dependencies.concat(template.dependencies);
}
compile(htmlBrunchStatic, callback) {
var hbs, run;
log(`COMPILING TEMPLATE ${this.filename}`);
hbs = handlebars.create();
if (htmlBrunchStatic.handlebarsHelpers != null) {
hbs.registerHelper(htmlBrunchStatic.handlebarsHelpers);
}
run = () => {
return this.compilePartials(htmlBrunchStatic, hbs, (err) => {
var afterCompile, processor;
if (err) {
callback(err);
return;
}
processor = htmlBrunchStatic.getProcessor(this.filename);
if (!processor) {
processor = PassthruProcessor;
}
try {
afterCompile = (err, content, dependencies) => {
var hbsOptions, ref, result, template;
if (err) {
callback(err);
return;
}
// compiler may add dependencies
if (dependencies && dependencies.constructor === Array) {
this.dependencies = this.dependencies.concat(dependencies);
}
// process through handlebars
hbsOptions = _.merge({}, htmlBrunchStatic.handlebarsOptions, (ref = this.options) != null ? ref.handlebars : void 0);
try {
template = hbs.compile(content, hbsOptions);
result = template(this.context);
return callback(null, result);
} catch (error) {
err = error;
return callback(err);
}
};
if (processor.acceptsContext) {
return processor.compile(this.template, this.filename, this.options, this.context, afterCompile);
} else {
return processor.compile(this.template, this.filename, this.options, afterCompile);
}
} catch (error) {
err = error;
return callback(err);
}
});
};
if (this.content) {
return this.content.compile(htmlBrunchStatic, function(err, content) {
if (err) {
callback(err);
return;
}
hbs.registerHelper('content', function() {
return new handlebars.SafeString(content);
});
return run();
});
} else {
return run();
}
}
};
var TemplateLoader;
TemplateLoader = class TemplateLoader {
constructor() {
this.cache = {};
}
load(filename, data, defaultContext, content) {
var context, file, i, layout, len, options, partial, ref, template;
log(`LOAD ${filename}`);
if (this.cache[filename]) {
return this.cache[filename];
}
// read file, if no data
if (!data) {
data = fs.readFileSync(filename);
}
// parse the front matter
context = yaml.loadFront(data);
if (context instanceof Error) {
return context;
}
if (context === false) {
return new Error(`Could not parse ${filename}.`);
}
if (defaultContext) {
// pull out content and settings
context = _.merge({}, defaultContext, context);
}
template = context.__content;
options = context._options;
delete context.__content;
delete context._options;
template = new Template(filename, template, context, options);
if (content) {
template.setContent(content);
}
this.cache[filename] = template;
// load partials
if (options != null ? options.partials : void 0) {
ref = options.partials;
for (i = 0, len = ref.length; i < len; i++) {
file = ref[i];
partial = this.loadPartial(file);
if (partial instanceof Error) {
return partial;
}
template.addPartial(partial);
}
}
// if there's a layout, load that
if (options != null ? options.layout : void 0) {
layout = this.load(options.layout, null, defaultContext, template);
if (layout instanceof Error) {
return layout;
}
return layout;
}
return template;
}
loadPartial(filename) {
var child, context, data, file, i, len, options, partial, ref, ref1, template;
log(`LOAD PARTIAL ${filename}`);
if (this.cache[filename]) {
return this.cache[filename];
}
// parse front matter
data = fs.readFileSync(filename);
context = yaml.loadFront(data);
if (context instanceof Error) {
return context;
}
if (context === false) {
return new Error(`Could not parse ${filename}.`);
}
// create partial
template = context.__content;
options = context._options;
delete context.__content;
delete context._options;
partial = new Partial(filename, template, context, options);
this.cache[filename] = partial;
// load child partials
if ((ref = partial.options) != null ? ref.partials : void 0) {
ref1 = partial.options.partials;
for (i = 0, len = ref1.length; i < len; i++) {
file = ref1[i];
child = this.loadPartial(file);
if (child instanceof Error) {
return child;
}
partial.addPartial(child);
}
}
return partial;
}
};
var HtmlBrunchStatic;
HtmlBrunchStatic = class HtmlBrunchStatic {
constructor(config) {
var ref, ref1;
this.processors = (config != null ? config.processors : void 0) || [];
this.defaultContext = config != null ? config.defaultContext : void 0;
this.partials = (config != null ? config.partials : void 0) || /partials?/;
this.layouts = (config != null ? config.layouts : void 0) || /layouts?/;
this.handlebarsOptions = config != null ? config.handlebars : void 0;
if ((ref = this.handlebarsOptions) != null ? ref.enableProcessor : void 0) {
this.processors.push(new HandlebarsBrunchStatic(this.handlebarsOptions.enableProcessor));
delete this.handlebarsOptions.enableProcessor;
}
if ((ref1 = this.handlebarsOptions) != null ? ref1.helpers : void 0) {
this.handlebarsHelpers = this.handlebarsOptions.helpers;
delete this.handlebarsHelpers.helpers;
}
this.minify = (config != null ? config.minify : void 0) || false;
}
handles(filename) {
return this.getProcessor(filename) !== null;
}
getProcessor(filename) {
var map, processorIdx;
map = function(p) {
if (p.handles.constructor === Function) {
return function(f) {
return p.handles(f);
};
} else {
return p.handles;
}
};
processorIdx = anymatch(this.processors.map(map), filename, true);
if (processorIdx === -1) {
return null;
} else {
return this.processors[processorIdx];
}
}
transformPath(filename) {
var processor;
processor = this.getProcessor(filename);
if (!processor) {
return filename;
}
if (processor.transformPath) {
return processor.transformPath(filename);
} else {
return filename.replace(new RegExp(path.extname(filename) + '$'), '.html');
}
}
compile(data, filename, callback) {
var err, loader, template;
if (anymatch(this.partials, filename) || anymatch(this.layouts, filename)) {
// don't output partials and layouts
log(`Skipping ${filename}`);
callback();
return;
}
loader = new TemplateLoader;
template = loader.load(filename, data, this.defaultContext);
if (template instanceof Error) {
callback(template);
return;
}
try {
return template.compile(this, (err, content) => {
var result;
if (err) {
callback(err);
return;
}
if (this.minify) {
if (this.minify === true) {
content = minify(content);
} else {
content = minify(content, this.minify);
}
}
result = {
filename: this.transformPath(filename),
content: content
};
return callback(null, [result], template.dependencies);
});
} catch (error) {
err = error;
return callback(err);
}
}
};
module.exports = function(config) {
return new HtmlBrunchStatic(config);
};