Skip to content

Commit

Permalink
Merge pull request #1580 from k0kubun/hamlit-support
Browse files Browse the repository at this point in the history
Support capture and content_for with Hamlit
  • Loading branch information
namusyaka committed Dec 30, 2019
2 parents d423f56 + 58e1be0 commit 7c144ee
Show file tree
Hide file tree
Showing 16 changed files with 39 additions and 6 deletions.
4 changes: 3 additions & 1 deletion sinatra-contrib/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ group :development, :test do
end

platform :jruby, :ruby do
gem 'slim', '2.1.0'
gem 'hamlit'
gem 'hamlit-block', '>= 0.7.1'
gem 'liquid', '~> 2.6.x'
gem 'slim'
end

platform :ruby do
Expand Down
2 changes: 1 addition & 1 deletion sinatra-contrib/lib/sinatra/capture.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ module Capture

def capture(*args, &block)
return block[*args] if ruby?
if haml?
if haml? && Tilt[:haml] == Tilt::HamlTemplate
buffer = Haml::Buffer.new(nil, Haml::Options.new.for_buffer)
with_haml_buffer(buffer) { capture_haml(*args, &block) }
else
Expand Down
2 changes: 1 addition & 1 deletion sinatra-contrib/lib/sinatra/content_for.rb
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def clear_content_for(key)
# for <tt>:head</tt>.
def yield_content(key, *args, &block)
if block_given? && !content_for?(key)
haml? ? capture_haml(*args, &block) : yield(*args)
(haml? && Tilt[:haml] == Tilt::HamlTemplate) ? capture_haml(*args, &block) : yield(*args)
else
content = content_blocks[key.to_sym].map { |b| capture(*args, &b) }
content.join.tap do |c|
Expand Down
4 changes: 2 additions & 2 deletions sinatra-contrib/lib/sinatra/respond_with.rb
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,8 @@ def self.engines
:css => [:less, :sass, :scss],
:xml => [:builder, :nokogiri],
:js => [:coffee],
:html => [:erb, :erubi, :erubis, :haml, :slim, :liquid, :radius, :mab,
:markdown, :textile, :rdoc],
:html => [:erb, :erubi, :erubis, :haml, :halmit, :slim, :liquid, :radius,
:mab, :markdown, :textile, :rdoc],
:all => (Sinatra::Templates.instance_methods.map(&:to_sym) +
[:mab] - [:find_template, :markaby]),
:json => [:yajl],
Expand Down
4 changes: 4 additions & 0 deletions sinatra-contrib/spec/capture_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ def render(engine, template)
if engine == :erubi || engine == :erubis
lang = :erb
end
if engine == :hamlit
lang = :haml
end
require "#{engine}"

it "captures content" do
Expand All @@ -35,6 +38,7 @@ def render(engine, template)
end

describe('haml') { it_behaves_like "a template language", :haml }
describe('hamlit') { it_behaves_like "a template language", :hamlit }
describe('slim') { it_behaves_like "a template language", :slim }
describe('erubi') { it_behaves_like "a template language", :erubi }
describe('erubis') { it_behaves_like "a template language", :erubis }
Expand Down
2 changes: 2 additions & 0 deletions sinatra-contrib/spec/content_for/different_key.hamlit
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- content_for :bar do
bar
2 changes: 2 additions & 0 deletions sinatra-contrib/spec/content_for/footer.hamlit
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- if content_for? :foo
!= yield_content :foo
1 change: 1 addition & 0 deletions sinatra-contrib/spec/content_for/layout.hamlit
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
= yield_content :foo
8 changes: 8 additions & 0 deletions sinatra-contrib/spec/content_for/multiple_blocks.hamlit
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
- content_for :foo do
foo
- content_for :foo do
bar
- content_for :baz do
WON'T RENDER ME
- content_for :foo do
baz
3 changes: 3 additions & 0 deletions sinatra-contrib/spec/content_for/multiple_yields.hamlit
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
= yield_content :foo
= yield_content :foo
= yield_content :foo
1 change: 1 addition & 0 deletions sinatra-contrib/spec/content_for/parameter_value.hamlit
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- content_for :foo, 'foo'
1 change: 1 addition & 0 deletions sinatra-contrib/spec/content_for/passes_values.hamlit
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!= yield_content :foo, 1, 2
2 changes: 2 additions & 0 deletions sinatra-contrib/spec/content_for/same_key.hamlit
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- content_for :foo do
foo
3 changes: 3 additions & 0 deletions sinatra-contrib/spec/content_for/takes_values.hamlit
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- content_for :foo do |a, b|
%i= a
=b
2 changes: 2 additions & 0 deletions sinatra-contrib/spec/content_for/yield_block.hamlit
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
!= yield_content :foo do
baz
4 changes: 3 additions & 1 deletion sinatra-contrib/spec/content_for_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
end

Tilt.prefer Tilt::ERBTemplate
require 'hamlit/block'
Tilt.register Tilt::HamlTemplate, :haml

extend Forwardable
def_delegators :subject, :content_for, :clear_content_for, :yield_content
Expand Down Expand Up @@ -89,7 +91,7 @@ def render(engine, template)
end

# TODO: liquid radius markaby builder nokogiri
engines = %w[erb erubi erubis haml slim]
engines = %w[erb erubi erubis haml hamlit slim]

engines.each do |inner|
describe inner.capitalize do
Expand Down

0 comments on commit 7c144ee

Please sign in to comment.