diff --git a/lib/helpers/raw.js b/lib/helpers/raw.js index 5a0bd006b..b04454be6 100644 --- a/lib/helpers/raw.js +++ b/lib/helpers/raw.js @@ -13,7 +13,7 @@ var TerraformError = exports.TerraformError = require("../error").TerraformError */ var processors = exports.processors = { - "html": ["jade", "ejs", "md"], + "html": ["jade", "ejs", "md", "hbs"], "css" : ["styl", "less", "scss", "sass"], "js" : ["coffee"] } diff --git a/lib/template/processors/hbs.js b/lib/template/processors/hbs.js new file mode 100644 index 000000000..3b201027e --- /dev/null +++ b/lib/template/processors/hbs.js @@ -0,0 +1,45 @@ +var Handlebars = require('handlebars') +var TerraformError = require("../../error").TerraformError + +module.exports = function(fileContents, options){ + + /** + * Provides support to load partials. + * + * @usage without locals + * {{partial '../foo.md'}} + * + * @example with provided locals + * {{partial '../foo.jade' locals='{ "a": "b" }'}} + * + * @returns {Handlebars.SafeString} HTML-safe rendered partial + */ + Handlebars.registerHelper('partial', function(filePath, options) { + var locals = options.hash.locals || {}; + if (typeof locals === 'string') locals = JSON.parse(locals) + return new Handlebars.SafeString(this.partial(filePath, locals)) + }); + + return { + compile: function(){ + return Handlebars.compile(fileContents.toString(), options) + }, + + parseError: function(error){ + + var arr = error.message.split("\n") + var path_arr = arr[0].split(":") + + error.lineno = parseInt(error.lineno || path_arr[path_arr.length -1] || -1) + error.message = arr[arr.length - 1] + error.name = error.name + error.source = "HBS" + error.dest = "HTML" + error.filename = error.path || options.filename + error.stack = fileContents.toString() + + return new TerraformError(error) + } + } + +} diff --git a/package.json b/package.json index d74702f8f..d3f96b532 100644 --- a/package.json +++ b/package.json @@ -37,7 +37,8 @@ "less": "2.5.0", "stylus": "0.47.3", "minify": "git://github.com/kennethormandy/minify#v0.3.0", - "autoprefixer": "5.1.0" + "autoprefixer": "5.1.0", + "handlebars": "^3.0.3" }, "devDependencies": { "mocha": "1.8.2", diff --git a/test/fixtures/templates/content.hbs b/test/fixtures/templates/content.hbs new file mode 100644 index 000000000..1a614b8d5 --- /dev/null +++ b/test/fixtures/templates/content.hbs @@ -0,0 +1 @@ +

Hello! This is {{!-- should be invisible --}}Handlebars!

diff --git a/test/fixtures/templates/hbs/partials-with-data.hbs b/test/fixtures/templates/hbs/partials-with-data.hbs new file mode 100644 index 000000000..f0ec17581 --- /dev/null +++ b/test/fixtures/templates/hbs/partials-with-data.hbs @@ -0,0 +1,2 @@ +

Hello Handlebars

+{{partial '../profile.jade' locals='{ "title": "Brock Whitten" }' }} diff --git a/test/fixtures/templates/hbs/partials.hbs b/test/fixtures/templates/hbs/partials.hbs new file mode 100644 index 000000000..ef4040556 --- /dev/null +++ b/test/fixtures/templates/hbs/partials.hbs @@ -0,0 +1,2 @@ +

Hello Handlebars

+{{partial '../stuff.md' }} diff --git a/test/fixtures/templates/index.jade b/test/fixtures/templates/index.jade index eee22fc56..19e13809e 100644 --- a/test/fixtures/templates/index.jade +++ b/test/fixtures/templates/index.jade @@ -3,7 +3,7 @@ h2 Hello World -!= partial("profile.jade") +!= partial("profile.jade", { title: "Brock Whitten" }) != partial("stuff.md") h4= place pre diff --git a/test/fixtures/templates/profile.jade b/test/fixtures/templates/profile.jade index 5d358cf2b..95b34cc6f 100644 --- a/test/fixtures/templates/profile.jade +++ b/test/fixtures/templates/profile.jade @@ -1 +1 @@ -h3 Brock Whitten \ No newline at end of file +h3= title diff --git a/test/helpers.js b/test/helpers.js index f7dbf3075..da2603190 100644 --- a/test/helpers.js +++ b/test/helpers.js @@ -9,8 +9,8 @@ describe("helpers", function(){ it('should return all possible file names for html ordered by priority.', function(done){ var list = polymer.helpers.buildPriorityList('index.html') list.should.be.an.instanceOf(Array) - list.should.have.lengthOf(6) - var plist = "index.jade, index.ejs, index.md, index.html.jade, index.html.ejs, index.html.md".split(', ') + list.should.have.lengthOf(8) + var plist = "index.jade, index.ejs, index.md, index.hbs, index.html.jade, index.html.ejs, index.html.md, index.html.hbs".split(', ') list.should.eql(plist) done() }) @@ -26,27 +26,28 @@ describe("helpers", function(){ it('should build priority list assuming template file when unknown.', function(done){ var list = polymer.helpers.buildPriorityList('feed.xml') list.should.be.an.instanceOf(Array) - list.should.have.lengthOf(3) - list.should.eql('feed.xml.jade, feed.xml.ejs, feed.xml.md'. split(', ')) + list.should.have.lengthOf(4) + list.should.eql('feed.xml.jade, feed.xml.ejs, feed.xml.md, feed.xml.hbs'. split(', ')) done() }) it('should look for templates on json files.', function(done){ var list = polymer.helpers.buildPriorityList('profile.json') list.should.be.an.instanceOf(Array) - list.should.have.lengthOf(3) + list.should.have.lengthOf(4) list.should.include('profile.json.jade') list.should.include('profile.json.ejs') list.should.include('profile.json.md') - list.should.eql('profile.json.jade, profile.json.ejs, profile.json.md'. split(', ')) + list.should.include('profile.json.hbs') + list.should.eql('profile.json.jade, profile.json.ejs, profile.json.md, profile.json.hbs'. split(', ')) done() }) it('should look for templates when no ext present.', function(done){ var list = polymer.helpers.buildPriorityList('appcache') list.should.be.an.instanceOf(Array) - list.should.have.lengthOf(3) - list.should.eql('appcache.jade, appcache.ejs, appcache.md'.split(', ')) + list.should.have.lengthOf(4) + list.should.eql('appcache.jade, appcache.ejs, appcache.md, appcache.hbs'.split(', ')) done() }) diff --git a/test/templates.js b/test/templates.js index 9bb436a1b..0d2ab0032 100644 --- a/test/templates.js +++ b/test/templates.js @@ -25,6 +25,40 @@ describe("templates", function(){ }) }) + describe(".hbs", function(){ + it("should render handlebars file", function(done){ + poly.render("content.hbs", function(error, body){ + should.exist(body) + body.should.include("

Hello! This is Handlebars!

") + done() + }) + }) + + it("should render partials", function(done){ + poly.render("hbs/partials.hbs", function(error, body){ + should.not.exist(error) + should.exist(body) + // from main file + body.should.include("

Hello Handlebars

") + // from markdown partial + body.should.include("

hello markdown

") + done() + }) + }) + + it("should pass data into partials", function(done){ + poly.render("hbs/partials-with-data.hbs", function(error, body){ + should.not.exist(error) + should.exist(body) + // from main file + body.should.include("

Hello Handlebars

") + // from jade partial + body.should.include("

Brock Whitten

") + done() + }) + }) + }) + describe(".md", function(){ it("should render markdown file", function(done){ poly.render("stuff.md", function(error, body){ @@ -114,10 +148,7 @@ describe("templates", function(){ poly.render("extend.jade", function(error, body){ should.not.exist(error) should.exist(body) - body.should.include("

Sintaxi

") - body.should.include("

Hello World

") - body.should.include("

Brock Whitten

") - body.should.include("

Vancouver

") + body.should.include("

hello markdown

") done() }) })