handlebars_assets 0.16 → 0.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: db80ec041bb1a30fa034a7191da617510877dd1a
4
- data.tar.gz: 591a444cbb3bdb7f71ee728c085b7912bf7769ce
3
+ metadata.gz: f8b59f300773aa0df84cd8569d3e7ae44bac3a0d
4
+ data.tar.gz: 85435577ad0029b162fd3bb7139131da0a930191
5
5
  SHA512:
6
- metadata.gz: b79943a6ffe7c085e29907aeeb0ee2187710d379453cae0bbbe3dfb25aef0b61470b6e170ebb9f413a9d03d34f1f6f00984b6bc4b53592173aa53cdb262611bb
7
- data.tar.gz: 0c91048ddb22913fccc4334d1f0a2c4d92b851b550710cb0d3660d2cd4366cda16f4a1038894b46ecf1196edfc3e709783b12c38e756cbf216706f1bd7f5f5df
6
+ metadata.gz: 95922af6d18e0966e7e17fc4c99bf70e2969a8c6d44f3a367455f842c3e704c9d09662fe1ca4ee809f900eb11d7386946a48b2b3a156cf18f4ce82141877e28e
7
+ data.tar.gz: e997c63c1bdca6278f653e1dbefd21bd759de896d07b1ecd7fd110cbacefc422d850f737da5b8fad6072576c1d6623dfc2b74a81c6b11ae0d2fbabc8e7702c3f
@@ -1,7 +1,14 @@
1
+ ## 0.17 (2014-06-22)
2
+
3
+ * Massive revamp - @AlexRiedler
4
+ * Changed how sprockets is registered - @AlexRiedler
5
+ * AMD loading - @AlexRiedler, based on @pboling changes (THANKS!)
6
+ * Fix extension being too liberal issues - @langalex
7
+
1
8
  ## 0.16 (2014-05-27)
2
9
 
3
10
  * README clarification - @jithugopal
4
- * Upgrade `handlebars` to 1.1.3 - @neodude
11
+ * Upgrade `handlebars` to 1.3 - @neodude
5
12
 
6
13
  ## 0.15 (2013-12-06)
7
14
 
data/README.md CHANGED
@@ -6,6 +6,12 @@ Yea, I think so too. That is why I wrote **handlebars_assets**. Give your Handle
6
6
 
7
7
  Using `sprockets` with Sinatra or another framework? **handlebars_assets** works outside of Rails too (as of v0.2.0)
8
8
 
9
+ # BREAKING CHANGES AS OF OF v0.17
10
+
11
+ @AlexRiedler has made some larger changes to this repository for going forward; If you have existing monkey patches they may not work, and the configuration schema has changed slightly to handle multiple extensions for the same compilation pipeline.
12
+
13
+ If you have any problems, please post an issue! I will attempt to fix ASAP
14
+
9
15
  # BREAKING CHANGE AS OF v0.9.0
10
16
 
11
17
  My pull request to allow `/` in partials was pulled into Handlebars. The hack that converted partial names to underscored paths (`shared/_time` -> `_shared_time`) is no longer necessary and has been removed. You should change all the partial references in your app when upgrading from a version prior to v0.9.0.
@@ -286,6 +292,7 @@ Les Hill, follow me on [Github](https://github.com/leshill) and [Twitter](https:
286
292
  * Parker Selbert (@sorentwo) : README section for `multiple_frameworks`
287
293
  * Francisco QV (@panchoqv) : README clarification
288
294
  * Dylan Markow (@dmarkow) : README cleanup
295
+ * Peter Boling (@pboling) : AMD Loading
289
296
 
290
297
  # Contributing
291
298
 
@@ -1,23 +1,43 @@
1
- require "handlebars_assets/version"
1
+ require 'handlebars_assets/version'
2
2
 
3
3
  module HandlebarsAssets
4
- PATH = File.expand_path("../../vendor/assets/javascripts", __FILE__)
4
+ autoload(:Config, 'handlebars_assets/config')
5
+ autoload(:Handlebars, 'handlebars_assets/handlebars')
6
+ autoload(:HandlebarsTemplate, 'handlebars_assets/handlebars_template')
7
+
8
+ PATH = File.expand_path('../../vendor/assets/javascripts', __FILE__)
5
9
 
6
10
  def self.path
7
11
  PATH
8
12
  end
9
13
 
10
- autoload(:Config, 'handlebars_assets/config')
11
- autoload(:Handlebars, 'handlebars_assets/handlebars')
12
- autoload(:TiltHandlebars, 'handlebars_assets/tilt_handlebars')
14
+ def self.configure
15
+ yield Config
16
+ end
13
17
 
14
- if defined?(Rails) && defined?(::Rails::Engine)
15
- require 'handlebars_assets/engine'
16
- else
17
- require 'sprockets'
18
- Sprockets.register_engine '.hbs', TiltHandlebars
19
- Sprockets.register_engine '.handlebars', TiltHandlebars
20
- Sprockets.register_engine('.hamlbars', TiltHandlebars) if HandlebarsAssets::Config.haml_available?
21
- Sprockets.register_engine('.slimbars', TiltHandlebars) if HandlebarsAssets::Config.slim_available?
18
+ def self.register_extensions(sprockets_environment)
19
+ Config.handlebars_extensions.each do |ext|
20
+ sprockets_environment.register_engine(ext, HandlebarsTemplate)
21
+ end
22
+ if Config.haml_enabled? && Config.haml_available?
23
+ Config.hamlbars_extensions.each do |ext|
24
+ sprockets_environment.register_engine(ext, HandlebarsTemplate)
25
+ end
26
+ end
27
+ if Config.slim_enabled? && Config.slim_available?
28
+ Config.slimbars_extensions.each do |ext|
29
+ sprockets_environment.register_engine(ext, HandlebarsTemplate)
30
+ end
31
+ end
22
32
  end
33
+
34
+ end
35
+
36
+ # Register the engine (which will register extension in the app)
37
+ # or ASSUME using sprockets
38
+ if defined?(Rails)
39
+ require 'handlebars_assets/engine'
40
+ else
41
+ require 'sprockets'
42
+ ::HandlebarsAssets.register_extensions(Sprockets)
23
43
  end
@@ -2,27 +2,31 @@ module HandlebarsAssets
2
2
  # Change config options in an initializer:
3
3
  #
4
4
  # HandlebarsAssets::Config.path_prefix = 'app/templates'
5
+
5
6
  module Config
6
7
  extend self
7
8
 
8
9
  attr_writer :compiler, :compiler_path, :ember, :multiple_frameworks,
9
10
  :haml_options, :known_helpers, :known_helpers_only, :options,
10
- :patch_files, :patch_path, :path_prefix, :slim_options, :template_namespace
11
-
12
- def configure
13
- yield self
14
- end
11
+ :patch_files, :patch_path, :path_prefix, :slim_options, :template_namespace,
12
+ :precompile, :haml_enabled, :slim_enabled,
13
+ :handlebars_extensions, :hamlbars_extensions, :slimbars_extensions,
14
+ :amd, :handlebars_amd_path, :amd_with_template_namespace
15
15
 
16
16
  def compiler
17
17
  @compiler || 'handlebars.js'
18
18
  end
19
19
 
20
+ def self.configure
21
+ yield self
22
+ end
23
+
20
24
  def compiler_path
21
25
  @compiler_path || HandlebarsAssets.path
22
26
  end
23
27
 
24
28
  def ember?
25
- @ember
29
+ @ember || false
26
30
  end
27
31
 
28
32
  def multiple_frameworks?
@@ -33,16 +37,35 @@ module HandlebarsAssets
33
37
  defined? ::Haml::Engine
34
38
  end
35
39
 
40
+ def haml_enabled?
41
+ @haml_enabled = true if @haml_enabled.nil?
42
+ @haml_enabled
43
+ end
44
+
36
45
  def haml_options
37
46
  @haml_options || {}
38
47
  end
39
48
 
49
+ def slim_available?
50
+ defined? ::Slim::Engine
51
+ end
52
+
53
+ def slim_enabled?
54
+ @slim_enabled = true if @slim_enabled.nil?
55
+ @slim_enabled
56
+ end
57
+
58
+ def slim_options
59
+ @slim_options || {}
60
+ end
61
+
40
62
  def known_helpers
41
63
  @known_helpers || []
42
64
  end
43
65
 
44
66
  def known_helpers_only
45
- @known_helpers_only || false
67
+ @known_helpers_only = false if @known_helpers_only.nil?
68
+ @known_helpers_only
46
69
  end
47
70
 
48
71
  def options
@@ -58,21 +81,50 @@ module HandlebarsAssets
58
81
  end
59
82
 
60
83
  def path_prefix
61
- @path_prefix || 'templates'
84
+ @path_prefix ||= 'templates'
62
85
  end
63
86
 
64
- def slim_available?
65
- defined? ::Slim::Engine
66
- end
67
-
68
- def slim_options
69
- @slim_options || {}
87
+ def precompile
88
+ @precompile = true if @precompile.nil?
89
+ @precompile
70
90
  end
71
91
 
72
92
  def template_namespace
73
93
  @template_namespace || 'HandlebarsTemplates'
74
94
  end
75
95
 
96
+ def handlebars_extensions
97
+ @hbs_extensions ||= ['hbs', 'handlebars']
98
+ end
99
+
100
+ def hamlbars_extensions
101
+ @hamlbars_extensions ||= ['hamlbars']
102
+ end
103
+
104
+ def slimbars_extensions
105
+ @slimbars_extensions ||= ['slimbars']
106
+ end
107
+
108
+ def ember_extensions
109
+ @ember_extensions ||= ['ember']
110
+ end
111
+
112
+ def amd?
113
+ @amd || false
114
+ end
115
+
116
+ # indicate whether the template should
117
+ # be added to the global template namespace
118
+ def amd_with_template_namespace
119
+ @amd_with_template_namespace || false
120
+ end
121
+
122
+ # path specified by the require.js paths
123
+ # during configuration for the handlebars
124
+ def handlebars_amd_path
125
+ @handlebars_amd_path || 'handlebars'
126
+ end
127
+
76
128
  private
77
129
 
78
130
  def generate_known_helpers_hash
@@ -1,11 +1,8 @@
1
1
  module HandlebarsAssets
2
+ # NOTE: must be an engine because we are including assets in the gem
2
3
  class Engine < ::Rails::Engine
3
- initializer "sprockets.handlebars", :after => "sprockets.environment", :group => :all do |app|
4
- next unless app.assets
5
- app.assets.register_engine('.hbs', TiltHandlebars)
6
- app.assets.register_engine('.handlebars', TiltHandlebars)
7
- app.assets.register_engine('.hamlbars', TiltHandlebars) if HandlebarsAssets::Config.haml_available?
8
- app.assets.register_engine('.slimbars', TiltHandlebars) if HandlebarsAssets::Config.slim_available?
4
+ initializer "handlebars_assets.assets.register" do |app|
5
+ ::HandlebarsAssets::register_extensions(app.assets)
9
6
  end
10
7
  end
11
8
  end
@@ -0,0 +1,205 @@
1
+ require 'tilt'
2
+ require 'json'
3
+
4
+ module HandlebarsAssets
5
+ module Unindent
6
+ # http://bit.ly/aze9FV
7
+ # Strip leading whitespace from each line that is the same as the
8
+ # amount of whitespace on the first line of the string.
9
+ # Leaves _additional_ indentation on later lines intact.
10
+ def unindent(heredoc)
11
+ heredoc.gsub(/^#{heredoc[/\A\s*/]}/, '')
12
+ end
13
+ end
14
+
15
+ class HandlebarsTemplate < Tilt::Template
16
+
17
+ include Unindent
18
+
19
+ def self.default_mime_type
20
+ 'application/javascript'
21
+ end
22
+
23
+ def initialize_engine
24
+ begin
25
+ require 'haml'
26
+ rescue LoadError
27
+ # haml not available
28
+ end
29
+ begin
30
+ require 'slim'
31
+ rescue LoadError
32
+ # slim not available
33
+ end
34
+ end
35
+
36
+ def prepare
37
+ @template_path = TemplatePath.new(@file)
38
+ @engine =
39
+ if @template_path.is_haml?
40
+ Haml::Engine.new(data, HandlebarsAssets::Config.haml_options)
41
+ elsif @template_path.is_slim?
42
+ Slim::Template.new(HandlebarsAssets::Config.slim_options) { data }
43
+ else
44
+ nil
45
+ end
46
+ end
47
+
48
+ def evaluate(scope, locals, &block)
49
+ source =
50
+ if @engine
51
+ @engine.render(scope, locals, &block)
52
+ else
53
+ data
54
+ end
55
+
56
+ # remove trailing \n on file, for some reason the directives pipeline adds this
57
+ source.chomp!($/)
58
+
59
+ # handle the case of multiple frameworks combined with ember
60
+ # DEFER: use extension setup for ember
61
+ if (HandlebarsAssets::Config.multiple_frameworks? && @template_path.is_ember?) ||
62
+ (HandlebarsAssets::Config.ember? && !HandlebarsAssets::Config.multiple_frameworks?)
63
+ compile_ember(source)
64
+ else
65
+ compile_default(source)
66
+ end
67
+ end
68
+
69
+ def compile_ember(source)
70
+ "window.Ember.TEMPLATES[#{@template_path.name}] = Ember.Handlebars.compile(#{JSON.dump(source)});"
71
+ end
72
+
73
+ def compile_default(source)
74
+ template =
75
+ if HandlebarsAssets::Config.precompile
76
+ compiled_hbs = Handlebars.precompile(source, HandlebarsAssets::Config.options)
77
+ "Handlebars.template(#{compiled_hbs})"
78
+ else
79
+ "Handlebars.compile(#{JSON.dump(source)})"
80
+ end
81
+
82
+ template_namespace = HandlebarsAssets::Config.template_namespace
83
+
84
+ if HandlebarsAssets::Config.amd?
85
+ handlebars_amd_path = HandlebarsAssets::Config.handlebars_amd_path
86
+ if HandlebarsAssets::Config.amd_with_template_namespace
87
+ if @template_path.is_partial?
88
+ unindent <<-PARTIAL
89
+ define(['#{handlebars_amd_path}'],function(Handlebars){
90
+ var t = #{template};
91
+ Handlebars.registerPartial(#{@template_path.name}, t);
92
+ return t;
93
+ ;})
94
+ PARTIAL
95
+ else
96
+ unindent <<-TEMPLATE
97
+ define(['#{handlebars_amd_path}'],function(Handlebars){
98
+ return #{template};
99
+ });
100
+ TEMPLATE
101
+ end
102
+ else
103
+ if @template_path.is_partial?
104
+ unindent <<-PARTIAL
105
+ define(['#{handlebars_amd_path}'],function(Handlebars){
106
+ var t = #{template};
107
+ Handlebars.registerPartial(#{@template_path.name}, t);
108
+ return t;
109
+ ;})
110
+ PARTIAL
111
+ else
112
+ unindent <<-TEMPLATE
113
+ define(['#{handlebars_amd_path}'],function(Handlebars){
114
+ this.#{template_namespace} || (this.#{template_namespace} = {});
115
+ this.#{template_namespace}[#{@template_path.name}] = #{template};
116
+ return this.#{template_namespace}[#{@template_path.name}];
117
+ });
118
+ TEMPLATE
119
+ end
120
+ end
121
+ else
122
+ if @template_path.is_partial?
123
+ unindent <<-PARTIAL
124
+ (function() {
125
+ Handlebars.registerPartial(#{@template_path.name}, #{template});
126
+ }).call(this);
127
+ PARTIAL
128
+ else
129
+ unindent <<-TEMPLATE
130
+ (function() {
131
+ this.#{template_namespace} || (this.#{template_namespace} = {});
132
+ this.#{template_namespace}[#{@template_path.name}] = #{template};
133
+ return this.#{template_namespace}[#{@template_path.name}];
134
+ }).call(this);
135
+ TEMPLATE
136
+ end
137
+ end
138
+ end
139
+
140
+ protected
141
+
142
+ class TemplatePath
143
+ def initialize(path)
144
+ @full_path = path
145
+ end
146
+
147
+ def is_haml?
148
+ result = false
149
+ ::HandlebarsAssets::Config.hamlbars_extensions.each do |ext|
150
+ if ext.start_with? '.'
151
+ ext = '\\#{ext}'
152
+ result ||= !(@full_path =~ /#{ext}(\..*)*$/).nil?
153
+ else
154
+ result ||= !(@full_path =~ /\.#{ext}(\..*)*$/).nil?
155
+ end
156
+ end
157
+ result
158
+ end
159
+
160
+ def is_slim?
161
+ result = false
162
+ ::HandlebarsAssets::Config.slimbars_extensions.each do |ext|
163
+ if ext.start_with? '.'
164
+ ext = '\\#{ext}'
165
+ result ||= !(@full_path =~ /#{ext}(\..*)*$/).nil?
166
+ else
167
+ result ||= !(@full_path =~ /\.#{ext}(\..*)*$/).nil?
168
+ end
169
+ end
170
+ result
171
+ end
172
+
173
+ def is_partial?
174
+ @full_path.gsub(%r{.*/}, '').start_with?('_')
175
+ end
176
+
177
+ def is_ember?
178
+ result = false
179
+ ::HandlebarsAssets::Config.ember_extensions.each do |ext|
180
+ if ext.start_with? '.'
181
+ ext = '\\#{ext}'
182
+ result ||= !(@full_path =~ /#{ext}(\..*)*$/).nil?
183
+ else
184
+ result ||= !(@full_path =~ /\.#{ext}(\..*)*$/).nil?
185
+ end
186
+ end
187
+ result
188
+ end
189
+
190
+ def name
191
+ template_name
192
+ end
193
+
194
+ private
195
+
196
+ def relative_path
197
+ @full_path.match(/.*#{HandlebarsAssets::Config.path_prefix}\/((.*\/)*([^.]*)).*$/)[1]
198
+ end
199
+
200
+ def template_name
201
+ relative_path.dump
202
+ end
203
+ end
204
+ end
205
+ end
@@ -1,3 +1,3 @@
1
1
  module HandlebarsAssets
2
- VERSION = "0.16"
2
+ VERSION = "0.17"
3
3
  end
@@ -6,7 +6,7 @@ module HandlebarsAssets
6
6
  include CompilerSupport
7
7
 
8
8
  def compile_haml(source)
9
- Haml::Engine.new(source, HandlebarsAssets::Config.haml_options).render
9
+ (Haml::Engine.new(source, HandlebarsAssets::Config.haml_options).render).chomp
10
10
  end
11
11
 
12
12
  def teardown
@@ -20,7 +20,7 @@ module HandlebarsAssets
20
20
  scope = make_scope root, file
21
21
  source = "%p This is {{handlebars}}"
22
22
 
23
- template = HandlebarsAssets::TiltHandlebars.new(scope.pathname.to_s) { source }
23
+ template = HandlebarsAssets::HandlebarsTemplate.new(scope.pathname.to_s) { source }
24
24
 
25
25
  assert_equal hbs_compiled('test_render', compile_haml(source)), template.render(scope, {})
26
26
  end
@@ -20,7 +20,7 @@ module HandlebarsAssets
20
20
  scope = make_scope root, file
21
21
  source = "p This is {{handlebars}}"
22
22
 
23
- template = HandlebarsAssets::TiltHandlebars.new(scope.pathname.to_s) { source }
23
+ template = HandlebarsAssets::HandlebarsTemplate.new(scope.pathname.to_s) { source }
24
24
 
25
25
  assert_equal hbs_compiled('test_render', compile_slim(source)), template.render(scope, {})
26
26
  end
@@ -1,7 +1,7 @@
1
1
  require 'test_helper'
2
2
 
3
3
  module HandlebarsAssets
4
- class TiltHandlebarsTest < Test::Unit::TestCase
4
+ class HandlebarsTemplateTest < Test::Unit::TestCase
5
5
  include CompilerSupport
6
6
  include SprocketsScope
7
7
 
@@ -24,7 +24,7 @@ module HandlebarsAssets
24
24
  scope = make_scope root, file
25
25
  source = "This is {{handlebars}}"
26
26
 
27
- template = HandlebarsAssets::TiltHandlebars.new(scope.pathname.to_s) { source }
27
+ template = HandlebarsAssets::HandlebarsTemplate.new(scope.pathname.to_s) { source }
28
28
 
29
29
  assert_equal hbs_compiled('test_render', source), template.render(scope, {})
30
30
  end
@@ -37,7 +37,7 @@ module HandlebarsAssets
37
37
  scope = make_scope root, file
38
38
  source = "This is {{handlebars}}"
39
39
 
40
- template = HandlebarsAssets::TiltHandlebars.new(scope.pathname.to_s) { source }
40
+ template = HandlebarsAssets::HandlebarsTemplate.new(scope.pathname.to_s) { source }
41
41
 
42
42
  assert_equal hbs_compiled('test_template_misnaming', source), template.render(scope, {})
43
43
  end
@@ -50,7 +50,7 @@ module HandlebarsAssets
50
50
 
51
51
  HandlebarsAssets::Config.path_prefix = 'app/templates'
52
52
 
53
- template = HandlebarsAssets::TiltHandlebars.new(scope.pathname.to_s) { source }
53
+ template = HandlebarsAssets::HandlebarsTemplate.new(scope.pathname.to_s) { source }
54
54
 
55
55
  assert_equal hbs_compiled('test_path_prefix', source), template.render(scope, {})
56
56
  end
@@ -65,11 +65,11 @@ module HandlebarsAssets
65
65
 
66
66
  HandlebarsAssets::Config.path_prefix = 'app/templates'
67
67
 
68
- template1 = HandlebarsAssets::TiltHandlebars.new(scope1.pathname.to_s) { source }
68
+ template1 = HandlebarsAssets::HandlebarsTemplate.new(scope1.pathname.to_s) { source }
69
69
 
70
70
  assert_equal hbs_compiled_partial('_test_underscore', source), template1.render(scope1, {})
71
71
 
72
- template2 = HandlebarsAssets::TiltHandlebars.new(scope2.pathname.to_s) { source }
72
+ template2 = HandlebarsAssets::HandlebarsTemplate.new(scope2.pathname.to_s) { source }
73
73
 
74
74
  assert_equal hbs_compiled_partial('some/thing/_test_underscore', source), template2.render(scope2, {})
75
75
  end
@@ -80,7 +80,7 @@ module HandlebarsAssets
80
80
  scope = make_scope root, file
81
81
  source = "{{#with author}}By {{first_name}} {{last_name}}{{/with}}"
82
82
 
83
- template = HandlebarsAssets::TiltHandlebars.new(scope.pathname.to_s) { source }
83
+ template = HandlebarsAssets::HandlebarsTemplate.new(scope.pathname.to_s) { source }
84
84
 
85
85
  assert_equal hbs_compiled('test_without_known', source), template.render(scope, {})
86
86
  end
@@ -93,7 +93,7 @@ module HandlebarsAssets
93
93
 
94
94
  HandlebarsAssets::Config.known_helpers_only = true
95
95
 
96
- template = HandlebarsAssets::TiltHandlebars.new(scope.pathname.to_s) { source }
96
+ template = HandlebarsAssets::HandlebarsTemplate.new(scope.pathname.to_s) { source }
97
97
 
98
98
  assert_equal hbs_compiled('test_known', source), template.render(scope, {})
99
99
  end
@@ -104,7 +104,7 @@ module HandlebarsAssets
104
104
  scope = make_scope root, file
105
105
  source = "{{#custom author}}By {{first_name}} {{last_name}}{{/custom}}"
106
106
 
107
- template = HandlebarsAssets::TiltHandlebars.new(scope.pathname.to_s) { source }
107
+ template = HandlebarsAssets::HandlebarsTemplate.new(scope.pathname.to_s) { source }
108
108
 
109
109
  assert_equal hbs_compiled('test_custom_helper', source), template.render(scope, {})
110
110
  end
@@ -118,20 +118,20 @@ module HandlebarsAssets
118
118
  HandlebarsAssets::Config.known_helpers_only = true
119
119
  HandlebarsAssets::Config.known_helpers = %w(custom)
120
120
 
121
- template = HandlebarsAssets::TiltHandlebars.new(scope.pathname.to_s) { source }
121
+ template = HandlebarsAssets::HandlebarsTemplate.new(scope.pathname.to_s) { source }
122
122
 
123
123
  assert_equal hbs_compiled('test_custom_known_helper', source), template.render(scope, {})
124
124
  end
125
125
 
126
126
  def test_template_namespace
127
- root = '/myapp/app/assets/javascripts'
127
+ root = '/myapp/app/assets/javascripts/templates'
128
128
  file = 'test_template_namespace.hbs'
129
129
  scope = make_scope root, file
130
130
  source = "This is {{handlebars}}"
131
131
 
132
132
  HandlebarsAssets::Config.template_namespace = 'JST'
133
133
 
134
- template = HandlebarsAssets::TiltHandlebars.new(scope.pathname.to_s) { source }
134
+ template = HandlebarsAssets::HandlebarsTemplate.new(scope.pathname.to_s) { source }
135
135
 
136
136
  assert_equal hbs_compiled('test_template_namespace', source), template.render(scope, {})
137
137
  end
@@ -143,7 +143,7 @@ module HandlebarsAssets
143
143
  source = "This is {{handlebars}}"
144
144
 
145
145
  HandlebarsAssets::Config.ember = true
146
- template = HandlebarsAssets::TiltHandlebars.new(scope.pathname.to_s) { source }
146
+ template = HandlebarsAssets::HandlebarsTemplate.new(scope.pathname.to_s) { source }
147
147
 
148
148
  expected_compiled = %{window.Ember.TEMPLATES["test_render"] = Ember.Handlebars.compile("This is {{handlebars}}");};
149
149
  assert_equal expected_compiled, template.render(scope, {})
@@ -152,10 +152,12 @@ module HandlebarsAssets
152
152
  def test_multiple_frameworks_with_ember_render
153
153
  root = '/myapp/app/assets/templates'
154
154
  non_ember = 'test_render.hbs'
155
+ non_ember_but_with_ember = 'test_member.hbs'
155
156
  ember_ext_no_hbs = 'test_render.ember'
156
157
  ember_ext = 'test_render.ember.hbs'
157
158
  ember_with_haml = 'test_render.ember.hamlbars'
158
159
  ember_with_slim = 'test_render.ember.slimbars'
160
+ ember_ext_with_erb = 'test_render.ember.hbs.erb'
159
161
 
160
162
  HandlebarsAssets::Config.ember = true
161
163
  HandlebarsAssets::Config.multiple_frameworks = true
@@ -163,33 +165,45 @@ module HandlebarsAssets
163
165
  # File without ember extension should compile to default namespace
164
166
  scope = make_scope root, non_ember
165
167
  source = "This is {{handlebars}}"
166
- template = HandlebarsAssets::TiltHandlebars.new(scope.pathname.to_s) { source }
168
+ template = HandlebarsAssets::HandlebarsTemplate.new(scope.pathname.to_s) { source }
167
169
  assert_equal hbs_compiled('test_render', source), template.render(scope, {})
168
170
 
171
+ # File without ember extension but with ember in it should compile to default namespace
172
+ scope = make_scope root, non_ember_but_with_ember
173
+ source = "This is {{handlebars}}"
174
+ template = HandlebarsAssets::HandlebarsTemplate.new(scope.pathname.to_s) { source }
175
+ assert_equal hbs_compiled('test_member', source), template.render(scope, {})
176
+
169
177
  # File with ember extension should compile to ember specific namespace
170
178
  expected_compiled = %{window.Ember.TEMPLATES["test_render"] = Ember.Handlebars.compile("This is {{handlebars}}");};
171
179
  scope = make_scope root, ember_ext_no_hbs
172
- template = HandlebarsAssets::TiltHandlebars.new(scope.pathname.to_s) { source }
180
+ template = HandlebarsAssets::HandlebarsTemplate.new(scope.pathname.to_s) { source }
181
+ assert_equal expected_compiled, template.render(scope, {})
182
+
183
+ # File with ember and erb extension should compile to ember specific namespace
184
+ expected_compiled = %{window.Ember.TEMPLATES["test_render"] = Ember.Handlebars.compile("This is {{handlebars}}");};
185
+ scope = make_scope root, ember_ext_with_erb
186
+ template = HandlebarsAssets::HandlebarsTemplate.new(scope.pathname.to_s) { source }
173
187
  assert_equal expected_compiled, template.render(scope, {})
174
188
 
175
189
  # File with ember.hbs extension should compile to ember specific namespace
176
190
  expected_compiled = %{window.Ember.TEMPLATES["test_render"] = Ember.Handlebars.compile("This is {{handlebars}}");};
177
191
  scope = make_scope root, ember_ext
178
- template = HandlebarsAssets::TiltHandlebars.new(scope.pathname.to_s) { source }
192
+ template = HandlebarsAssets::HandlebarsTemplate.new(scope.pathname.to_s) { source }
179
193
  assert_equal expected_compiled, template.render(scope, {})
180
194
 
181
195
  # File with ember.hamlbars extension should compile to ember specific namespace
182
- expected_compiled = %{window.Ember.TEMPLATES["test_render"] = Ember.Handlebars.compile("<p>This is {{handlebars}}</p>\\n");};
196
+ expected_compiled = %{window.Ember.TEMPLATES["test_render"] = Ember.Handlebars.compile("<p>This is {{handlebars}}</p>");};
183
197
  scope = make_scope root, ember_with_haml
184
198
  source = "%p This is {{handlebars}}"
185
- template = HandlebarsAssets::TiltHandlebars.new(scope.pathname.to_s) { compile_haml(source) }
199
+ template = HandlebarsAssets::HandlebarsTemplate.new(scope.pathname.to_s) { compile_haml(source) }
186
200
  assert_equal expected_compiled, template.render(scope, {})
187
201
 
188
202
  # File with ember.slimbars extension should compile to ember specific namespace
189
203
  expected_compiled = %{window.Ember.TEMPLATES["test_render"] = Ember.Handlebars.compile("<p>This is {{handlebars}}</p>");};
190
204
  source = "p This is {{handlebars}}"
191
205
  scope = make_scope root, ember_with_slim
192
- template = HandlebarsAssets::TiltHandlebars.new(scope.pathname.to_s) { compile_slim(source) }
206
+ template = HandlebarsAssets::HandlebarsTemplate.new(scope.pathname.to_s) { compile_slim(source) }
193
207
  assert_equal expected_compiled, template.render(scope, {})
194
208
  end
195
209
  end
@@ -1,6 +1,6 @@
1
1
  require 'handlebars_assets'
2
2
  require 'handlebars_assets/config'
3
- require 'handlebars_assets/tilt_handlebars'
3
+ require 'handlebars_assets/handlebars_template'
4
4
  require 'handlebars_assets/handlebars'
5
5
 
6
6
  require 'test/unit'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: handlebars_assets
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.16'
4
+ version: '0.17'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Les Hill
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-27 00:00:00.000000000 Z
11
+ date: 2014-06-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: execjs
@@ -155,7 +155,7 @@ files:
155
155
  - lib/handlebars_assets/config.rb
156
156
  - lib/handlebars_assets/engine.rb
157
157
  - lib/handlebars_assets/handlebars.rb
158
- - lib/handlebars_assets/tilt_handlebars.rb
158
+ - lib/handlebars_assets/handlebars_template.rb
159
159
  - lib/handlebars_assets/version.rb
160
160
  - test/edge/handlebars.js
161
161
  - test/handlebars_assets/compiling_test.rb
@@ -1,129 +0,0 @@
1
- require 'tilt'
2
- require 'multi_json'
3
-
4
- module HandlebarsAssets
5
- module Unindent
6
- # http://bit.ly/aze9FV
7
- # Strip leading whitespace from each line that is the same as the
8
- # amount of whitespace on the first line of the string.
9
- # Leaves _additional_ indentation on later lines intact.
10
- def unindent(heredoc)
11
- heredoc.gsub /^#{heredoc[/\A\s*/]}/, ''
12
- end
13
- end
14
-
15
- class TiltHandlebars < Tilt::Template
16
-
17
- include Unindent
18
-
19
- def self.default_mime_type
20
- 'application/javascript'
21
- end
22
-
23
- def evaluate(scope, locals, &block)
24
- template_path = TemplatePath.new(scope)
25
-
26
- source = if template_path.is_haml?
27
- Haml::Engine.new(data, HandlebarsAssets::Config.haml_options).render(scope, locals)
28
- elsif template_path.is_slim?
29
- Slim::Template.new(HandlebarsAssets::Config.slim_options) { data }.render(scope, locals)
30
- else
31
- data
32
- end
33
-
34
- if HandlebarsAssets::Config.multiple_frameworks? && HandlebarsAssets::Config.ember?
35
- if template_path.is_ember?
36
- compile_ember(source, template_path)
37
- else
38
- compile_default(source, template_path)
39
- end
40
- elsif HandlebarsAssets::Config.ember? && !HandlebarsAssets::Config.multiple_frameworks?
41
- compile_ember(source, template_path)
42
- else
43
- compile_default(source, template_path)
44
- end
45
- end
46
-
47
- def initialize_engine
48
- begin
49
- require 'haml'
50
- rescue LoadError
51
- # haml not available
52
- end
53
- begin
54
- require 'slim'
55
- rescue LoadError
56
- # slim not available
57
- end
58
- end
59
-
60
- def compile_ember(source, template_path)
61
- "window.Ember.TEMPLATES[#{template_path.name}] = Ember.Handlebars.compile(#{::MultiJson.dump source});"
62
- end
63
-
64
- def compile_default(source, template_path)
65
- compiled_hbs = Handlebars.precompile(source, HandlebarsAssets::Config.options)
66
-
67
- template_namespace = HandlebarsAssets::Config.template_namespace
68
-
69
- if template_path.is_partial?
70
- unindent <<-PARTIAL
71
- (function() {
72
- Handlebars.registerPartial(#{template_path.name}, Handlebars.template(#{compiled_hbs}));
73
- }).call(this);
74
- PARTIAL
75
- else
76
- unindent <<-TEMPLATE
77
- (function() {
78
- this.#{template_namespace} || (this.#{template_namespace} = {});
79
- this.#{template_namespace}[#{template_path.name}] = Handlebars.template(#{compiled_hbs});
80
- return this.#{template_namespace}[#{template_path.name}];
81
- }).call(this);
82
- TEMPLATE
83
- end
84
- end
85
-
86
- protected
87
-
88
- def prepare; end
89
-
90
- class TemplatePath
91
- def initialize(scope)
92
- self.full_path = scope.pathname
93
- self.template_path = scope.logical_path
94
- end
95
-
96
- def is_haml?
97
- full_path.to_s.end_with?('.hamlbars')
98
- end
99
-
100
- def is_slim?
101
- full_path.to_s.end_with?('.slimbars')
102
- end
103
-
104
- def is_partial?
105
- template_path.gsub(%r{.*/}, '').start_with?('_')
106
- end
107
-
108
- def is_ember?
109
- full_path.to_s =~ %r{.ember(.hbs|.hamlbars|.slimbars)?$}
110
- end
111
-
112
- def name
113
- template_name
114
- end
115
-
116
- private
117
-
118
- attr_accessor :full_path, :template_path
119
-
120
- def relative_path
121
- template_path.gsub(/^#{HandlebarsAssets::Config.path_prefix}\/(.*)$/i, "\\1")
122
- end
123
-
124
- def template_name
125
- relative_path.dump
126
- end
127
- end
128
- end
129
- end