padrino-helpers 0.14.0.2 → 0.14.1

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: 3c4121918eb605bf34851cc7eb66d3e04cde3075
4
- data.tar.gz: e702062920b5999b527ed97848e84e8eabd9576a
3
+ metadata.gz: 0aedec3da2a6164e6ea901cd5c17d5d96a45ac18
4
+ data.tar.gz: 23448eb96d10c43b54da803996c142b92aa0dffa
5
5
  SHA512:
6
- metadata.gz: e07cca38d7a864db57fd2a80e66c68084122fac37455456beea4e2c7683745d5c70915d0d27d591469a0602e44cf049038d222d9490e23df4e630addf1188b5b
7
- data.tar.gz: 57d06a0d13bda7e6356e02f81ddaf901e08582bf71d99e38d54e3bd3a82ddf165dcbd6c86c4fff479bf0fe3cbfaf4addd07d9353bd8be224fbd69e6244e6cb5c
6
+ metadata.gz: 037c40c71d6f0ef00f9f58ee6c9ffac3dd68b656013ddd7c3ec4dfe01d99f9cfce19653530f924a32d77535223e1cce07e4b4f8a185b920c07e981d2205e832e
7
+ data.tar.gz: 8ad075ff1b96642c437ba95655299154c621bb5a441c96ef84f6098b2b0614464fd3e637b01fa66ccd73e0e0904446e9fa80723ba702678d394b4461e9748512
@@ -1,25 +1,41 @@
1
1
  module Padrino
2
2
  module Helpers
3
3
  module OutputHelpers
4
- ##
5
- # Handler for Haml templates.
6
- #
7
- class HamlHandler < AbstractHandler
4
+ # Haml and Hamlit require different detection code
5
+ if Tilt.template_for('.haml').to_s == "Padrino::Rendering::HamlTemplate"
8
6
  ##
9
- # Returns true if the block is for Haml
7
+ # Handler for Haml templates.
10
8
  #
11
- def engine_matches?(block)
12
- template.block_is_haml?(block)
13
- end
9
+ class HamlHandler < AbstractHandler
10
+ ##
11
+ # Returns true if the block is for Haml
12
+ #
13
+ def engine_matches?(block)
14
+ template.block_is_haml?(block)
15
+ end
14
16
 
17
+ ##
18
+ # Captures the html from a block of template code for this handler.
19
+ #
20
+ def capture_from_template(*args, &block)
21
+ engine_matches?(block) ? template.capture_haml(*args, &block) : yield(*args)
22
+ end
23
+ end
24
+ OutputHelpers.register(:haml, HamlHandler)
25
+ else
15
26
  ##
16
- # Captures the html from a block of template code for this handler.
27
+ # Handler for Haml templates.
17
28
  #
18
- def capture_from_template(*args, &block)
19
- engine_matches?(block) ? template.capture_haml(*args, &block) : yield(*args)
29
+ class HamlitHandler < AbstractHandler
30
+ ##
31
+ # Returns true if the block is for Hamlit.
32
+ #
33
+ def engine_matches?(block)
34
+ block.binding.eval('defined? __in_hamlit_template')
35
+ end
20
36
  end
37
+ OutputHelpers.register(:haml, HamlitHandler)
21
38
  end
22
- OutputHelpers.register(:haml, HamlHandler)
23
39
  end
24
40
  end
25
41
  end
@@ -65,6 +65,7 @@ module Padrino
65
65
  fail "gem 'tilt' is required" unless defined?(::Tilt)
66
66
 
67
67
  def render(engine, file=nil, options={}, locals=nil, &block)
68
+ options.delete(:layout)
68
69
  engine, file = file, engine if file.nil?
69
70
  template_engine = engine ? ::Tilt[engine] : ::Tilt.default_mapping[file]
70
71
  fail "Engine #{engine.inspect} is not registered with Tilt" unless template_engine
@@ -179,6 +179,50 @@ module Padrino
179
179
 
180
180
  private
181
181
 
182
+ def render_like_sinatra(engine, data, options={}, locals={}, &block)
183
+ # merge app-level options
184
+ engine_options = settings.respond_to?(engine) ? settings.send(engine) : {}
185
+ options = engine_options.merge(options)
186
+
187
+ # extract generic options
188
+ locals = options.delete(:locals) || locals || {}
189
+ views = options.delete(:views) || settings.views || "./views"
190
+ layout = options[:layout]
191
+ layout = false if layout.nil? && options.include?(:layout)
192
+ eat_errors = layout.nil?
193
+ layout = engine_options[:layout] if layout.nil? or (layout == true && engine_options[:layout] != false)
194
+ layout = @default_layout if layout.nil? or layout == true
195
+ layout_options = options.delete(:layout_options) || {}
196
+ content_type = options.delete(:default_content_type)
197
+ content_type = options.delete(:content_type) || content_type
198
+ layout_engine = options.delete(:layout_engine) || engine
199
+ scope = options.delete(:scope) || self
200
+ options.delete(:layout)
201
+
202
+ # set some defaults
203
+ options[:default_encoding] ||= settings.default_encoding
204
+
205
+ # compile and render template
206
+ begin
207
+ layout_was = @default_layout
208
+ @default_layout = false
209
+ template = compile_template(engine, data, options, views)
210
+ output = template.render(scope, locals, &block)
211
+ ensure
212
+ @default_layout = layout_was
213
+ end
214
+
215
+ # render layout
216
+ if layout
217
+ layout_engine_options = settings.respond_to?(layout_engine) ? settings.send(layout_engine).dup : {}
218
+ options = layout_engine_options.update(:views => views, :layout => false, :eat_errors => eat_errors, :scope => scope).update(layout_options)
219
+ catch(:layout_missing) { return render_like_sinatra(layout_engine, layout, options, locals) { output } }
220
+ end
221
+
222
+ output.extend(Sinatra::Base::ContentTyped).content_type = content_type if content_type
223
+ output
224
+ end
225
+
182
226
  ##
183
227
  # Enhancing Sinatra render functionality for:
184
228
  #
@@ -210,7 +254,7 @@ module Padrino
210
254
  @_out_buf, buf_was = SafeBuffer.new, @_out_buf
211
255
 
212
256
  # Pass arguments to Sinatra render method.
213
- super(engine, data, with_layout(options), locals, &block)
257
+ render_like_sinatra(engine, data, with_layout(options), locals, &block)
214
258
  ensure
215
259
  @current_engine = engine_was
216
260
  @_out_buf = buf_was
@@ -365,6 +409,15 @@ unless defined? Padrino::Rendering::HamlTemplate
365
409
  end
366
410
  end
367
411
 
412
+ unless defined? Padrino::Rendering::HamlitTemplate
413
+ begin
414
+ require 'hamlit'
415
+ rescue LoadError
416
+ else
417
+ require 'padrino/rendering/hamlit_template'
418
+ end
419
+ end
420
+
368
421
  unless defined? Padrino::Rendering::ErubisTemplate
369
422
  begin
370
423
  require 'erubis'
@@ -48,5 +48,6 @@ end
48
48
  Tilt.prefer(Padrino::Rendering::ERBTemplate, :erb)
49
49
 
50
50
  Padrino::Rendering.engine_configurations[:erb] = {
51
- :safe_buffer => true
51
+ :safe_buffer => true,
52
+ :outvar => '@_out_buf',
52
53
  }
@@ -63,4 +63,5 @@ Tilt.prefer(Padrino::Rendering::ErubisTemplate, :erb)
63
63
 
64
64
  Padrino::Rendering.engine_configurations[:erb] = {
65
65
  :engine_class => Padrino::Rendering::SafeEruby,
66
+ :outvar => '@_out_buf',
66
67
  }
@@ -0,0 +1,35 @@
1
+ module Padrino
2
+ module Rendering
3
+ class HamlitOutputBuffer < Temple::Generators::StringBuffer
4
+ define_options :buffer_class => 'SafeBuffer'
5
+
6
+ def call(exp)
7
+ [preamble, compile(exp), postamble].flatten.compact.join('; '.freeze)
8
+ end
9
+
10
+ def create_buffer
11
+ "#{buffer} = #{options[:buffer_class]}.new"
12
+ end
13
+
14
+ def concat(str)
15
+ "#{buffer}.safe_concat((#{str}))"
16
+ end
17
+ end
18
+
19
+ class HamlitTemplate < Hamlit::Template
20
+ include SafeTemplate
21
+
22
+ def precompiled_preamble(locals)
23
+ "__in_hamlit_template = true\n" << super
24
+ end
25
+ end
26
+ end
27
+ end
28
+
29
+ Tilt.prefer(Padrino::Rendering::HamlitTemplate, :haml)
30
+
31
+ Padrino::Rendering.engine_configurations[:haml] = {
32
+ :generator => Padrino::Rendering::HamlitOutputBuffer,
33
+ :buffer => "@_out_buf",
34
+ :use_html_safe => true,
35
+ }
@@ -3,6 +3,8 @@ require File.expand_path(File.dirname(__FILE__) + '/helper')
3
3
  describe "Rendering Extensions" do
4
4
  describe 'for haml' do
5
5
  it 'should render haml_tag correctly' do
6
+ skip unless Padrino::Helpers::OutputHelpers.handlers[:haml].to_s == "Padrino::Helpers::OutputHelpers::HamlHandler"
7
+
6
8
  mock_app do
7
9
  get('/') { render :haml, '-haml_tag :div'}
8
10
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: padrino-helpers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.14.0.2
4
+ version: 0.14.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Padrino Team
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2017-05-08 00:00:00.000000000 Z
14
+ date: 2017-05-16 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: padrino-support
@@ -19,14 +19,14 @@ dependencies:
19
19
  requirements:
20
20
  - - '='
21
21
  - !ruby/object:Gem::Version
22
- version: 0.14.0.2
22
+ version: 0.14.1
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
26
26
  requirements:
27
27
  - - '='
28
28
  - !ruby/object:Gem::Version
29
- version: 0.14.0.2
29
+ version: 0.14.1
30
30
  - !ruby/object:Gem::Dependency
31
31
  name: tilt
32
32
  requirement: !ruby/object:Gem::Requirement
@@ -126,6 +126,7 @@ files:
126
126
  - lib/padrino/rendering/erubi_template.rb
127
127
  - lib/padrino/rendering/erubis_template.rb
128
128
  - lib/padrino/rendering/haml_template.rb
129
+ - lib/padrino/rendering/hamlit_template.rb
129
130
  - lib/padrino/rendering/slim_template.rb
130
131
  - lib/padrino/safe_buffer.rb
131
132
  - padrino-helpers.gemspec