This repository has been archived by the owner on Dec 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
52 lines (40 loc) · 1.38 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
'use strict';
var libxslt = require('libxslt');
var es = require('event-stream');
var PluginError = require('plugin-error');
var fs = require('fs');
module.exports = function(template, config) {
if (!template) throw new Error('Template option missing');
var parameters = config || {};
var stylesheet;
try {
var contents = fs.readFileSync(template);
// XXX: using #parse() directly fails with a coredump ...
var stylesheetRaw = libxslt.libxmljs.parseXml(contents);
stylesheet = libxslt.parse(stylesheetRaw);
} catch (e) {
throw new Error(e.message);
}
function modifyContents(file, cb) {
function throwError(message) {
return cb(new PluginError('gulp-xslt', message));
}
if (file.isNull()) {
return cb(null, file);
}
if (file.isStream()) {
return throwError('Streaming not supported');
}
if (file.isBuffer()) {
try {
var document = libxslt.libxmljs.parseXml(file.contents);
var output = stylesheet.apply(document, parameters, {outputFormat: 'string', noWrapParams: true});
file.contents = Buffer.from(output);
} catch (e) {
return throwError(e.message);
}
}
return cb(null, file);
}
return es.map(modifyContents);
};