Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
 - <script>contents are emitted as Files
  • Loading branch information
bruderstein committed Apr 10, 2014
0 parents commit 337a991
Show file tree
Hide file tree
Showing 4 changed files with 504 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
141 changes: 141 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
var File = require('vinyl'),
cheerio = require('cheerio'),
through = require('through2'),
extend = require('extend'),
fs = require('fs'),
q = require('q');

module.exports = function(options) {
var defaults = {
presets: 'script',
includeHtmlInOutput: false,
createReadStream : fs.createReadStream
};

var presets = {
script : {
selector: 'script:not([data-ignore=true], [data-remove=true])',
getFileName: function(node) { return node.attr('src'); }
},
}

var selectedPresets = (options && options.presets && presets[options.presets]) ||
presets[defaults.presets];


options = extend({}, defaults, selectedPresets, options);


var streamToBuffer = function(stream) {
var buffers = [];
var deferred = q.defer();
var totalLength = 0;
stream.on('readable', function() {
data = stream.read();
if (data !== null) {
buffers.push(data);
totalLength += data.length;
}
});

stream.on('error', function(err) {
deferred.reject(err);
});

stream.on('end', function() {
deferred.resolve(Buffer.concat(buffers, totalLength));
});

return deferred.promise;
}

// Calls the callback for each matching in the contents, with an error object
// and the filename. callback(err, fileName).
// fileName === null signals the end of the matches
var transformFile = function(contents, callback) {
var $ = cheerio.load(contents.toString());
$(options.selector).each(function() {
var element = $(this);
var fileName = options.getFileName(element);
callback(null, fileName);
});

callback(null, null);
}


var transform = function(file, enc, callback) {
var stream = this;
var bufferReadPromises = [];

if (file.isNull()) {
// No contents - do nothing
stream.push(file);
callback();
}

if (file.isStream()) {
streamToBuffer(file.contents)
.then(function(contents) {

transformFile(contents, function(err, fileName) {
if (fileName) {
stream.push(new File({
cwd: file.cwd,
base: file.base,
path: file.base + fileName,
contents: options.createReadStream(file.base + fileName)
}));
} else {
if (options.includeHtmlInOutput) {
stream.push(file);
}
callback();
}

});

}, function(err) {
stream.emit('error', err);
});
}

if (file.isBuffer()) {

transformFile(file.contents, function(err, fileName) {
var createdStream;
if (fileName) {
try {
var readPromise = streamToBuffer(options.createReadStream(file.base + fileName))
.then(function(contents) {
stream.push(new File({
cwd: file.cwd,
base: file.base,
path: file.base + fileName,
contents: contents
}));
}, function(err) {
stream.emit('error', err);
});
bufferReadPromises.push(readPromise);
}
catch(err) {
stream.emit('error', err);
}

} else {
q.all(bufferReadPromises)
.then(function() {
// end of contents, no further matches for this file
if (options.includeHtmlInOutput) {
stream.push(file);
}
callback();
});
}
});
}
};

return through.obj(transform);
}
23 changes: 23 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "gulp-html-src",
"version": "0.0.1",
"description": "",
"main": "index.js",
"scripts": {
"test": "mocha"
},
"author": "Dave Brotherstone",
"license": "MIT",
"dependencies": {
"vinyl": "~0.2.3",
"through2": "~0.4.1",
"cheerio": "~0.13.1",
"extend": "~1.2.1",
"q": "~1.0.1"
},
"devDependencies": {
"mocha": "~1.18.2",
"chai": "~1.9.1",
"gulp-util": "~2.2.14"
}
}
Loading

0 comments on commit 337a991

Please sign in to comment.