Skip to content

Commit

Permalink
fixed an issue where grunt.file.isDir was used incorrectly to assume …
Browse files Browse the repository at this point in the history
…path as directory

added support for usePartials to be a string path
added support for options.data as external JSON files
fixed several test cases
added new tests
  • Loading branch information
Dan Lasky committed Feb 27, 2014
1 parent f8df443 commit 7639d5d
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 24 deletions.
29 changes: 25 additions & 4 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,36 @@ module.exports = function(grunt) {
},
partials: {
options: {
title:'title test',
body:'body test',
data: {
title:'title test',
body:'body test'
},
usePartials: true,
},
files: {
'tmp/':['test/fixtures/simple.html','test/fixtures/partials.html'],
},
},
partialsList: {
options: {
data: {
title:'title test',
body:'body test',
},
usePartials:'test/fixtures/simple.html'
},
files: {
'tmp/':['test/fixtures/extPartials.html'],
},
},
jsonData: {
options: {
data:'test/fixtures/data.json'
},
files: {
'tmp/extJSON.html':'test/fixtures/simple.html'
}
},
delimiters: {
options: {
data: {
Expand Down Expand Up @@ -87,7 +109,6 @@ module.exports = function(grunt) {
grunt.registerTask('test', ['clean', 'hogan_static', 'nodeunit']);

// By default, lint and run all tests.
//grunt.registerTask('default', ['jshint', 'test']);
grunt.registerTask('default', ['hogan_static']);
grunt.registerTask('default', ['jshint', 'test']);

};
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,16 @@ grunt.initConfig({
### Options

#### options.data
Type: `Object`
Type: `Object` or `String`
Default value: `undefined`

JSON object data to pass to hogan for templating
JSON object data to pass to hogan for templating. If data is passed as a string (including grunt expansion) JSON will be read and stuffed into options.data. note that subsequent json will overwrite any existing keys

#### options.usePartials
Type: `Boolean`
Type: `Boolean` or `String`
Default value: `false`

A Boolean that tells the task to cache discovered templates for use as partials
A Boolean that tells the task to cache discovered templates for use as partials. If the value is a string this is treated as a file path (including grunt expansion) to search for partials. In this use case it is assumed that files on the regular file path are NOT partials.

#### options.delimiters
Type: `String`
Expand All @@ -67,4 +67,4 @@ _a word of caution: this string is still subject to grunt templating so usage of
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using [Grunt](http://gruntjs.com/).

## Release History
_(Nothing yet)_
* 0.1.2 - fixed some issues with output paths. added external JSON support for data. added separate partial dir support.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "grunt-hogan-static",
"description": "compile static templates using hogan.js",
"version": "0.1.1",
"version": "0.1.2",
"homepage": "https://github.com/dlasky/grunt-hogan-static",
"author": {
"name": "Dan Lasky"
Expand All @@ -27,7 +27,8 @@
"test": "grunt test"
},
"dependencies":{
"hogan.js":"*"
"hogan.js":"*",
"object-extend":"*"
},
"devDependencies": {
"grunt-contrib-jshint": "~0.6.0",
Expand Down
53 changes: 43 additions & 10 deletions tasks/hogan_static.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
'use strict';

var hogan = require('hogan.js'),
path = require('path');
path = require('path'),
extend = require('object-extend');

module.exports = function(grunt) {

Expand All @@ -21,7 +22,41 @@ module.exports = function(grunt) {
//delimiters:'{{ }}',
disableLambda: false
}),
partials = {};
partials = {},
partialsList = [],
dataList = [];

if (typeof options.usePartials === "string") {
partialsList = grunt.file.expand(options.usePartials);
partialsList.forEach( function(partial) {
var name,
template;

if (!grunt.file.exists(partial)) {
grunt.log.warn('Source file "' + partial + '" not found.');
} else {
template = hogan.compile( grunt.file.read(partial) );
name = path.basename(partial).split(".")[0];
partials[name] = template;
}
});
}

if (typeof options.data === "string") {
dataList = grunt.file.expand(options.data);
options.data = {};
dataList.forEach(function(dataFile) {
var tempData;
if (!grunt.file.exists(dataFile)) {
grunt.log.warn('Source file "' + dataFile + '" not found.');
} else {
tempData = grunt.file.readJSON(dataFile);
extend(options.data, tempData);
}
});
}

console.dir(options.data);

this.files.forEach(function(f) {

Expand All @@ -44,8 +79,8 @@ module.exports = function(grunt) {
render = "",
template = hogan.compile( grunt.file.read(filepath), opts );

if (options.usePartials) {
name = path.basename(f.src).split(".")[0];
if (options.usePartials && typeof options.usePartials !== "string") {
name = path.basename(filepath).split(".")[0];
partials[name] = template;
}

Expand All @@ -58,14 +93,12 @@ module.exports = function(grunt) {

src.map(function(parsed) {
var render = parsed.template.render(options.data, partials);
if (grunt.file.isDir(f.dest)) {
grunt.file.write(f.dest + parsed.file, render);
grunt.log.writeln("Wrote file:" + f.dest + parsed.file);
} else {
//note this will overwrite for right now
//TODO: add concat or a concat flag
if (path.dirname(f.dest) === path.dirname(f.dest + "*")) {
grunt.file.write(f.dest, render);
grunt.log.writeln("Wrote file:" + f.dest);
} else {
grunt.file.write(f.dest + parsed.file, render);
grunt.log.writeln("Wrote file:" + f.dest + parsed.file);
}
});

Expand Down
2 changes: 2 additions & 0 deletions test/expected/partials.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
title test
body test
4 changes: 4 additions & 0 deletions test/fixtures/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"title":"title test",
"body":"body test"
}
1 change: 1 addition & 0 deletions test/fixtures/extPartials.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{>simple}}
4 changes: 2 additions & 2 deletions test/fixtures/simple.html
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
title test
body test
{{title}}
{{body}}
19 changes: 18 additions & 1 deletion test/hogan_static_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ exports.hogan_static = {
done();
},
default_options: function(test) {
test.expect(1);
test.expect(2);

var actual = grunt.file.read('tmp/simple.html'),
expected = grunt.file.read('test/expected/simple.html');
Expand All @@ -51,6 +51,23 @@ exports.hogan_static = {
test.done();
},

externalPartials: function(test) {
test.expect(1);
var actual = grunt.file.read('tmp/extPartials.html'),
expected = grunt.file.read('test/expected/partials.html');
test.equal(actual, expected, 'should properly handle partial templates.');

test.done();
},

externalJson: function(test) {
test.expect(1);
var actual = grunt.file.read('tmp/extJSON.html'),
expected = grunt.file.read('test/expected/simple.html');
test.equal(actual, expected, 'should properly handle external JSON data');
test.done();
},

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

Expand Down

0 comments on commit 7639d5d

Please sign in to comment.