forked from kaanon/gulp-compile-handlebars
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
178 lines (147 loc) · 4.62 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
var gutil = require('gulp-util');
var through = require('through2');
var Handlebars = require('handlebars');
var fs = require('fs');
var extend = require('util')._extend;
var path = require('path');
function handlebars(data, opts) {
var options = opts || {};
var hb = handlebars.Handlebars;
//Go through a partials object
if(options.partials){
for(var p in options.partials){
hb.registerPartial(p, options.partials[p]);
}
}
//Go through a helpers object
if(options.helpers){
for(var h in options.helpers){
hb.registerHelper(h, options.helpers[h]);
}
}
// Do not search for more than 10 nestings
var maxDepth = 10;
// Process only files with given extension names
var allowedExtensions = ['hb', 'hbs', 'handlebars', 'html'];
var isDir = function (filename) {
var stats = fs.statSync(filename);
return stats && stats.isDirectory();
};
var isHandlebars = function (filename) {
return allowedExtensions.indexOf(filename.split('.').pop()) !== -1;
};
var partialName = function (filename, base) {
var name = path.join(path.dirname(filename), path.basename(filename, path.extname(filename)));
if (name.indexOf(base) === 0) {
name = name.slice(base.length);
}
// Change the name of the partial to use / in the partial name, not \
name = name.replace(/\\/g, '/');
// Remove leading _ and / character
var firstChar = name.charAt(0);
if( firstChar === '_' || firstChar === '/' ){
name = name.substring(1);
}
return name;
};
var registerPartial = function (filename, base) {
if (!isHandlebars(filename)) { return; }
var name = partialName(filename, base);
var template;
if(typeof opts.debugMode === "object") {
var startComment = "";
var endComment = "";
var logContext = "";
var logRootContext = "";
var logContextHelper = function(context) {
return new handlebars.Handlebars.SafeString(JSON.stringify(context))
};
if(typeof opts.debugMode.start === "string") {
startComment = opts.debugMode.start.replace("{{partial}}", name);
}
if(typeof opts.debugMode.end === "string") {
endComment = opts.debugMode.end.replace("{{partial}}", name);
}
if(typeof opts.debugMode.logContext === "string") {
var logHelperName = "logHelper_" + Date.now();
hb.registerHelper(logHelperName, logContextHelper);
logContext = opts.debugMode.logContext.replace("{{context}}", "{{" + logHelperName + " .}}");
}
template = startComment + logContext + fs.readFileSync(filename, 'utf8') + endComment;
} else {
template = fs.readFileSync(filename, 'utf8');
}
hb.registerPartial(name, template);
};
var registerPartials = function (dir, base, depth) {
if (depth > maxDepth) { return; }
base = base || dir;
fs.readdirSync(dir).forEach(function (basename) {
var filename = path.join(dir, basename);
if (isDir(filename)) {
registerPartials(filename, base);
} else {
registerPartial(filename, base);
}
});
};
// Go through a partials directory array
if(options.batch){
// Allow single string
if(typeof options.batch === 'string') options.batch = [options.batch];
options.batch.forEach(function (dir) {
dir = path.normalize(dir);
registerPartials(dir, dir, 0);
});
}
/**
* For handling unknown partials
* @method mockPartials
* @param {string} content Contents of handlebars file
*/
var mockPartials = function(content){
var regex = /{{> (.*)}}/gim, match, partial;
if(content.match(regex)){
while((match = regex.exec(content)) !== null){
partial = match[1];
//Only register an empty partial if the partial has not already been registered
if(!hb.partials.hasOwnProperty(partial)){
hb.registerPartial(partial, '');
}
}
}
};
return through.obj(function (file, enc, cb) {
var _data = extend({}, data);
if (file.isNull()) {
this.push(file);
return cb();
}
if (file.isStream()) {
this.emit('error', new gutil.PluginError('gulp-compile-handlebars', 'Streaming not supported'));
return cb();
}
try {
var fileContents = file.contents.toString();
if(options.ignorePartials){
mockPartials(fileContents);
}
// Enable gulp-data usage, Extend default data with data from file.data
if(file.data){
_data = extend(_data, file.data);
}
var template = hb.compile(fileContents, options.compile);
file.contents = new Buffer(template(_data));
} catch (err) {
this.emit('error', new gutil.PluginError('gulp-compile-handlebars', err));
}
this.push(file);
cb();
});
}
handlebars.reset = function(){
// Expose the Handlebars object
handlebars.Handlebars = Handlebars.create();
}
handlebars.reset();
module.exports = handlebars;