From b409d7dc2c3ad152f930db3803e3b51198eb3b3b Mon Sep 17 00:00:00 2001 From: Rachel Carvalho Date: Sun, 4 Sep 2011 19:11:26 -0300 Subject: [PATCH] Added compile to js option to CLI. Closes #58. --- CHANGELOG.md | 1 + README.md | 6 +- examples/browser/decaf/Cakefile | 11 -- examples/browser/decaf/build | 3 + examples/browser/decaf/index.html | 2 +- examples/browser/decaf/template.js | 224 +++++++++++++++++++++++++- examples/browser/regular/Cakefile | 13 -- examples/browser/regular/build | 4 + examples/browser/regular/index.coffee | 2 +- examples/browser/regular/index.js | 2 +- examples/browser/regular/template.js | 224 +++++++++++++++++++++++++- src/cli.coffee | 40 +++-- 12 files changed, 488 insertions(+), 44 deletions(-) delete mode 100644 examples/browser/decaf/Cakefile create mode 100755 examples/browser/decaf/build delete mode 100644 examples/browser/regular/Cakefile create mode 100755 examples/browser/regular/build diff --git a/CHANGELOG.md b/CHANGELOG.md index b5ce5ac..6dbdc61 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ - Fixed #64 (`hardcode` option masked in express adapter). [smathy] - Added missing elements from the HTML 5 spec (both valid and obsolete). Closes #66 (thanks @aeosynth). + - Added compile to js option to CLI. Closes #58. [rachel-carvalho] **v0.3.0beta** (2011-07-27): diff --git a/README.md b/README.md index fad5997..8d4a7a5 100644 --- a/README.md +++ b/README.md @@ -186,7 +186,7 @@ On the browser: This is one of many browser deployment possibilities, pre-compiling your template on the server to a standalone function. To see all serving suggestions, check out [regular](http://github.com/mauricemach/coffeekup/blob/master/examples/browser/regular/index.html), [decaf](http://github.com/mauricemach/coffeekup/blob/master/examples/browser/decaf/index.html) and [crème](http://github.com/mauricemach/coffeekup/blob/master/examples/browser/creme/index.html). @@ -198,6 +198,8 @@ Command-line: Usage: coffeekup [options] path/to/template.coffee + --js compile template to js function + -n, --namespace global object holding the templates (default: "templates") -w, --watch watch templates for changes, and recompile -o, --output set the directory for compiled html -p, --print print the compiled html to stdout @@ -253,4 +255,4 @@ Latest version tested with node 0.4.9 and CoffeeScript 1.1.1. ## Special thanks - [Jeremy Ashkenas](https://github.com/jashkenas), for the amazing CoffeeScript language. - - [why the lucky stiff](Why_the_lucky_stiff), for the inspiration. \ No newline at end of file + - [why the lucky stiff](Why_the_lucky_stiff), for the inspiration. diff --git a/examples/browser/decaf/Cakefile b/examples/browser/decaf/Cakefile deleted file mode 100644 index 31ee370..0000000 --- a/examples/browser/decaf/Cakefile +++ /dev/null @@ -1,11 +0,0 @@ -exec = require('child_process').exec -fs = require 'fs' -coffekup = require 'coffeekup' - -task 'build', -> - fs.readFile 'template.coffee', 'utf-8', (err, data) -> - puts err if err - compiled = coffekup.compile data - str = String(compiled).replace 'function anonymous', 'function template' - fs.writeFile 'template.js', str, (err) -> - puts err if err diff --git a/examples/browser/decaf/build b/examples/browser/decaf/build new file mode 100755 index 0000000..649d077 --- /dev/null +++ b/examples/browser/decaf/build @@ -0,0 +1,3 @@ +#!/usr/bin/env sh +coffeekup --js template.coffee + diff --git a/examples/browser/decaf/index.html b/examples/browser/decaf/index.html index fb72f94..29f3aad 100644 --- a/examples/browser/decaf/index.html +++ b/examples/browser/decaf/index.html @@ -11,7 +11,7 @@ diff --git a/examples/browser/decaf/template.js b/examples/browser/decaf/template.js index 7d2f5a6..03cbd85 100644 --- a/examples/browser/decaf/template.js +++ b/examples/browser/decaf/template.js @@ -1 +1,223 @@ -function template(data) {var a,i,li,p,s,th,u,ul;a = function(){return __ck.tag('a', arguments);};i = function(){return __ck.tag('i', arguments);};li = function(){return __ck.tag('li', arguments);};p = function(){return __ck.tag('p', arguments);};s = function(){return __ck.tag('s', arguments);};th = function(){return __ck.tag('th', arguments);};u = function(){return __ck.tag('u', arguments);};ul = function(){return __ck.tag('ul', arguments);};var __slice = Array.prototype.slice;var __hasProp = Object.prototype.hasOwnProperty;var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };var __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; };var __indexOf = Array.prototype.indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (this[i] === item) return i; } return -1; }; var coffeescript, comment, doctype, h, tag, text, __ck, _base, _base2, _ref, _ref2, _ref3; if ((_ref = data.options) != null) { _ref; } else { data.options = {}; }; if ((_ref2 = (_base = data.options).format) != null) { _ref2; } else { _base.format = false; }; if ((_ref3 = (_base2 = data.options).autoescape) != null) { _ref3; } else { _base2.autoescape = false; }; __ck = { options: data.options, buffer: [], doctypes: { '5': '', 'xml': '', 'default': '', 'transitional': '', 'strict': '', 'frameset': '', '1.1': '', 'basic': '', 'mobile': '', 'ce': '' }, self_closing: ['area', 'base', 'basefont', 'br', 'col', 'frame', 'hr', 'img', 'input', 'link', 'meta', 'param'], render_attrs: function(obj) { var k, str, v; str = ''; for (k in obj) { v = obj[k]; str += " " + k + "=\"" + (this.esc(v)) + "\""; } return str; }, esc: function(txt) { if (this.options.autoescape) { return h(txt); } else { return String(txt); } }, tabs: 0, repeat: function(string, count) { return Array(count + 1).join(string); }, indent: function() { if (this.options.format) { return this.text(this.repeat(' ', this.tabs)); } }, tag: function(name, opts) { var o, result, _i, _j, _len, _len2; this.indent(); text("<" + name); for (_i = 0, _len = opts.length; _i < _len; _i++) { o = opts[_i]; if (typeof o === 'object') { text(this.render_attrs(o)); } } if (__indexOf.call(this.self_closing, name) >= 0) { text(' />'); if (this.options.format) { text('\n'); } } else { text('>'); for (_j = 0, _len2 = opts.length; _j < _len2; _j++) { o = opts[_j]; switch (typeof o) { case 'string': case 'number': text(this.esc(o)); break; case 'function': if (this.options.format) { text('\n'); } this.tabs++; result = o.call(data); if (typeof result === 'string') { this.indent(); text(this.esc(result)); if (this.options.format) { text('\n'); } } this.tabs--; this.indent(); } } text(""); if (this.options.format) { text('\n'); } } return null; } }; h = function(txt) { return String(txt).replace(/&/g, '&').replace(//g, '>').replace(/"/g, '"'); }; doctype = function(type) { if (type == null) { type = 5; } text(__ck.doctypes[type]); if (data.options.format) { return text('\n'); } }; text = function(txt) { __ck.buffer.push(String(txt)); return null; }; comment = function(cmt) { text(""); if (data.options.format) { return text('\n'); } }; tag = function() { var name; name = arguments[0]; delete arguments[0]; return __ck.tag(name, arguments); }; coffeescript = function(code) { return script(";(" + code + ")();"); }; (function(){ul(function() { var guy, _i, _len, _ref, _results; _ref = this.stooges; _results = []; for (_i = 0, _len = _ref.length; _i < _len; _i++) { guy = _ref[_i]; _results.push(li(guy)); } return _results;});}).call(data);return __ck.buffer.join('');} +(function(){ + this.templates || (this.templates = {}); + this.templates["template"] = function anonymous(data) { +var a,i,li,p,s,th,u,ul;a = function(){return __ck.tag('a', arguments);};i = function(){return __ck.tag('i', arguments);};li = function(){return __ck.tag('li', arguments);};p = function(){return __ck.tag('p', arguments);};s = function(){return __ck.tag('s', arguments);};th = function(){return __ck.tag('th', arguments);};u = function(){return __ck.tag('u', arguments);};ul = function(){return __ck.tag('ul', arguments);};var __slice = Array.prototype.slice;var __hasProp = Object.prototype.hasOwnProperty;var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };var __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; };var __indexOf = Array.prototype.indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (this[i] === item) return i; } return -1; }; + var coffeescript, comment, doctype, h, ie, tag, text, __ck, _ref, _ref2; + if (data == null) { + data = {}; + } + if ((_ref = data.format) != null) { + _ref; + } else { + data.format = false; + }; + if ((_ref2 = data.autoescape) != null) { + _ref2; + } else { + data.autoescape = false; + }; + __ck = { + buffer: [], + esc: function(txt) { + if (data.autoescape) { + return h(txt); + } else { + return String(txt); + } + }, + tabs: 0, + repeat: function(string, count) { + return Array(count + 1).join(string); + }, + indent: function() { + if (data.format) { + return text(this.repeat(' ', this.tabs)); + } + }, + tag: function(name, args) { + var combo, i, _i, _len; + combo = [name]; + for (_i = 0, _len = args.length; _i < _len; _i++) { + i = args[_i]; + combo.push(i); + } + return tag.apply(data, combo); + }, + render_idclass: function(str) { + var c, classes, i, id, _i, _j, _len, _len2, _ref3; + classes = []; + _ref3 = str.split('.'); + for (_i = 0, _len = _ref3.length; _i < _len; _i++) { + i = _ref3[_i]; + if (i.indexOf('#') === 0) { + id = i.replace('#', ''); + } else { + if (i !== '') { + classes.push(i); + } + } + } + if (id) { + text(" id=\"" + id + "\""); + } + if (classes.length > 0) { + text(" class=\""); + for (_j = 0, _len2 = classes.length; _j < _len2; _j++) { + c = classes[_j]; + if (c !== classes[0]) { + text(' '); + } + text(c); + } + return text('"'); + } + }, + render_attrs: function(obj) { + var k, v, _results; + _results = []; + for (k in obj) { + v = obj[k]; + if (typeof v === 'boolean' && v) { + v = k; + } + _results.push(v ? text(" " + k + "=\"" + (this.esc(v)) + "\"") : void 0); + } + return _results; + }, + render_contents: function(contents) { + var result; + switch (typeof contents) { + case 'string': + case 'number': + case 'boolean': + return text(this.esc(contents)); + case 'function': + if (data.format) { + text('\n'); + } + this.tabs++; + result = contents.call(data); + if (typeof result === 'string') { + this.indent(); + text(this.esc(result)); + if (data.format) { + text('\n'); + } + } + this.tabs--; + return this.indent(); + } + }, + render_tag: function(name, idclass, attrs, contents) { + this.indent(); + text("<" + name); + if (idclass) { + this.render_idclass(idclass); + } + if (attrs) { + this.render_attrs(attrs); + } + if (__indexOf.call(this.self_closing, name) >= 0) { + text(' />'); + if (data.format) { + text('\n'); + } + } else { + text('>'); + this.render_contents(contents); + text(""); + if (data.format) { + text('\n'); + } + } + return null; + } + }; + tag = function() { + var a, args, attrs, contents, idclass, name, _i, _len; + name = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : []; + for (_i = 0, _len = args.length; _i < _len; _i++) { + a = args[_i]; + switch (typeof a) { + case 'function': + contents = a; + break; + case 'object': + attrs = a; + break; + case 'number': + case 'boolean': + contents = a; + break; + case 'string': + if (args.length === 1) { + contents = a; + } else { + if (a === args[0]) { + idclass = a; + } else { + contents = a; + } + } + } + } + return __ck.render_tag(name, idclass, attrs, contents); + }; + h = function(txt) { + return String(txt).replace(/&/g, '&').replace(//g, '>').replace(/"/g, '"'); + }; + doctype = function(type) { + if (type == null) { + type = 'default'; + } + text(__ck.doctypes[type]); + if (data.format) { + return text('\n'); + } + }; + text = function(txt) { + __ck.buffer.push(String(txt)); + return null; + }; + comment = function(cmt) { + text(""); + if (data.format) { + return text('\n'); + } + }; + coffeescript = function(param) { + switch (typeof param) { + case 'function': + return script("" + __ck.coffeescript_helpers + "(" + param + ").call(this);"); + case 'string': + return script({ + type: 'text/coffeescript' + }, function() { + return param; + }); + case 'object': + param.type = 'text/coffeescript'; + return script(param); + } + }; + ie = function(condition, contents) { + __ck.indent(); + text(""); + if (data.format) { + return text('\n'); + } + }; + __ck.doctypes = {"5":"","default":"","xml":"","transitional":"","strict":"","frameset":"","1.1":"","basic":"","mobile":"","ce":""};__ck.coffeescript_helpers = "var __slice = Array.prototype.slice;var __hasProp = Object.prototype.hasOwnProperty;var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };var __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; };var __indexOf = Array.prototype.indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (this[i] === item) return i; } return -1; };";__ck.self_closing = ["area","base","br","col","command","embed","hr","img","input","keygen","link","meta","param","source","track","wbr","basefont","frame"];(function(){ul(function() { + var guy, _i, _len, _ref, _results; + _ref = this.stooges; + _results = []; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + guy = _ref[_i]; + _results.push(li(guy)); + } + return _results; +});}).call(data);return __ck.buffer.join(''); +}; +}).call(this); \ No newline at end of file diff --git a/examples/browser/regular/Cakefile b/examples/browser/regular/Cakefile deleted file mode 100644 index bb747bd..0000000 --- a/examples/browser/regular/Cakefile +++ /dev/null @@ -1,13 +0,0 @@ -exec = require('child_process').exec -fs = require 'fs' -coffekup = require 'coffeekup' - -task 'build', -> - fs.readFile 'template.coffee', 'utf-8', (err, data) -> - puts err if err - compiled = coffekup.compile data - str = String(compiled).replace 'function anonymous', 'function template' - fs.writeFile 'template.js', str, (err) -> - puts err if err - exec 'coffee -c index.coffee', (err) -> - puts err if err diff --git a/examples/browser/regular/build b/examples/browser/regular/build new file mode 100755 index 0000000..016fe15 --- /dev/null +++ b/examples/browser/regular/build @@ -0,0 +1,4 @@ +#!/usr/bin/env sh +coffeekup --js template.coffee +coffee -c index.coffee + diff --git a/examples/browser/regular/index.coffee b/examples/browser/regular/index.coffee index 1925238..7173baf 100644 --- a/examples/browser/regular/index.coffee +++ b/examples/browser/regular/index.coffee @@ -1,2 +1,2 @@ $().ready -> - $('body').append template(stooges: ['moe', 'larry', 'curly']) + $('body').append templates.template(stooges: ['moe', 'larry', 'curly']) diff --git a/examples/browser/regular/index.js b/examples/browser/regular/index.js index 14b8b30..f7a8ac5 100644 --- a/examples/browser/regular/index.js +++ b/examples/browser/regular/index.js @@ -1,6 +1,6 @@ (function() { $().ready(function() { - return $('body').append(template({ + return $('body').append(templates.template({ stooges: ['moe', 'larry', 'curly'] })); }); diff --git a/examples/browser/regular/template.js b/examples/browser/regular/template.js index 7d2f5a6..03cbd85 100644 --- a/examples/browser/regular/template.js +++ b/examples/browser/regular/template.js @@ -1 +1,223 @@ -function template(data) {var a,i,li,p,s,th,u,ul;a = function(){return __ck.tag('a', arguments);};i = function(){return __ck.tag('i', arguments);};li = function(){return __ck.tag('li', arguments);};p = function(){return __ck.tag('p', arguments);};s = function(){return __ck.tag('s', arguments);};th = function(){return __ck.tag('th', arguments);};u = function(){return __ck.tag('u', arguments);};ul = function(){return __ck.tag('ul', arguments);};var __slice = Array.prototype.slice;var __hasProp = Object.prototype.hasOwnProperty;var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };var __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; };var __indexOf = Array.prototype.indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (this[i] === item) return i; } return -1; }; var coffeescript, comment, doctype, h, tag, text, __ck, _base, _base2, _ref, _ref2, _ref3; if ((_ref = data.options) != null) { _ref; } else { data.options = {}; }; if ((_ref2 = (_base = data.options).format) != null) { _ref2; } else { _base.format = false; }; if ((_ref3 = (_base2 = data.options).autoescape) != null) { _ref3; } else { _base2.autoescape = false; }; __ck = { options: data.options, buffer: [], doctypes: { '5': '', 'xml': '', 'default': '', 'transitional': '', 'strict': '', 'frameset': '', '1.1': '', 'basic': '', 'mobile': '', 'ce': '' }, self_closing: ['area', 'base', 'basefont', 'br', 'col', 'frame', 'hr', 'img', 'input', 'link', 'meta', 'param'], render_attrs: function(obj) { var k, str, v; str = ''; for (k in obj) { v = obj[k]; str += " " + k + "=\"" + (this.esc(v)) + "\""; } return str; }, esc: function(txt) { if (this.options.autoescape) { return h(txt); } else { return String(txt); } }, tabs: 0, repeat: function(string, count) { return Array(count + 1).join(string); }, indent: function() { if (this.options.format) { return this.text(this.repeat(' ', this.tabs)); } }, tag: function(name, opts) { var o, result, _i, _j, _len, _len2; this.indent(); text("<" + name); for (_i = 0, _len = opts.length; _i < _len; _i++) { o = opts[_i]; if (typeof o === 'object') { text(this.render_attrs(o)); } } if (__indexOf.call(this.self_closing, name) >= 0) { text(' />'); if (this.options.format) { text('\n'); } } else { text('>'); for (_j = 0, _len2 = opts.length; _j < _len2; _j++) { o = opts[_j]; switch (typeof o) { case 'string': case 'number': text(this.esc(o)); break; case 'function': if (this.options.format) { text('\n'); } this.tabs++; result = o.call(data); if (typeof result === 'string') { this.indent(); text(this.esc(result)); if (this.options.format) { text('\n'); } } this.tabs--; this.indent(); } } text(""); if (this.options.format) { text('\n'); } } return null; } }; h = function(txt) { return String(txt).replace(/&/g, '&').replace(//g, '>').replace(/"/g, '"'); }; doctype = function(type) { if (type == null) { type = 5; } text(__ck.doctypes[type]); if (data.options.format) { return text('\n'); } }; text = function(txt) { __ck.buffer.push(String(txt)); return null; }; comment = function(cmt) { text(""); if (data.options.format) { return text('\n'); } }; tag = function() { var name; name = arguments[0]; delete arguments[0]; return __ck.tag(name, arguments); }; coffeescript = function(code) { return script(";(" + code + ")();"); }; (function(){ul(function() { var guy, _i, _len, _ref, _results; _ref = this.stooges; _results = []; for (_i = 0, _len = _ref.length; _i < _len; _i++) { guy = _ref[_i]; _results.push(li(guy)); } return _results;});}).call(data);return __ck.buffer.join('');} +(function(){ + this.templates || (this.templates = {}); + this.templates["template"] = function anonymous(data) { +var a,i,li,p,s,th,u,ul;a = function(){return __ck.tag('a', arguments);};i = function(){return __ck.tag('i', arguments);};li = function(){return __ck.tag('li', arguments);};p = function(){return __ck.tag('p', arguments);};s = function(){return __ck.tag('s', arguments);};th = function(){return __ck.tag('th', arguments);};u = function(){return __ck.tag('u', arguments);};ul = function(){return __ck.tag('ul', arguments);};var __slice = Array.prototype.slice;var __hasProp = Object.prototype.hasOwnProperty;var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };var __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; };var __indexOf = Array.prototype.indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (this[i] === item) return i; } return -1; }; + var coffeescript, comment, doctype, h, ie, tag, text, __ck, _ref, _ref2; + if (data == null) { + data = {}; + } + if ((_ref = data.format) != null) { + _ref; + } else { + data.format = false; + }; + if ((_ref2 = data.autoescape) != null) { + _ref2; + } else { + data.autoescape = false; + }; + __ck = { + buffer: [], + esc: function(txt) { + if (data.autoescape) { + return h(txt); + } else { + return String(txt); + } + }, + tabs: 0, + repeat: function(string, count) { + return Array(count + 1).join(string); + }, + indent: function() { + if (data.format) { + return text(this.repeat(' ', this.tabs)); + } + }, + tag: function(name, args) { + var combo, i, _i, _len; + combo = [name]; + for (_i = 0, _len = args.length; _i < _len; _i++) { + i = args[_i]; + combo.push(i); + } + return tag.apply(data, combo); + }, + render_idclass: function(str) { + var c, classes, i, id, _i, _j, _len, _len2, _ref3; + classes = []; + _ref3 = str.split('.'); + for (_i = 0, _len = _ref3.length; _i < _len; _i++) { + i = _ref3[_i]; + if (i.indexOf('#') === 0) { + id = i.replace('#', ''); + } else { + if (i !== '') { + classes.push(i); + } + } + } + if (id) { + text(" id=\"" + id + "\""); + } + if (classes.length > 0) { + text(" class=\""); + for (_j = 0, _len2 = classes.length; _j < _len2; _j++) { + c = classes[_j]; + if (c !== classes[0]) { + text(' '); + } + text(c); + } + return text('"'); + } + }, + render_attrs: function(obj) { + var k, v, _results; + _results = []; + for (k in obj) { + v = obj[k]; + if (typeof v === 'boolean' && v) { + v = k; + } + _results.push(v ? text(" " + k + "=\"" + (this.esc(v)) + "\"") : void 0); + } + return _results; + }, + render_contents: function(contents) { + var result; + switch (typeof contents) { + case 'string': + case 'number': + case 'boolean': + return text(this.esc(contents)); + case 'function': + if (data.format) { + text('\n'); + } + this.tabs++; + result = contents.call(data); + if (typeof result === 'string') { + this.indent(); + text(this.esc(result)); + if (data.format) { + text('\n'); + } + } + this.tabs--; + return this.indent(); + } + }, + render_tag: function(name, idclass, attrs, contents) { + this.indent(); + text("<" + name); + if (idclass) { + this.render_idclass(idclass); + } + if (attrs) { + this.render_attrs(attrs); + } + if (__indexOf.call(this.self_closing, name) >= 0) { + text(' />'); + if (data.format) { + text('\n'); + } + } else { + text('>'); + this.render_contents(contents); + text(""); + if (data.format) { + text('\n'); + } + } + return null; + } + }; + tag = function() { + var a, args, attrs, contents, idclass, name, _i, _len; + name = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : []; + for (_i = 0, _len = args.length; _i < _len; _i++) { + a = args[_i]; + switch (typeof a) { + case 'function': + contents = a; + break; + case 'object': + attrs = a; + break; + case 'number': + case 'boolean': + contents = a; + break; + case 'string': + if (args.length === 1) { + contents = a; + } else { + if (a === args[0]) { + idclass = a; + } else { + contents = a; + } + } + } + } + return __ck.render_tag(name, idclass, attrs, contents); + }; + h = function(txt) { + return String(txt).replace(/&/g, '&').replace(//g, '>').replace(/"/g, '"'); + }; + doctype = function(type) { + if (type == null) { + type = 'default'; + } + text(__ck.doctypes[type]); + if (data.format) { + return text('\n'); + } + }; + text = function(txt) { + __ck.buffer.push(String(txt)); + return null; + }; + comment = function(cmt) { + text(""); + if (data.format) { + return text('\n'); + } + }; + coffeescript = function(param) { + switch (typeof param) { + case 'function': + return script("" + __ck.coffeescript_helpers + "(" + param + ").call(this);"); + case 'string': + return script({ + type: 'text/coffeescript' + }, function() { + return param; + }); + case 'object': + param.type = 'text/coffeescript'; + return script(param); + } + }; + ie = function(condition, contents) { + __ck.indent(); + text(""); + if (data.format) { + return text('\n'); + } + }; + __ck.doctypes = {"5":"","default":"","xml":"","transitional":"","strict":"","frameset":"","1.1":"","basic":"","mobile":"","ce":""};__ck.coffeescript_helpers = "var __slice = Array.prototype.slice;var __hasProp = Object.prototype.hasOwnProperty;var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };var __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; };var __indexOf = Array.prototype.indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (this[i] === item) return i; } return -1; };";__ck.self_closing = ["area","base","br","col","command","embed","hr","img","input","keygen","link","meta","param","source","track","wbr","basefont","frame"];(function(){ul(function() { + var guy, _i, _len, _ref, _results; + _ref = this.stooges; + _results = []; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + guy = _ref[_i]; + _results.push(li(guy)); + } + return _results; +});}).call(data);return __ck.buffer.join(''); +}; +}).call(this); \ No newline at end of file diff --git a/src/cli.coffee b/src/cli.coffee index 213b946..a3deb65 100644 --- a/src/cli.coffee +++ b/src/cli.coffee @@ -9,26 +9,38 @@ options = null handle_error = (err) -> console.log err.stack if err -render = (input_path, output_directory) -> +compile = (input_path, output_directory, js, namespace = 'templates') -> fs.readFile input_path, 'utf-8', (err, contents) -> handle_error err - try - html = coffeekup.render contents, options - write input_path, html, output_directory - catch err - handle_error err -write = (input_path, html, output_directory) -> - filename = path.basename(input_path, path.extname(input_path)) + '.html' + name = path.basename input_path, path.extname(input_path) + + if not js + output = coffeekup.render contents, options + ext = '.html' + else + func = coffeekup.compile contents, options + output = """ + (function(){ + this.#{namespace} || (this.#{namespace} = {}); + this.#{namespace}[#{JSON.stringify name}] = #{func}; + }).call(this); + """ + ext = '.js' + + write input_path, name, output, output_directory, ext + +write = (input_path, name, contents, output_directory, ext) -> + filename = name + ext dir = output_directory or path.dirname input_path path.exists dir, (exists) -> unless exists then fs.mkdirSync dir, 0777 output_path = path.join dir, filename - html = ' ' if html.length <= 0 - fs.writeFile output_path, html, (err) -> + contents = ' ' if contents.length <= 0 + fs.writeFile output_path, contents, (err) -> handle_error err - puts html if options.print + puts contents if options.print puts "Compiled #{input_path}" if options.watch usage = ''' @@ -37,6 +49,8 @@ usage = ''' ''' switches = [ + ['--js', 'compile template to js function'] + ['-n', '--namespace [name]', 'global object holding the templates (default: "templates")'] ['-w', '--watch', 'watch templates for changes, and recompile'] ['-o', '--output [dir]', 'set the directory for compiled html'] ['-p', '--print', 'print the compiled html to stdout'] @@ -66,5 +80,5 @@ switches = [ if options.watch fs.watchFile file, {persistent: true, interval: 500}, (curr, prev) -> return if curr.size is prev.size and curr.mtime.getTime() is prev.mtime.getTime() - render file, options.output - else render file, options.output + compile file, options.output, options.js, options.namespace + else compile file, options.output, options.js, options.namespace