homura-runtime 0.2.17 → 0.2.19

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
  SHA256:
3
- metadata.gz: 484afdeb8e2980e08e029bf515a07530b08296357eaa6bfbf255a721104358bf
4
- data.tar.gz: 77bf0079ae55ecdbbe137d6b804d8501a90f66daa3859e22d9e9114fad582830
3
+ metadata.gz: 94e4ec7af2878bc59ebedd0ebe8841d5852b4fb35d736d713a62ce1ef1bb1cfb
4
+ data.tar.gz: 6f21d253588daa097a1541ddd017b58a28b10c38ccc703f2a3ee9e6c650f490f
5
5
  SHA512:
6
- metadata.gz: 89a21e0c74732fa2b2467ccff72a3ca365c706d0928c9ffb3fc4e4f2b96ecf8688fc4bdbf3beda1e806122d8abca9053d787a528c48d13a3f709aeb8c57ab027
7
- data.tar.gz: 79975dd3ce599d61824919d790ec05727603043887577ece2af6231136e320514837ca54e710095e4c8a3aadfe06fc9ebf97ae791da0255cf19feea6eef83803
6
+ metadata.gz: dd0b2cb20babf2fe3cb57167dfdc509fef69ccd5d141cf44c0a370b9e8aca29425267433eb4fdf002b0e081583c8028deb1799d4e8c53e808a2b175f2a611c7e
7
+ data.tar.gz: d794586f918cef8b0cabebec3923525078c69a9b68e75e0da96096ffa8afdc02aceb6cd277b07f8b15c0396a3f6f6b6ecf77e2c0c9308cd27624acce320b45ad
data/exe/compile-erb CHANGED
@@ -287,11 +287,21 @@ def emit_header(io, namespace)
287
287
  body = @templates[name.to_sym]
288
288
  raise "#{namespace}: no template registered for \#{name.inspect}" unless body
289
289
  previous_block = instance.instance_variable_get(:@__homura_template_block__)
290
+ previous_locals = instance.instance_variable_get(:@__homura_template_locals__)
290
291
  begin
291
292
  instance.instance_variable_set(:@__homura_template_block__, block)
293
+ # Stack locals so nested `erb :_partial, locals: {...}` calls
294
+ # restore the outer scope on the way out — Sinatra's standard
295
+ # `locals[:t]` -> bare `t` lookup is implemented via
296
+ # method_missing on the Sinatra instance (see Sinatra::Base
297
+ # patch installed by emit_sinatra_patch).
298
+ stack = previous_locals.is_a?(::Array) ? previous_locals.dup : []
299
+ stack.push(locals || {})
300
+ instance.instance_variable_set(:@__homura_template_locals__, stack)
292
301
  instance.instance_exec(locals, &body)
293
302
  ensure
294
303
  instance.instance_variable_set(:@__homura_template_block__, previous_block)
304
+ instance.instance_variable_set(:@__homura_template_locals__, previous_locals)
295
305
  end
296
306
  end
297
307
 
@@ -341,6 +351,61 @@ def emit_sinatra_patch(io, namespace)
341
351
  block.call
342
352
  end
343
353
 
354
+ # Resolve a Sinatra-style local variable reference.
355
+ # `erb :_partial, locals: { t: row }` causes the precompiled body
356
+ # to reference a bare `t`. Stock Sinatra uses Ruby's `binding`
357
+ # eval to install locals into the template's scope; we don't have
358
+ # `binding` under Opal/Workers, so we hand-roll the lookup
359
+ # through method_missing against the locals stack maintained by
360
+ # the renderer.
361
+ def __homura_template_local__(name)
362
+ stack = @__homura_template_locals__
363
+ return nil unless stack.is_a?(::Array)
364
+ key = name.to_sym
365
+ stack.reverse_each do |frame|
366
+ next unless frame.is_a?(::Hash)
367
+ return frame[key] if frame.key?(key)
368
+ sk = name.to_s
369
+ return frame[sk] if frame.key?(sk)
370
+ end
371
+ nil
372
+ end
373
+
374
+ def __homura_template_local_defined?(name)
375
+ stack = @__homura_template_locals__
376
+ return false unless stack.is_a?(::Array)
377
+ key = name.to_sym
378
+ sk = name.to_s
379
+ stack.reverse_each do |frame|
380
+ next unless frame.is_a?(::Hash)
381
+ return true if frame.key?(key) || frame.key?(sk)
382
+ end
383
+ false
384
+ end
385
+ end
386
+ end
387
+
388
+ # Method-missing fallback that surfaces template locals as bare names.
389
+ # Installed on Sinatra::Base so `<%= t['title'] %>` inside a
390
+ # precompiled partial Just Works without the user having to write
391
+ # `<%= locals[:t]['title'] %>`.
392
+ class ::Sinatra::Base
393
+ def method_missing(name, *args, &block)
394
+ if args.empty? && block.nil? && __homura_template_local_defined?(name)
395
+ return __homura_template_local__(name)
396
+ end
397
+ super
398
+ end
399
+
400
+ def respond_to_missing?(name, include_private = false)
401
+ return true if __homura_template_local_defined?(name)
402
+ super
403
+ end
404
+ end
405
+
406
+ module ::Sinatra
407
+ module Templates
408
+
344
409
  def __homura_default_template_block_for__(template)
345
410
  case template.to_sym
346
411
  when :layout
@@ -350,6 +415,16 @@ def emit_sinatra_patch(io, namespace)
350
415
  end
351
416
  end
352
417
 
418
+ # A template is "layout-like" if its name is `:layout` or starts
419
+ # with `layout_` (e.g. `:layout_docs`). The auto-layout pass in
420
+ # `erb` skips wrapping such templates in `:layout` so that
421
+ # rendering `:layout_docs` doesn't end up nested inside `:layout`,
422
+ # which would otherwise produce a duplicate header / nav.
423
+ def __homura_layout_template?(template)
424
+ name = template.to_sym
425
+ name == :layout || name.to_s.start_with?('layout_')
426
+ end
427
+
353
428
  # homura patch: dispatch to precompiled templates when we
354
429
  # have one. Unknown symbols raise a clear message instead of
355
430
  # wandering into upstream Tilt, which would blow up on
@@ -367,7 +442,7 @@ def emit_sinatra_patch(io, namespace)
367
442
 
368
443
  layout = options[:layout]
369
444
  layout = false if layout.nil? && options.include?(:layout)
370
- if layout.nil? && template.to_sym != :layout && ::#{namespace}.registered?(:layout)
445
+ if layout.nil? && !__homura_layout_template?(template) && ::#{namespace}.registered?(:layout)
371
446
  layout = :layout
372
447
  end
373
448
  if layout
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module CloudflareWorkers
4
- VERSION = '0.2.17'
4
+ VERSION = '0.2.19'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: homura-runtime
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.17
4
+ version: 0.2.19
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kazuhiro Homma