Skip to content

Commit 91f62b0

Browse files
author
Kaanon MacFarlane
committed
Added windows support. More test cases
1 parent 0af996f commit 91f62b0

File tree

6 files changed

+68
-6
lines changed

6 files changed

+68
-6
lines changed

index.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ var through = require('through2');
33
var Handlebars = require('handlebars');
44
var fs = require('fs');
55
var extend = require('util')._extend;
6+
var path = require('path');
67

78
function handlebars(data, opts) {
89

@@ -36,23 +37,35 @@ function handlebars(data, opts) {
3637
};
3738

3839
var partialName = function (filename, base) {
39-
var name = filename.substr(0, filename.lastIndexOf('.'));
40-
name = name.replace(new RegExp('^' + base + '\\/'), '');
41-
return name.substring(name.charAt(0) === '_' ? 1 : 0);
40+
var name = path.join(path.dirname(filename), path.basename(filename, path.extname(filename)));
41+
if (name.indexOf(base) === 0) {
42+
name = name.slice(base.length);
43+
}
44+
// Change the name of the partial to use / in the partial name, not \
45+
name = name.replace(/\\/g, '/');
46+
47+
// Remove leading _ and / character
48+
var firstChar = name.charAt(0);
49+
if( firstChar === '_' || firstChar === '/' ){
50+
name = name.substring(1);
51+
}
52+
53+
return name;
4254
};
4355

4456
var registerPartial = function (filename, base) {
4557
if (!isHandlebars(filename)) { return; }
4658
var name = partialName(filename, base);
4759
var template = fs.readFileSync(filename, 'utf8');
60+
4861
Handlebars.registerPartial(name, template);
4962
};
5063

5164
var registerPartials = function (dir, base, depth) {
5265
if (depth > maxDepth) { return; }
5366
base = base || dir;
5467
fs.readdirSync(dir).forEach(function (basename) {
55-
var filename = dir + '/' + basename;
68+
var filename = path.join(dir, basename);
5669
if (isDir(filename)) {
5770
registerPartials(filename, base);
5871
} else {
@@ -67,6 +80,7 @@ function handlebars(data, opts) {
6780
if(typeof options.batch === 'string') options.batch = [options.batch];
6881

6982
options.batch.forEach(function (dir) {
83+
dir = path.normalize(dir);
7084
registerPartials(dir, dir, 0);
7185
});
7286
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "gulp-compile-handlebars",
3-
"version": "0.5.0",
3+
"version": "0.6.0",
44
"description": "Compile Handlebars templates to file - gulp plugin",
55
"license": "MIT",
66
"repository": "kaanon/gulp-compile-handlebars",
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Desktop Header Goes Here

test/partials/header-test.hbs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Header Goes Here
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Mobile Header Goes Here

test.js renamed to test/test.js

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22
var assert = require('assert');
33
var gutil = require('gulp-util');
4-
var template = require('./index');
4+
var template = require('../index');
55

66
it('should compile Handlebars templates', function (cb) {
77
var stream = template(
@@ -49,6 +49,51 @@ it('should compile Handlebars templates, and ignore unknown partials', function
4949
stream.end();
5050
});
5151

52+
it('should compile Handlebars templates, and use batched partials', function (cb) {
53+
var stream = template({}, { batch: ['test/partials'] });
54+
55+
stream.on('data', function (data) {
56+
assert.equal(data.contents.toString(), 'Header Goes Here');
57+
cb();
58+
});
59+
60+
stream.write(new gutil.File({
61+
contents: new Buffer('{{> header-test}}')
62+
}));
63+
64+
stream.end();
65+
});
66+
67+
it('should compile Handlebars templates, and use batched NESTED partials', function (cb) {
68+
var stream = template({}, { batch: ['test/partials'] });
69+
70+
stream.on('data', function (data) {
71+
assert.equal(data.contents.toString(), 'Mobile Header Goes Here');
72+
cb();
73+
});
74+
75+
stream.write(new gutil.File({
76+
contents: new Buffer('{{> mobile/header-test}}')
77+
}));
78+
79+
stream.end();
80+
});
81+
82+
it('should compile Handlebars templates, and use multiple batched NESTED partials directories', function (cb) {
83+
var stream = template({}, { batch: ['test/partials/desktop', 'test/partials/mobile'] });
84+
85+
stream.on('data', function (data) {
86+
assert.equal(data.contents.toString(), 'Desktop Header Goes Here');
87+
cb();
88+
});
89+
90+
stream.write(new gutil.File({
91+
contents: new Buffer('{{> desktop/header-test}}')
92+
}));
93+
94+
stream.end();
95+
});
96+
5297

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

0 commit comments

Comments
 (0)