coffee-views 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +3 -0
- data/README.md +13 -0
- data/coffee-views.gemspec +1 -1
- data/lib/coffee_views.rb +23 -8
- data/spec/preprocess_spec.rb +50 -0
- data/spec/spec_helper.rb +4 -0
- metadata +11 -8
data/Gemfile
ADDED
data/README.md
CHANGED
@@ -1,8 +1,21 @@
|
|
1
1
|
CoffeeViews
|
2
2
|
=================
|
3
|
+
Inspired by [coffeebeans](https://github.com/markbates/coffeebeans) but with fixes:
|
4
|
+
|
5
|
+
1. Doesn't compile coffeescript everytime in production
|
6
|
+
2. Smart replacements for `<%=%>`
|
7
|
+
|
8
|
+
Use #to_json in safe mode
|
9
|
+
|
10
|
+
<%= x %> # replaced with `<%== (x).to_json %>`
|
11
|
+
|
12
|
+
Use raw mode in raw mode
|
13
|
+
|
14
|
+
<%== x %> # replaced with `<%== x %>`
|
3
15
|
|
4
16
|
Installation
|
5
17
|
------------
|
18
|
+
In your Gemfile:
|
6
19
|
|
7
20
|
gem "coffee-views"
|
8
21
|
|
data/coffee-views.gemspec
CHANGED
data/lib/coffee_views.rb
CHANGED
@@ -8,18 +8,23 @@ module CoffeeViews
|
|
8
8
|
@@erb_handler ||= ActionView::Template.registered_template_handler(:erb)
|
9
9
|
end
|
10
10
|
|
11
|
-
def self.
|
12
|
-
source
|
13
|
-
source.gsub!(
|
14
|
-
source.gsub!(
|
15
|
-
source.gsub!(
|
16
|
-
source.gsub!
|
11
|
+
def self.preprocess source
|
12
|
+
source ||= ""
|
13
|
+
source.gsub! /<%==(.*?)%>/, '`<%==\1%>`'
|
14
|
+
source.gsub! /<%=([^=].*?)%>/, '`<%==(\1).to_json%>`'
|
15
|
+
source.gsub! /<%([^=].*?)%>/, '`<%\1%>`'
|
16
|
+
source.gsub! /#\{==(.*?)\}/, '#{`<%==\1%>`}'
|
17
|
+
source.gsub! /#\{=([^=].*?)\}/, '#{`<%==(\1).to_json%>`}'
|
17
18
|
source
|
18
19
|
end
|
20
|
+
|
21
|
+
def self.compile_coffee(source)
|
22
|
+
source = self.preprocess(source)
|
23
|
+
::CoffeeScript.compile(source)
|
24
|
+
end
|
19
25
|
|
20
26
|
def self.call(template)
|
21
|
-
source = self.
|
22
|
-
source = ::CoffeeScript.compile(source)
|
27
|
+
source = self.compile_coffee(template.source)
|
23
28
|
# TODO: find how to set source back to template without instance_variable_set
|
24
29
|
template.instance_variable_set :@source, source
|
25
30
|
|
@@ -29,4 +34,14 @@ module CoffeeViews
|
|
29
34
|
end
|
30
35
|
|
31
36
|
ActionView::Template.register_template_handler :coffee, CoffeeViews::Handlers::CoffeeScript
|
37
|
+
|
38
|
+
if defined?(Slim)
|
39
|
+
class CoffeeViewEngine < Slim::EmbeddedEngine::ERBEngine
|
40
|
+
def on_slim_embedded(engine, body)
|
41
|
+
source = CoffeeViews::Handlers::CoffeeScript.compile_coffee(collect_text(body))
|
42
|
+
source = Temple::ERB::Parser.new.call(source)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
Slim::EmbeddedEngine.register :coffeeview, Slim::EmbeddedEngine::TagEngine, :tag => :script, :attributes => { :type => 'text/javascript' }, :engine => CoffeeViewEngine
|
46
|
+
end
|
32
47
|
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe CoffeeViews::Handlers::CoffeeScript do
|
4
|
+
|
5
|
+
def preprocess source
|
6
|
+
subject.preprocess(source)
|
7
|
+
end
|
8
|
+
|
9
|
+
describe "Without substitutions" do
|
10
|
+
it "should work with empty source" do
|
11
|
+
preprocess('').should == ''
|
12
|
+
preprocess(nil).should == ''
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should preprocess sources" do
|
16
|
+
preprocess("x").should == "x"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "<%= code %>" do
|
21
|
+
it "should wrap with `` and call #to_json on code" do
|
22
|
+
preprocess("<%=x%>").should == "`<%==(x).to_json%>`"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe "<%%>" do
|
27
|
+
it "should wrap with ``" do
|
28
|
+
preprocess("<%x%>").should == "`<%x%>`"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe "<%==%>" do
|
33
|
+
it "should wrap with ``" do
|
34
|
+
preprocess("<%==x%>").should == "`<%==x%>`"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe '#{= code}' do
|
39
|
+
it "should wrap with `<%== %>` and call #to_json on code" do
|
40
|
+
preprocess('alert "#{=code}"').should == 'alert "#{`<%==(code).to_json%>`}"'
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
describe '#{== code}' do
|
45
|
+
it "should wrap with `<%== %>`" do
|
46
|
+
preprocess('alert "#{==code}"').should == 'alert "#{`<%==code%>`}"'
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: coffee-views
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-07-
|
12
|
+
date: 2011-07-10 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: coffee-script
|
16
|
-
requirement: &
|
16
|
+
requirement: &2156165280 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *2156165280
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: actionpack
|
27
|
-
requirement: &
|
27
|
+
requirement: &2156164660 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *2156164660
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: rspec
|
38
|
-
requirement: &
|
38
|
+
requirement: &2156160320 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ~>
|
@@ -43,7 +43,7 @@ dependencies:
|
|
43
43
|
version: '2.6'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *2156160320
|
47
47
|
description: Allows you to create .js.coffee views
|
48
48
|
email: yury.korolev@gmail.com
|
49
49
|
executables: []
|
@@ -52,11 +52,14 @@ extra_rdoc_files:
|
|
52
52
|
- README.md
|
53
53
|
files:
|
54
54
|
- .gitignore
|
55
|
+
- Gemfile
|
55
56
|
- LICENSE
|
56
57
|
- README.md
|
57
58
|
- coffee-views.gemspec
|
58
59
|
- lib/coffee-views.rb
|
59
60
|
- lib/coffee_views.rb
|
61
|
+
- spec/preprocess_spec.rb
|
62
|
+
- spec/spec_helper.rb
|
60
63
|
homepage: http://github.com/yury/coffee-views
|
61
64
|
licenses: []
|
62
65
|
post_install_message:
|