Skip to content

Commit

Permalink
allow for arrays instead of globs. fixes #9
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickkettner committed Mar 9, 2014
1 parent 382440a commit 08446b3
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 10 deletions.
7 changes: 7 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ module.exports = function(grunt) {
templateData: 'test/fixtures/data.json',
output: 'tmp/dynamicPost.html'
},
anyArray: {
template: ['test/fixtures/deep/romanian.handlebars', 'test/fixtures/deep/german.handlebars'],
templateData: ['test/fixtures/deep/romanian.json', 'test/fixtures/deep/german.json'],
output: ['tmp/deep/romanian.html','tmp/deep/german.html'],
helpers: ['test/helpers/super_helper.js'],
partials: ['test/fixtures/deep/shared/foo.handlebars']
},
globbedTemplateAndOutput: {
template: 'test/fixtures/deep/**/*.handlebars',
templateData: 'test/fixtures/deep/**/*.json',
Expand Down
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ Heres a few of the ways you can use it
templateData: 'test/fixtures/data.json',
output: 'tmp/dynamicPost.html'
},
allArray: {
template: ['test/fixtures/deep/spanish.handlebars', 'test/fixtures/deep/deeper/portuguese.handlebars'],
templateData: ['test/fixtures/deep/spanish.handlebars', 'test/fixtures/deep/deeper/portuguese.json'],
output: ['tmp/deep/spanish.html','tmp/deep/deeper/portuguese.html'],
helpers: ['test/helpers/super_helper.js'],
partials: ['test/fixtures/deep/shared/foo.handlebars']
},
globbedTemplateAndOutput: {
template: 'test/fixtures/deep/**/*.handlebars',
templateData: 'test/fixtures/deep/**/*.json',
Expand All @@ -82,7 +89,7 @@ Heres a few of the ways you can use it
'test/globals/info.json',
'test/globals/textspec.json'
]
}
}
}
```

Expand Down
26 changes: 17 additions & 9 deletions tasks/compile-handlebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ module.exports = function(grunt) {
* and if so, returns the unglobbed
* version of the filename */
var isGlob = function(filename) {
if (!filename) return;
var match = filename.match(/[^\*]*/);
if (match[0] !== filename) {
return match.pop();
Expand All @@ -71,8 +72,9 @@ module.exports = function(grunt) {
* any globs are used, so the globbed outputs
* can be generated */
var getBasename = function(filename, template) {
var glob = isGlob(template);
var basename;
var basename, glob;
template = Array.isArray(template) ? filename : template;
glob = isGlob(template);
if (glob) {
basename = filename.slice(glob.length, filename.length).split('.');
basename.pop();
Expand All @@ -84,7 +86,13 @@ module.exports = function(grunt) {
return basename.join('.');
};

var getName = function(filename, basename) {
var getName = function(filename, basename, index) {
if (Array.isArray(filename)) {
var file = filename[index];
if (file) return file;
grunt.log.error('You need to assign the same number of ouputs as you do templates when using array notation.');
return;
}
if (typeof filename === 'object') {
return filename;
}
Expand Down Expand Up @@ -138,29 +146,29 @@ module.exports = function(grunt) {
handlebars.registerPartial(basename, require(fs.realpathSync(partial)));
});

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

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

if (config.globals) {
json = mergeJson(parseData(getName(templateData, basename)), config.globals);
json = mergeJson(parseData(getName(templateData, basename, index)), config.globals);
} else {
json = parseData(getName(templateData, basename));
json = parseData(getName(templateData, basename, index));
}

html += compiledTemplate(json);

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

grunt.file.write(getName(config.output, basename), html);
grunt.file.write(getName(config.output, basename, index), html);
});

process.nextTick(done);
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 @@ -53,6 +53,18 @@ exports.clean = {

test.done();
},
allArray: function(test) {
test.expect(2);
var germanActual = grunt.file.read('tmp/deep/german.html');
var romanianActual = grunt.file.read('tmp/deep/romanian.html');
var germanExpected = grunt.file.read('test/expected/german.html');
var romanianExpected = grunt.file.read('test/expected/romanian.html');

test.equal(germanActual, germanExpected);
test.equal(romanianActual, romanianExpected, 'array output should be working');

test.done();
},
globbedTemplateAndOutput: function(test) {
test.expect(2);

Expand Down
1 change: 1 addition & 0 deletions test/expected/german.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>Hallo, Welt</h1>
1 change: 1 addition & 0 deletions test/expected/romanian.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h2>Alo, mundo</h2>
1 change: 1 addition & 0 deletions test/fixtures/deep/german.handlebars
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>{{Anrede}}{{Zeichensetzung}} {{Lage}}</h1>
5 changes: 5 additions & 0 deletions test/fixtures/deep/german.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"Anrede": "Hallo",
"Zeichensetzung": ", ",
"Lage": "Welt"
}
1 change: 1 addition & 0 deletions test/fixtures/deep/romanian.handlebars
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h2>{{salut}}{{punctuație}} {{localizare}}</h2>
5 changes: 5 additions & 0 deletions test/fixtures/deep/romanian.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"salut": "Alo",
"punctuație": ",",
"localizare": "mundo"
}

0 comments on commit 08446b3

Please sign in to comment.