-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathindex.js
50 lines (42 loc) · 1.16 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
'use strict';
var map = require('map-stream');
var rext = require('replace-ext');
var PluginError = require('plugin-error');
var _ = require('lodash');
var compilers = {
creationix: require('haml'),
visionmedia: require('hamljs'),
};
module.exports = function(opts) {
var options = _.merge({
ext: '.html',
compiler: 'creationix',
compilerOpts: {}
}, opts);
// Map each file to this function
function hamlStream(file, cb) {
// Remember that contents is ALWAYS a buffer
if (file.isNull()) {
return cb(null, file); // pass along
}
if (file.isStream()) {
return cb(new PluginError('gulp-haml', 'Streaming not supported'));
}
try {
// Render using the selected compiler
var html = compilers[options.compiler]
.render(file.contents.toString('utf8'), options.compilerOpts);
} catch (e) {
return cb(new PluginError({
plugin: 'gulp-haml',
message: 'HAML conversion failed\n' + file.path +'\n'+ e
})
)
}
file.path = rext(file.path, options.ext);
file.contents = new Buffer(html);
cb(null, file);
}
// Return a stream
return map(hamlStream);
};