forked from dlmanning/browserify-handlebars
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
158 lines (144 loc) · 5.5 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
var through = require('through');
var parser = require("node-html-parser");
var defaultPatterns = ['.tpl', '.mustache', '.hb', '.html'];
var defaultEngine = 'handlebars';
var isObject = function (a) {
return (!!a) && (a.constructor === Object);
};
var defaultEngines = {
handlebars: function (input, opts) {
var handlebars = require('handlebars');
var compilerOptions = opts.compilerOptions ?
opts.compilerOptions : {};
var out = ['var hb = require("handlebars/runtime");'];
if (isObject(input)) {
out.push('module.exports = {}');
Object.keys(input).forEach(function(key) {
var compiled = handlebars.precompile(input[key], compilerOptions);
out.push('module.exports["'+key+'"] = hb.template('+compiled +');')
});
} else {
var compiled = handlebars.precompile(input, compilerOptions);
"module.exports = hb.template(" + compiled + ");";
}
//'var templater = require("handlebars/runtime")["default"].template;' +
return out.join("\n");
},
hogan: function(input, opts) {
var hogan = require('jogan.js');
var defOpts = {
asString: 1,
enableHelpers: 1,
fixMethodCalls: 1
};
var hogOpts = opts.compilerOptions ? opts.compilerOptions : defOpts;
var out = ['var hogan = require("hogan.js");'];
if (isObject(input)) {
out.push('module.exports = {}');
out.push("var n = {};");
Object.keys(input).forEach(function (key) {
var compiled = hogan.compile(input[key], hogOpts);
out.push('module.exports["' + key + '"] = function(data, partials) {');
out.push(' var n = new hogan.Template(' + compiled + ');');
out.push(' return n.render(data, partials);');
out.push('};')
});
} else {
var compiled = hogan.compile(input, hogOpts);
out.push("var n = new hogan.Template(" + compiled + ");");
out.push("module.exports = function(data, partials) {");
out.push(' return n.render(data, partials);');
out.push('};');
}
return out.join("\n");
},
mustache: function(input, opts) {
var out = ['var mustache = require("mustache");'];
if (isObject(input)) {
out.push('module.exports = {}');
Object.keys(input).forEach(function (key) {
var compiled = JSON.stringify(input[key]);
out.push('module.exports["'+key+'"] = function(data) {');
out.push(' var tpl = ' + compiled + ';');
out.push(' return mustache.render(tpl, data);');
out.push('};');
});
} else {
var compiled = JSON.stringify(input);
out.push('module.exports = function(data) {');
out.push(' var tpl = ' + compiled + ';');
out.push(' mustache.parse(tpl);');
out.push(' return mustache.render(tpl, data);');
out.push('};');
}
return out.join("\n");
}
};
function patternMatcher(search, patterns) {
return patterns.find(function (el) {
return search.endsWith(el);
}) !== undefined;
}
function parseTemplate(content, opts) {
var type = opts.type || "text/x-handlebars-template";
var root = parser.parse(content, { script: true });
var scripts = root.querySelectorAll("script");
var templates = scripts.filter(function (element) {
return element.attributes["type"].toLowerCase() === type.toLowerCase();
});
var partials = {};
if (templates.length == 0) {
partials = content;
} else {
templates.forEach(function (tpl) {
partials[tpl.id] = tpl.innerHTML;
tpl.set_content("");
});
partials.root = root.toString();
}
return partials;
}
module.exports = function (file, opts) {
//check if user passes patterns to filter
var patterns = opts.patterns ? opts.patterns : defaultPatterns;
if (typeof patterns === "string") {
patterns = [patterns];
}
//if is not an array
if (!Array.isArray(patterns)) {
throw new Error('Option patterns must be a string or an array!');
}
// if path doesnt matches, just pass through
if (!patternMatcher(file, patterns)) return through();
//read data and passes to compiler
var input = '';
var write = function (buffer) {
input += buffer;
}
var end = function () {
this.queue(compileTemplate(input));
this.queue(null);
}
var compileTemplate = function (data) {
var engine = opts.engine ? opts.engine : defaultEngine;
var partials = opts.partials || true;
//handles parsing
var templates = data;
if (partials === true) {
templates = parseTemplate(data, opts);
}
//handle engines
if (typeof engine === "string") {
var engine = Object.keys(defaultEngines).find(function(key) {
return strEngine.toLowerCase() === key;
});
if (engine) {
return defaultEngines[engine](templates, opts);
}
} else if (typeof engine === 'function') {
//user is providing a custom function to compile and parse data
return engine(templates, opts.compilerOptions ? opts.compilerOptions : {});
}
}
return through(write, end);
}