diff --git a/.gitignore b/.gitignore index 2fb4efe..a9b8e9a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ *.swp -*.gem \ No newline at end of file +*.gem +.bundle +Gemfile.lock \ No newline at end of file diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..f6a560f --- /dev/null +++ b/Gemfile @@ -0,0 +1,3 @@ +source :rubygems + +gemspec \ No newline at end of file diff --git a/coffee-views.gemspec b/coffee-views.gemspec index 6b9e6c0..6f2c2b1 100644 --- a/coffee-views.gemspec +++ b/coffee-views.gemspec @@ -13,8 +13,11 @@ Gem::Specification.new do |s| s.rdoc_options = %w(--charset=UTF-8) s.files = `git ls-files`.split("\n") + s.require_path = 'lib' s.add_runtime_dependency('coffee-script') - s.add_runtime_dependency('actionpack') + s.add_runtime_dependency('actionpack') + + s.add_development_dependency("rspec", ["~> 2.6"]) end \ No newline at end of file diff --git a/lib/coffee_views.rb b/lib/coffee_views.rb index 9858e95..a6f9993 100644 --- a/lib/coffee_views.rb +++ b/lib/coffee_views.rb @@ -1,3 +1,4 @@ +require 'action_view' require 'coffee-script' module CoffeeViews @@ -6,17 +7,22 @@ module CoffeeScript def self.erb_handler @@erb_handler ||= ActionView::Template.registered_template_handler(:erb) end + + def self.prepare source + source ||= "" + source.gsub! /<%==(.*?)%>/, '`<%==\1%>`' + source.gsub! /<%=([^=].*?)%>/, '`<%==(\1).to_json%>`' + source.gsub! /<%([^=].*?)%>/, '`<%\1%>`' + source.gsub! /#\{==(.*?)\}/, '#{`<%==\1%>`}' + source.gsub! /#\{=([^=].*?)\}/, '#{`<%==(\1).to_json%>`}' + source + end def self.call(template) - src = template.source - src = src.gsub(/<%=([^=].*?)%>/, '`<%==(\1).to_json%>`') - src = src.gsub(/<%([^=].*?)%>/, '`<%\1%>`') - src = src.gsub(/[^`]<%==(.*?)%>/, '`<%==\1%>`') - src = src.gsub(/#\{=(.*?)\}/, '#{`<%=(\1).to_json%>`}') - src = src.gsub(/#\{==(.*?)\}/, '#{`<%== \1 %>`}') - + source = self.prepare(template.source) + source = ::CoffeeScript.compile(source) # TODO: find how to set source back to template without instance_variable_set - template.instance_variable_set :@source, src + template.instance_variable_set :@source, source erb_handler.call(template) end diff --git a/spec/prepare_spec.rb b/spec/prepare_spec.rb new file mode 100644 index 0000000..a111bb3 --- /dev/null +++ b/spec/prepare_spec.rb @@ -0,0 +1,38 @@ +require 'spec_helper' + +describe CoffeeViews::Handlers::CoffeeScript do + + def prepare source + subject.prepare(source) + end + + describe "Without substitutions" do + it "should work with empty source" do + prepare('').should == '' + prepare(nil).should == '' + end + + it "should prepare sources" do + prepare("x").should == "x" + end + end + + describe "<%= code %>" do + it "should wrap with `` and call #to_json on code" do + prepare("<%=x%>").should == "`<%==(x).to_json%>`" + end + end + + describe "<%%>" do + it "should wrap with ``" do + prepare("<%x%>").should == "`<%x%>`" + end + end + + describe "<%==%>" do + it "should wrap with ``" do + prepare("<%==x%>").should == "`<%==x%>`" + end + end + +end \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..2c59cc4 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,4 @@ +$LOAD_PATH.unshift(File.dirname(__FILE__)) +$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib")) + +require 'coffee-views'