From 7644217addc0de491e6f098ab576178a6973bfca Mon Sep 17 00:00:00 2001 From: Chris Anderson Date: Fri, 2 Jan 2009 01:59:33 -0800 Subject: [PATCH] change it to !include --- lib/couchrest/commands/push.rb | 4 +-- lib/couchrest/helper/file_manager.rb | 2 +- .../helper/template-app/forms/example-form.js | 7 ++-- .../helper/template-app/lib/helpers/helper.js | 3 -- .../template-app/lib/helpers/template.js | 32 +++++++++++++++++++ .../{example-template.html => example.html} | 0 .../helper/template-app/views/example/map.js | 2 +- spec/couchapp_spec.rb | 7 ++-- 8 files changed, 43 insertions(+), 14 deletions(-) delete mode 100644 lib/couchrest/helper/template-app/lib/helpers/helper.js create mode 100644 lib/couchrest/helper/template-app/lib/helpers/template.js rename lib/couchrest/helper/template-app/lib/templates/{example-template.html => example.html} (100%) diff --git a/lib/couchrest/commands/push.rb b/lib/couchrest/commands/push.rb index a1b624f..a1dcc93 100644 --- a/lib/couchrest/commands/push.rb +++ b/lib/couchrest/commands/push.rb @@ -81,11 +81,11 @@ module CouchRest (for project global libs). These libraries are only inserted into views which include the text - //include-lib + // !include lib or - #include-lib + # !include lib Couchview is a result of scratching my own itch. I'd be happy to make it more general, so please contact me at jchris@grabb.it if you'd like to see anything diff --git a/lib/couchrest/helper/file_manager.rb b/lib/couchrest/helper/file_manager.rb index 0e01deb..f300cc9 100644 --- a/lib/couchrest/helper/file_manager.rb +++ b/lib/couchrest/helper/file_manager.rb @@ -171,7 +171,7 @@ module CouchRest def apply_lib(funcs, lib) funcs.each do |k,v| next unless v.is_a?(String) - funcs[k] = v.sub(/(\/\/|#)\ ?include-lib/,lib) + funcs[k] = v.sub(/(\/\/|#)\ ?!include lib/,lib) end end diff --git a/lib/couchrest/helper/template-app/forms/example-form.js b/lib/couchrest/helper/template-app/forms/example-form.js index 2ac1ce7..5f3079f 100644 --- a/lib/couchrest/helper/template-app/forms/example-form.js +++ b/lib/couchrest/helper/template-app/forms/example-form.js @@ -1,8 +1,11 @@ function(doc, req) { - // include-lib + // !include lib.templates + + // !require lib.helpers.template + respondWith(req, { html : function() { - var html = template(lib["example.html"], doc); + var html = template(lib.templates.example, doc); return {body:html} }, xml : function() { diff --git a/lib/couchrest/helper/template-app/lib/helpers/helper.js b/lib/couchrest/helper/template-app/lib/helpers/helper.js deleted file mode 100644 index 6cb1445..0000000 --- a/lib/couchrest/helper/template-app/lib/helpers/helper.js +++ /dev/null @@ -1,3 +0,0 @@ -function aHelperFunction() { - return "help"; -}; \ No newline at end of file diff --git a/lib/couchrest/helper/template-app/lib/helpers/template.js b/lib/couchrest/helper/template-app/lib/helpers/template.js new file mode 100644 index 0000000..03499b0 --- /dev/null +++ b/lib/couchrest/helper/template-app/lib/helpers/template.js @@ -0,0 +1,32 @@ +// Simple JavaScript Templating +// John Resig - http://ejohn.org/ - MIT Licensed +var cache = {}; + +function template(str, data){ + // Figure out if we're getting a template, or if we need to + // load the template - and be sure to cache the result. + var fn = cache[str] || + + // Generate a reusable function that will serve as a template + // generator (and which will be cached). + new Function("obj", + "var p=[],print=function(){p.push.apply(p,arguments);};" + + + // Introduce the data as local variables using with(){} + "with(obj){p.push('" + + + // Convert the template into pure JavaScript + str + .replace(/[\r\t\n]/g, " ") + .replace(/'(?=[^%]*%>)/g,"\t") + .split("'").join("\\'") + .split("\t").join("'") + .replace(/<%=(.+?)%>/g, "',$1,'") + .split("<%").join("');") + .split("%>").join("p.push('") + + "');}return p.join('');"); + cache[str] = fn; + + // Provide some basic currying to the user + return data ? fn( data ) : fn; +}; \ No newline at end of file diff --git a/lib/couchrest/helper/template-app/lib/templates/example-template.html b/lib/couchrest/helper/template-app/lib/templates/example.html similarity index 100% rename from lib/couchrest/helper/template-app/lib/templates/example-template.html rename to lib/couchrest/helper/template-app/lib/templates/example.html diff --git a/lib/couchrest/helper/template-app/views/example/map.js b/lib/couchrest/helper/template-app/views/example/map.js index fa794ef..e3fdd80 100644 --- a/lib/couchrest/helper/template-app/views/example/map.js +++ b/lib/couchrest/helper/template-app/views/example/map.js @@ -2,7 +2,7 @@ // and the list of keys it contains function(doc) { - // include-lib + // !include lib var k, keys = [] for (k in doc) keys.push(k); emit(doc._id, keys); diff --git a/spec/couchapp_spec.rb b/spec/couchapp_spec.rb index da0a807..45692b5 100644 --- a/spec/couchapp_spec.rb +++ b/spec/couchapp_spec.rb @@ -34,7 +34,7 @@ describe "couchapp" do end it "should create a forms and libs" do Dir["#{@fixdir}/my-app/forms/*"].select{|x|x =~ /example-form.js/}.length.should == 1 - Dir["#{@fixdir}/my-app/lib/templates/*"].select{|x|x =~ /example-template.html/}.length.should == 1 + Dir["#{@fixdir}/my-app/lib/templates/*"].select{|x|x =~ /example.html/}.length.should == 1 end end @@ -55,10 +55,7 @@ describe "couchapp" do @doc['views']['example']['map'].should match(/function/) end it "should create the view libs" do - @doc['views']['example']['map'].should match(/aHelperFunction/) - end - it "should create specific view libs" do - @doc['views']['example']['map'].should match(/aHelperFunction/) + @doc['views']['example']['map'].should match(/Resig/) end it "should create view for all the views" do `mkdir -p #{@fixdir}/my-app/views/more`