Permalink
Browse files

allow use of exports.main for script file containing multiple templates

  • Loading branch information...
1 parent 09c4360 commit 0873585af443d934e455e90cb1f57c1448d32220 @mgutz committed May 26, 2012
Showing with 83 additions and 45 deletions.
  1. +3 −11 Cakefile
  2. +1 −1 index.js
  3. +48 −18 lib/funcd.js
  4. +7 −3 lib/funcdServerSide.js
  5. +1 −1 lib/index.js
  6. +1 −1 package.json
  7. +6 −2 src/lib/funcd.coffee
  8. +5 −0 src/test/funcdTest.coffee
  9. +11 −8 test/funcdTest.js
View
@@ -1,22 +1,14 @@
-asyncblock = require("asyncblock")
+o = require("shelljs")
task "build", "Builds the project", ->
- run "coffee --lint -c -o . src/"
+ o.exec "coffee --lint -c -o . src/"
writeHtml "src/test/index.funcd", "test/index.html"
task "test", "runs tests", ->
- run "mocha -u exports src/test/funcdTest.coffee"
+ o.exec "mocha -u exports src/test/funcdTest.coffee"
writeHtml = (file, out) ->
# lazily require because funcd may not compile cleanly while developing
Funcd = require(".")
Funcd.renderToFile file, out
-cp = require("child_process")
-
-run = (command) ->
- asyncblock (flow) ->
- cp.exec command, flow.add(['stdout', 'stderr'])
- results = flow.wait()
- console.log results.stdout if results.stdout?.length > 0
- console.error results.stderr if results.stderr?.length > 0
View
@@ -1,4 +1,4 @@
-// Generated by CoffeeScript 1.2.1-pre
+// Generated by CoffeeScript 1.3.3
(function() {
module.exports = require('./lib/index');
View
@@ -1,4 +1,4 @@
-// Generated by CoffeeScript 1.2.1-pre
+// Generated by CoffeeScript 1.3.3
(function() {
var DATA_FUNCD_ASYNC, Funcd, attributeList, defaultAttributes, doctypes, elements, escapeHtml, htmlChars, idSequence, mergeElements, mixinShortTag, mixinTag, nextId, rawContentElements, replaceToken, requireEx, tokensToReplace, _,
__slice = [].slice,
@@ -13,8 +13,12 @@
DATA_FUNCD_ASYNC = 'data-funcd';
requireEx = function(mod, nocache) {
- if (nocache == null) nocache = false;
- if (nocache) delete require.cache[require.resolve(mod)];
+ if (nocache == null) {
+ nocache = false;
+ }
+ if (nocache) {
+ delete require.cache[require.resolve(mod)];
+ }
return require(mod);
};
@@ -71,14 +75,18 @@
_ref = arg.split(' ');
for (_j = 0, _len1 = _ref.length; _j < _len1; _j++) {
element = _ref[_j];
- if (__indexOf.call(result, element) < 0) result.push(element);
+ if (__indexOf.call(result, element) < 0) {
+ result.push(element);
+ }
}
}
return result;
};
escapeHtml = function(value) {
- if (!value) return "";
+ if (!value) {
+ return "";
+ }
return value.replace(htmlChars, replaceToken);
};
@@ -98,7 +106,9 @@
attributeList = function(tag, obj) {
var attributes, list, name, val;
- if (obj == null) obj = {};
+ if (obj == null) {
+ obj = {};
+ }
attributes = defaultAttributes[tag] ? _.extend(_.clone(defaultAttributes[tag]), obj) : obj;
list = '';
for (name in attributes) {
@@ -124,18 +134,20 @@
return Funcd.prototype[tag] = function(attributes) {
var attrList;
attrList = "";
- if (_.isObject(attributes)) attrList = attributeList(tag, attributes);
+ if (_.isObject(attributes)) {
+ attrList = attributeList(tag, attributes);
+ }
return this.buffer += this.lead + ("<" + tag + attrList + "/>") + this.eol;
};
};
Funcd = (function() {
- Funcd.name = 'Funcd';
-
function Funcd(opts) {
var fn, name, self, _fn, _ref, _ref1;
- if (opts == null) opts = {};
+ if (opts == null) {
+ opts = {};
+ }
this.pretty = (_ref = opts.pretty) != null ? _ref : false;
this.options = opts;
self = this;
@@ -163,7 +175,9 @@
Funcd.prototype.applyAsyncCallbacks = function($parent) {
var pair, _i, _len, _ref, _results;
- if (!this.asyncCallbacks) return;
+ if (!this.asyncCallbacks) {
+ return;
+ }
_ref = this.asyncCallbacks;
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
@@ -239,7 +253,11 @@
options = arguments[0], template = arguments[1], args = 3 <= arguments.length ? __slice.call(arguments, 2) : [];
args = Array.prototype.slice.call(arguments);
first = args[0];
- if (_.isFunction(first)) {
+ if (_.isFunction(first.main)) {
+ template = first.main;
+ options = {};
+ args = args.slice(1);
+ } else if (_.isFunction(first)) {
template = first;
options = {};
args = args.slice(1);
@@ -327,7 +345,9 @@
if (arg[DATA_FUNCD_ASYNC]) {
asyncfn = arg[DATA_FUNCD_ASYNC];
delete arg[DATA_FUNCD_ASYNC];
- if (!arg.id) arg.id = nextId();
+ if (!arg.id) {
+ arg.id = nextId();
+ }
this.asyncCallbacks.push({
lambda: asyncfn,
id: arg.id
@@ -347,12 +367,22 @@
this.buffer += this.lead + ("<" + tag + attributes + ">" + this.eol);
}
if (parseBody) {
- if (this.pretty) this.lead += ' ';
- if (innerText.length > 0) this.buffer += this.lead + innerText + this.eol;
- if (innerHtmlFn != null) innerHtmlFn.apply(this);
- if (this.pretty) this.lead = this.lead.slice(0, -2);
+ if (this.pretty) {
+ this.lead += ' ';
+ }
+ if (innerText.length > 0) {
+ this.buffer += this.lead + innerText + this.eol;
+ }
+ if (innerHtmlFn != null) {
+ innerHtmlFn.apply(this);
+ }
+ if (this.pretty) {
+ this.lead = this.lead.slice(0, -2);
+ }
+ }
+ if (tag) {
+ return this.buffer += this.lead + ("</" + tag + ">" + this.eol);
}
- if (tag) return this.buffer += this.lead + ("</" + tag + ">" + this.eol);
};
return Funcd;
@@ -1,4 +1,4 @@
-// Generated by CoffeeScript 1.2.1-pre
+// Generated by CoffeeScript 1.3.3
(function() {
var Funcd, coffeescript, detectCallerPath, fs, path;
@@ -26,7 +26,9 @@
options = null;
}
code = inner;
- if (typeof code === "function") code = inner();
+ if (typeof code === "function") {
+ code = inner();
+ }
jscode = coffeescript.compile(code, options);
return this.script({
type: "text/javascript"
@@ -45,7 +47,9 @@
for (i = _i = 0, _len = _ref.length; _i < _len; i = ++_i) {
match = _ref[i];
path = match.match(/\(([^:]+)/)[1];
- if (path !== referencePath) return path;
+ if (path !== referencePath) {
+ return path;
+ }
}
return null;
};
View
@@ -1,4 +1,4 @@
-// Generated by CoffeeScript 1.2.1-pre
+// Generated by CoffeeScript 1.3.3
(function() {
module.exports = require("./funcdServerSide");
View
@@ -15,7 +15,7 @@
"coffee-script": "*"
},
"devDependencies": {
- "asyncblock": "",
+ "shelljs": "",
"chai": "",
"mocha": ""
},
@@ -194,7 +194,7 @@ class Funcd
# Renders a template function.
#
- # @param {Function} template
+ # @param {Object|Function|String} template
render: (template, args...) ->
if typeof template == 'function'
template @, args...
@@ -209,7 +209,11 @@ class Funcd
args = Array.prototype.slice.call(arguments)
first = args[0]
- if _.isFunction(first)
+ if _.isFunction(first.main)
+ template = first.main
+ options = {}
+ args = args.slice(1)
+ else if _.isFunction(first)
template = first
options = {}
args = args.slice(1)
@@ -7,6 +7,11 @@ module.exports =
"should have short tags": ->
assert.equal "<br/>", Funcd.render (t) -> t.br()
+ "should allow exports.main to be the default template function": ->
+ template =
+ main: (t) -> t.br()
+ assert.equal "<br/>", Funcd.render(template)
+
"should have full tags": ->
assert.equal "<div>foo</div>", Funcd.render (t) -> t.div "foo"
View
@@ -1,9 +1,9 @@
-// Generated by CoffeeScript 1.2.1-pre
+// Generated by CoffeeScript 1.3.3
(function() {
var Funcd, assert, fs,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
__hasProp = {}.hasOwnProperty,
- __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; };
+ __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; };
Funcd = require('..');
@@ -17,6 +17,15 @@
return t.br();
}));
},
+ "should allow exports.main to be the default template function": function() {
+ var template;
+ template = {
+ main: function(t) {
+ return t.br();
+ }
+ };
+ return assert.equal("<br/>", Funcd.render(template));
+ },
"should have full tags": function() {
assert.equal("<div>foo</div>", Funcd.render(function(t) {
return t.div("foo");
@@ -177,8 +186,6 @@
var Foo, foo, template;
Foo = (function() {
- Foo.name = 'Foo';
-
function Foo() {
this.bleh = __bind(this.bleh, this);
@@ -290,8 +297,6 @@
__extends(Layout, _super);
- Layout.name = 'Layout';
-
function Layout() {
return Layout.__super__.constructor.apply(this, arguments);
}
@@ -312,8 +317,6 @@
__extends(Page, _super);
- Page.name = 'Page';
-
function Page() {
return Page.__super__.constructor.apply(this, arguments);
}

0 comments on commit 0873585

Please sign in to comment.