From 91f62b08d9c159c247dabbb882c30f7558c6d406 Mon Sep 17 00:00:00 2001 From: Kaanon MacFarlane Date: Mon, 7 Dec 2015 16:55:49 -0800 Subject: [PATCH] Added windows support. More test cases --- index.js | 22 ++++++++++--- package.json | 2 +- test/partials/desktop/header-test.hbs | 1 + test/partials/header-test.hbs | 1 + test/partials/mobile/header-test.hbs | 1 + test.js => test/test.js | 47 ++++++++++++++++++++++++++- 6 files changed, 68 insertions(+), 6 deletions(-) create mode 100644 test/partials/desktop/header-test.hbs create mode 100644 test/partials/header-test.hbs create mode 100644 test/partials/mobile/header-test.hbs rename test.js => test/test.js (65%) diff --git a/index.js b/index.js index d258b5e..95c7b85 100644 --- a/index.js +++ b/index.js @@ -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) { @@ -36,15 +37,27 @@ 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); }; @@ -52,7 +65,7 @@ function handlebars(data, opts) { 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 { @@ -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); }); } diff --git a/package.json b/package.json index f21f485..fee9f46 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/partials/desktop/header-test.hbs b/test/partials/desktop/header-test.hbs new file mode 100644 index 0000000..3c8f0c3 --- /dev/null +++ b/test/partials/desktop/header-test.hbs @@ -0,0 +1 @@ +Desktop Header Goes Here \ No newline at end of file diff --git a/test/partials/header-test.hbs b/test/partials/header-test.hbs new file mode 100644 index 0000000..b1ede25 --- /dev/null +++ b/test/partials/header-test.hbs @@ -0,0 +1 @@ +Header Goes Here \ No newline at end of file diff --git a/test/partials/mobile/header-test.hbs b/test/partials/mobile/header-test.hbs new file mode 100644 index 0000000..54e3830 --- /dev/null +++ b/test/partials/mobile/header-test.hbs @@ -0,0 +1 @@ +Mobile Header Goes Here \ No newline at end of file diff --git a/test.js b/test/test.js similarity index 65% rename from test.js rename to test/test.js index f0f02bd..0742586 100644 --- a/test.js +++ b/test/test.js @@ -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( @@ -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']});