Permalink
Browse files

initial commit

  • Loading branch information...
Sam Breed
Sam Breed committed Nov 7, 2011
0 parents commit ba17dfd1c72ab305cb8a0f7281362bd286331d4e
Showing with 5,542 additions and 0 deletions.
  1. +7 −0 .gitignore
  2. +3 −0 .gitmodules
  3. +60 −0 Cakefile
  4. +14 −0 bin/tmpl.js
  5. +102 −0 example/templates.js
  6. +15 −0 example/templates/multiple.jst
  7. +7 −0 example/templates/sample.jst
  8. +111 −0 lib/index.js
  9. +25 −0 package.json
  10. +122 −0 readme.md
  11. +1,589 −0 test/handlebars.1.0.0.beta.2.js
  12. +1,493 −0 test/handlebars.1.0.0.beta.3.js
  13. +35 −0 test/index-spec.js
  14. +30 −0 test/index.html
  15. +484 −0 test/jquery.tmpl.js
  16. +148 −0 test/qunit/qunit.css
  17. +1,265 −0 test/qunit/qunit.js
  18. +32 −0 test/tests.js
@@ -0,0 +1,7 @@
+.DS_Store
+.rvmrc
+.bundle/*
+tmp/
+node_modules/
+.DS_Store?
+**/*/.DS_Store
@@ -0,0 +1,3 @@
+[submodule "vendor/jquery-tmpl"]
+ path = vendor/jquery-tmpl
+ url = https://github.com/jquery/jquery-tmpl.git
@@ -0,0 +1,60 @@
+###
+ Here's the example Cakefile for jquery-tmpl-jst.
+ Feel free to use it anywhere!
+###
+fs = require 'fs'
+util = require 'util'
+tmpl = require './lib/index.js'
+
+templatesDir = 'example/templates'
+targetDir = 'example'
+
+task 'watch', 'Watch prod source files and build changes', ->
+ invoke 'build'
+ util.log "Watching for changes in #{templatesDir}"
+ templates = fs.readdirSync(templatesDir)
+
+ for file in templates then do (file) ->
+ fs.watchFile "#{templatesDir}/#{file}", (curr, prev) ->
+ if +curr.mtime isnt +prev.mtime
+ util.log "Saw change in #{templatesDir}/#{file}"
+ invoke 'build'
+
+task 'build', 'Pre compile jQuery Templates', ->
+ tmpl.build templatesDir, ( data ) ->
+ tmpl.process( data, targetDir )
+
+handleError = (error) ->
+ util.log error
+ displayNotification error
+###
+ End Sample Cakefile
+###
+
+
+
+
+
+
+
+###
+ This is used to concat a custom build of the jquery-tmpl submodule,
+ and its output is committed
+###
+task 'build:dev', 'adds common js module support to jquery-tmpl', ->
+ files = ["lib/head.js", "vendor/jquery-tmpl/jquery.tmpl.js"]
+ data = []
+
+ for file, index in files then do (file, index)->
+
+ fs.readFile file, 'utf8', (err, fileContents) ->
+ handleError(err) if err
+ data[index] = fileContents
+
+ process( data.join('') ) if index is files.length - 1
+
+ process = ( data ) ->
+ fs.writeFile "lib/jquery.tmpl.js", data, 'utf8', (err) ->
+ handleError(err) if err
+ util.log "lib/jquery.tmpl.js written"
+
@@ -0,0 +1,14 @@
+#!/usr/bin/env node
+
+var tmpl = require("../lib/index");
+
+var args = process.argv.slice(0);
+// shift off node and script name
+args.shift(); args.shift();
+
+var target_dir = args[0] || process.cwd()
+ , output_dir = args[1] || process.cwd();
+
+tmpl.build( target_dir, function( data ){
+ tmpl.process( data, output_dir );
+});
@@ -0,0 +1,102 @@
+(function(){this.JST || (this.JST = {});this.JST["multiple"] = function(context) { return HandlebarsTemplates["multiple"](context); };this.HandlebarsTemplates || (this.HandlebarsTemplates = {});this.HandlebarsTemplates["multiple"] = Handlebars.template(function (Handlebars,depth0,helpers,partials,data) {
+ helpers = helpers || Handlebars.helpers;
+ var buffer = "", stack1, self=this, functionType="function", helperMissing=helpers.helperMissing, undef=void 0, escapeExpression=this.escapeExpression;
+
+
+ buffer += "<h1>";
+ stack1 = helpers.title || depth0.title
+ if(typeof stack1 === functionType) { stack1 = stack1.call(depth0, { hash: {} }); }
+ else if(stack1=== undef) { stack1 = helperMissing.call(depth0, "title", { hash: {} }); }
+ buffer += escapeExpression(stack1) + "</h1>\n\n";
+ return buffer;});}).call(this);(function(){this.JST || (this.JST = {});this.JST["multiple_footer"] = function(context) { return HandlebarsTemplates["multiple_footer"](context); };this.HandlebarsTemplates || (this.HandlebarsTemplates = {});this.HandlebarsTemplates["multiple_footer"] = Handlebars.template(function (Handlebars,depth0,helpers,partials,data) {
+ helpers = helpers || Handlebars.helpers;
+ var buffer = "", stack1, self=this, functionType="function", helperMissing=helpers.helperMissing, undef=void 0, escapeExpression=this.escapeExpression;
+
+
+ buffer += "\n<h1>";
+ stack1 = helpers.title || depth0.title
+ if(typeof stack1 === functionType) { stack1 = stack1.call(depth0, { hash: {} }); }
+ else if(stack1=== undef) { stack1 = helperMissing.call(depth0, "title", { hash: {} }); }
+ buffer += escapeExpression(stack1) + "</h1>\n\n";
+ return buffer;});}).call(this);(function(){this.JST || (this.JST = {});this.JST["multiple_foo_bar"] = function(context) { return HandlebarsTemplates["multiple_foo_bar"](context); };this.HandlebarsTemplates || (this.HandlebarsTemplates = {});this.HandlebarsTemplates["multiple_foo_bar"] = Handlebars.template(function (Handlebars,depth0,helpers,partials,data) {
+ helpers = helpers || Handlebars.helpers;
+ var buffer = "", stack1, stack2, tmp1, self=this, functionType="function", helperMissing=helpers.helperMissing, undef=void 0, escapeExpression=this.escapeExpression;
+
+function program1(depth0,data) {
+
+ var buffer = "", stack1, stack2;
+ buffer += "\n ";
+ stack1 = helpers.foo || depth0.foo
+ stack2 = helpers.each
+ tmp1 = self.program(2, program2, data);
+ tmp1.hash = {};
+ tmp1.fn = tmp1;
+ tmp1.inverse = self.noop;
+ stack1 = stack2.call(depth0, stack1, tmp1);
+ if(stack1 || stack1 === 0) { buffer += stack1; }
+ buffer += "\n";
+ return buffer;}
+function program2(depth0,data) {
+
+ var buffer = "", stack1;
+ buffer += "\n <div>";
+ stack1 = depth0;
+ if(typeof stack1 === functionType) { stack1 = stack1.call(depth0, { hash: {} }); }
+ else if(stack1=== undef) { stack1 = helperMissing.call(depth0, "this", { hash: {} }); }
+ buffer += escapeExpression(stack1) + "</div>\n ";
+ return buffer;}
+
+ buffer += "\n";
+ stack1 = helpers.foo || depth0.foo
+ stack2 = helpers['if']
+ tmp1 = self.program(1, program1, data);
+ tmp1.hash = {};
+ tmp1.fn = tmp1;
+ tmp1.inverse = self.noop;
+ stack1 = stack2.call(depth0, stack1, tmp1);
+ if(stack1 || stack1 === 0) { buffer += stack1; }
+ return buffer;});}).call(this);
+
+(function(){this.JST || (this.JST = {});this.JST["sample"] = function(context) { return HandlebarsTemplates["sample"](context); };this.HandlebarsTemplates || (this.HandlebarsTemplates = {});this.HandlebarsTemplates["sample"] = Handlebars.template(function (Handlebars,depth0,helpers,partials,data) {
+ helpers = helpers || Handlebars.helpers;
+ var buffer = "", stack1, stack2, tmp1, self=this, functionType="function", helperMissing=helpers.helperMissing, undef=void 0, escapeExpression=this.escapeExpression;
+
+function program1(depth0,data) {
+
+ var buffer = "", stack1, stack2;
+ buffer += "\n ";
+ stack1 = helpers.foo || depth0.foo
+ stack2 = helpers.each
+ tmp1 = self.program(2, program2, data);
+ tmp1.hash = {};
+ tmp1.fn = tmp1;
+ tmp1.inverse = self.noop;
+ stack1 = stack2.call(depth0, stack1, tmp1);
+ if(stack1 || stack1 === 0) { buffer += stack1; }
+ buffer += "\n";
+ return buffer;}
+function program2(depth0,data) {
+
+ var buffer = "", stack1;
+ buffer += "\n <div>";
+ stack1 = depth0;
+ if(typeof stack1 === functionType) { stack1 = stack1.call(depth0, { hash: {} }); }
+ else if(stack1=== undef) { stack1 = helperMissing.call(depth0, "this", { hash: {} }); }
+ buffer += escapeExpression(stack1) + "</div>\n ";
+ return buffer;}
+
+ buffer += "<h1>";
+ stack1 = helpers.title || depth0.title
+ if(typeof stack1 === functionType) { stack1 = stack1.call(depth0, { hash: {} }); }
+ else if(stack1=== undef) { stack1 = helperMissing.call(depth0, "title", { hash: {} }); }
+ buffer += escapeExpression(stack1) + "</h1>\n<p>This is a sample template</p>\n";
+ stack1 = helpers.foo || depth0.foo
+ stack2 = helpers['if']
+ tmp1 = self.program(1, program1, data);
+ tmp1.hash = {};
+ tmp1.fn = tmp1;
+ tmp1.inverse = self.noop;
+ stack1 = stack2.call(depth0, stack1, tmp1);
+ if(stack1 || stack1 === 0) { buffer += stack1; }
+ buffer += "\n";
+ return buffer;});}).call(this);
@@ -0,0 +1,15 @@
+<h1>{{title}}</h1>
+
+/*footer*/
+<h1>{{title}}</h1>
+
+/*foo_bar*/
+{{#if foo}}
+ {{#each foo}}
+ <div>{{this}}</div>
+ {{/each}}
+{{/if}}
+
+
+
+
@@ -0,0 +1,7 @@
+<h1>{{title}}</h1>
+<p>This is a sample template</p>
+{{#if foo}}
+ {{#each foo}}
+ <div>{{this}}</div>
+ {{/each}}
+{{/if}}
@@ -0,0 +1,111 @@
+var fs = require('fs')
+ , handlebars = require('handlebars')
+ , api = {};
+
+api.handleError = function( err ){
+ console.log(err);
+};
+
+api.process = function process( data, output_dir, callback ){
+ var nm = output_dir +'/templates.js';
+
+ fs.writeFile( nm, data.join('\n\n'), 'utf8', function( err ){
+ if( err ) api.handleError( err );
+ console.log(nm +' written.');
+ if( typeof callback == "function" ) callback();
+ });
+};
+
+api.build = function build( target_dir, callback ){
+ var templates = fs.readdirSync(target_dir)
+ , len = templates.length
+ , output = []
+ , readFiles
+ , subTemplate;
+
+ // Reads a file and appends it's template function to @output
+ build.readFile = function( index ){
+ var tmpl = templates[ index ]
+ , nm = tmpl.split('.')[0]
+ , path = target_dir +'/'+ tmpl;
+
+ if( /\.jst/.test(tmpl) === false ) return;
+
+ fs.readFile( path, 'utf8', function( err, file_contents ){
+ if( err ) return api.handleError();
+ var subs = build.subTemplate( file_contents );
+
+ if( subs ){
+ build.subTemplateString( nm, subs, index, file_contents );
+ } else {
+ output[index] = build.templateString( nm, file_contents );
+ }
+
+ if( index === 0 && typeof callback == "function" ){
+ return callback.call( api, output );
+ }
+ return;
+ });
+ };
+
+ // Returns a string of js with the compiled template
+ build.templateString = function( nm, file_contents ){
+ var func, compiled_hbs;
+
+ try {
+ // delete any cached versions of the template
+ console.log( 'Building '+ nm );
+
+ compiled_hbs = handlebars.precompile( file_contents );
+
+ func = nm.slice(0,1) == '_' ?
+ [
+ '(function() {',
+ 'Handlebars.registerPartial("'+ nm +'", Handlebars.template('+ compiled_hbs +'));',
+ '}).call(this);'
+ ] :
+ [ '(function(){',
+ 'this.JST || (this.JST = {});',
+ 'this.JST["'+ nm +'"] = function(context) { return HandlebarsTemplates["'+ nm +'"](context); };',
+ 'this.HandlebarsTemplates || (this.HandlebarsTemplates = {});',
+ 'this.HandlebarsTemplates["'+ nm +'"] = Handlebars.template('+ compiled_hbs +');',
+ '}).call(this);' ];
+
+ } catch( e ){
+ console.log( 'Error processing'+ nm, e);
+ return false;
+ }
+
+ return func.join('');
+ };
+
+ // Parses a raw template file and extracts subtemplates
+ build.subTemplate = function( file_contents ){
+ var find_subs = /\/\*\s?(\w+)\s?\*\//
+ , subs = file_contents.trim().split( find_subs );
+
+ return subs.length > 1 && subs.length % 2 ? subs : false;
+ };
+
+ // Builds multi part template string from subtemplates
+ build.subTemplateString = function( nm, subs, index, file_contents ){
+ output[index] = "";
+
+ var i = 0
+ , l = subs.length
+ , name;
+
+ for(; i < l; i += 2){
+ name = subs[ i - 1 ] != null ? nm +'_'+ subs[ i - 1] : nm;
+ output[index] += build.templateString( name, subs[ i ] );
+ }
+ return;
+ };
+
+ // Process each file in the target_directory
+ while(len--){
+ build.readFile(len);
+ }
+};
+
+module.exports = api;
@@ -0,0 +1,25 @@
+{
+ "author": "wookiehangover <sam@quickleft.com>",
+ "name": "jquery-tmpl-jst",
+ "description": "Pre-compiled jQuery Templates",
+ "version": "0.0.6",
+ "repository": {
+ "type": "git",
+ "url": "github.com:wookiehangover/jquery-tmpl-jst.git"
+ },
+ "engines": {
+ "node": ">=0.4.7"
+ },
+ "dependencies": {
+ "handlebars": "1.0.2beta"
+ },
+ "devDependencies": {
+ "jasmine-node": "~1.0.6"
+ },
+ "homepage": "https://github.com/wookiehangover/jquery-tmpl-jst",
+ "main": "./lib/index",
+ "scripts": {
+ "test": "jasmine-node ./test"
+ },
+ "bin": { "tmpl": "./bin/tmpl.js" }
+}
Oops, something went wrong.

0 comments on commit ba17dfd

Please sign in to comment.