Skip to content

Commit

Permalink
Merge pull request #6 from RobinQu/single-commit
Browse files Browse the repository at this point in the history
adding support for partial,helpers; fixes for jslint
  • Loading branch information
patrickkettner committed Oct 22, 2013
2 parents 5dcea8e + 416625a commit 6e30278
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 17 deletions.
4 changes: 3 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ module.exports = function(grunt) {
globbedTemplateAndOutput: {
template: 'test/fixtures/deep/**/*.handlebars',
templateData: 'test/fixtures/deep/**/*.json',
output: 'tmp/deep/**/*.html'
output: 'tmp/deep/**/*.html',
helpers: 'test/helpers/**/*.js',
partials: 'test/fixtures/deep/shared/**/*.handlebars'
}
},

Expand Down
52 changes: 38 additions & 14 deletions tasks/compile-handlebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ module.exports = function(grunt) {
if (grunt.file.expand(config).length) {
return grunt.file.expand(config);
}
else {
return [config];
}
return [config];
};

/* Guesses the file extension based on
Expand All @@ -33,7 +31,9 @@ module.exports = function(grunt) {
var extension = filepath.split('/').pop().split('.');
extension.shift();
extension = extension.join('.');
if (extension) extension = '.' + extension;
if (extension) {
extension = '.' + extension;
}
return extension;
};

Expand All @@ -42,7 +42,9 @@ module.exports = function(grunt) {
var parseData = function(data) {
/* grunt.file chokes on objects, so we
* check for it immiedietly */
if (grunt.util.kindOf(data) === 'object') return data;
if (grunt.util.kindOf(data) === 'object') {
return data;
}

/* data isn't an object, so its probably
* a file. */
Expand All @@ -59,7 +61,9 @@ module.exports = function(grunt) {
* version of the filename */
var isGlob = function(filename) {
var match = filename.match(/[^\*]*/);
if (match[0] !== filename) return match.pop();
if (match[0] !== filename) {
return match.pop();
}
};

/* Figures out the name of the file before
Expand All @@ -80,28 +84,48 @@ module.exports = function(grunt) {
};

var getName = function(filename, basename) {
if (grunt.util.kindOf(filename) === 'object') return filename;
if (grunt.file.exists(filename)) return filename;
if (isGlob(filename)) return isGlob(filename) + basename + filetype(filename);
if (grunt.util.kindOf(filename) === 'object') {
return filename;
}
if (grunt.file.exists(filename)) {
return filename;
}
if (isGlob(filename)) {
return isGlob(filename) + basename + filetype(filename);
}
return filename;
};

grunt.registerMultiTask('compile-handlebars', 'Compile Handlebars templates ', function() {
var path = require('path');
var config = this.data;
var templates = getConfig(config.template);
var templateData = config.templateData;

var helpers = config.helpers ? getConfig(config.helpers): [];
var partials = config.partials ? getConfig(config.partials) : [];

helpers.forEach(function (helper) {
var basename = getBasename(helper, config.helpers);
handlebars.registerHelper(basename, require(path.resolve(helper)));
});

partials.forEach(function (partial) {
var basename = getBasename(partial, config.partials);
handlebars.registerPartial(basename, require(path.resolve(partial)));
});

templates.forEach(function(template) {
var compiledTemplate = handlebars.compile(parseData(template));
var basename = getBasename(template, config.template);
var html = '';

if (config.preHTML) html += parseData(getName(config.preHTML, basename));

if (config.preHTML) {
html += parseData(getName(config.preHTML, basename));
}
html += compiledTemplate(parseData(getName(templateData, basename)));

if (config.postHTML) html += parseData(getName(config.postHTML, basename));
if (config.postHTML) {
html += parseData(getName(config.postHTML, basename));
}

grunt.file.write(getName(config.output, basename), html);
});
Expand Down
12 changes: 12 additions & 0 deletions test/compile_handlebars_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ exports.clean = {

test.equal(deepActual, deepExpected, 'Deeply globbed files should generated equally named output files');

test.done();
},

helperAndPartial: function (test) {
test.expect(1);

var actual = grunt.file.read('tmp/deep/helperAndPartial.html');
var expected = grunt.file.read('test/expected/helperAndPartial.html');

test.equal(actual, expected, 'Helpers and partials should be corrected renderred');

test.done();
}

};
2 changes: 1 addition & 1 deletion test/expected/deep/spanish.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<h1>hola, mundo</h1>
<h1>hola, mundo</h1>
2 changes: 2 additions & 0 deletions test/expected/helperAndPartial.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
super helper!
foo partial
2 changes: 2 additions & 0 deletions test/fixtures/deep/helperAndPartial.handlebars
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{{super_helper}}
{{> foo}}
3 changes: 3 additions & 0 deletions test/fixtures/deep/helperAndPartial.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"hello": "world"
}
1 change: 1 addition & 0 deletions test/fixtures/deep/shared/foo.handlebars
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo partial
2 changes: 1 addition & 1 deletion test/fixtures/deep/spanish.handlebars
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<h1>{{saludo}}{{puntuacion}} {{ubicacion}}</h1>
<h1>{{saludo}}{{puntuacion}} {{ubicacion}}</h1>
5 changes: 5 additions & 0 deletions test/helpers/super_helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = function (context, option) {

return "super helper!";

};

0 comments on commit 6e30278

Please sign in to comment.