Skip to content

Commit

Permalink
Merge pull request #21 from kaanon/windows-support
Browse files Browse the repository at this point in the history
Added windows support. More test cases
  • Loading branch information
kaanon committed Dec 12, 2015
2 parents 0af996f + 91f62b0 commit be4973a
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 6 deletions.
22 changes: 18 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ 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) {

Expand Down Expand Up @@ -36,23 +37,35 @@ function handlebars(data, opts) {
};

var partialName = function (filename, base) {
var name = filename.substr(0, filename.lastIndexOf('.'));
name = name.replace(new RegExp('^' + base + '\\/'), '');
return name.substring(name.charAt(0) === '_' ? 1 : 0);
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 = fs.readFileSync(filename, 'utf8');

Handlebars.registerPartial(name, template);
};

var registerPartials = function (dir, base, depth) {
if (depth > maxDepth) { return; }
base = base || dir;
fs.readdirSync(dir).forEach(function (basename) {
var filename = dir + '/' + basename;
var filename = path.join(dir, basename);
if (isDir(filename)) {
registerPartials(filename, base);
} else {
Expand All @@ -67,6 +80,7 @@ function handlebars(data, opts) {
if(typeof options.batch === 'string') options.batch = [options.batch];

options.batch.forEach(function (dir) {
dir = path.normalize(dir);
registerPartials(dir, dir, 0);
});
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gulp-compile-handlebars",
"version": "0.5.0",
"version": "0.6.0",
"description": "Compile Handlebars templates to file - gulp plugin",
"license": "MIT",
"repository": "kaanon/gulp-compile-handlebars",
Expand Down
1 change: 1 addition & 0 deletions test/partials/desktop/header-test.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Desktop Header Goes Here
1 change: 1 addition & 0 deletions test/partials/header-test.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Header Goes Here
1 change: 1 addition & 0 deletions test/partials/mobile/header-test.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Mobile Header Goes Here
47 changes: 46 additions & 1 deletion test.js → test/test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
var assert = require('assert');
var gutil = require('gulp-util');
var template = require('./index');
var template = require('../index');

it('should compile Handlebars templates', function (cb) {
var stream = template(
Expand Down Expand Up @@ -49,6 +49,51 @@ it('should compile Handlebars templates, and ignore unknown partials', function
stream.end();
});

it('should compile Handlebars templates, and use batched partials', function (cb) {
var stream = template({}, { batch: ['test/partials'] });

stream.on('data', function (data) {
assert.equal(data.contents.toString(), 'Header Goes Here');
cb();
});

stream.write(new gutil.File({
contents: new Buffer('{{> header-test}}')
}));

stream.end();
});

it('should compile Handlebars templates, and use batched NESTED partials', function (cb) {
var stream = template({}, { batch: ['test/partials'] });

stream.on('data', function (data) {
assert.equal(data.contents.toString(), 'Mobile Header Goes Here');
cb();
});

stream.write(new gutil.File({
contents: new Buffer('{{> mobile/header-test}}')
}));

stream.end();
});

it('should compile Handlebars templates, and use multiple batched NESTED partials directories', function (cb) {
var stream = template({}, { batch: ['test/partials/desktop', 'test/partials/mobile'] });

stream.on('data', function (data) {
assert.equal(data.contents.toString(), 'Desktop Header Goes Here');
cb();
});

stream.write(new gutil.File({
contents: new Buffer('{{> desktop/header-test}}')
}));

stream.end();
});


it('should compile Handlebars templates with no helpers or partials', function (cb) {
var stream = template( {people: ['foo', 'bar']});
Expand Down

0 comments on commit be4973a

Please sign in to comment.