Skip to content

Commit

Permalink
Support blocks inside ERB content areas (#346)
Browse files Browse the repository at this point in the history
Supports block being inside <%= .. %> erb blocks. This differs from
standard erb parsers, but rails adds support for this to allow for some
more complicated view helpers. This support doesnt break or complicate
the output buffer, so it is added here as a baseline.
adambeynon committed Aug 5, 2013

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent ca3a0d9 commit 9f73f5d
Showing 4 changed files with 24 additions and 4 deletions.
13 changes: 10 additions & 3 deletions lib/opal/erb.rb
Original file line number Diff line number Diff line change
@@ -22,21 +22,28 @@ def fix_quotes
@result = @result.gsub '"', '\\"'
end

BLOCK_EXPR = /\s+(do|\{)(\s*\|[^|]*\|)?\s*\Z/

def find_contents
@result = @result.gsub(/<%=([\s\S]+?)%>/) do
inner = $1.gsub(/\\'/, "'").gsub(/\\"/, '"')
"\")\nout.<<(#{ inner })\nout.<<(\""

if inner =~ BLOCK_EXPR
"\")\noutput_buffer.append= #{ inner }\noutput_buffer.append(\""
else
"\")\noutput_buffer.append=(#{ inner })\noutput_buffer.append(\""
end
end
end

def find_code
@result = @result.gsub(/<%([\s\S]+?)%>/) do
"\")\n#{ $1 }\nout.<<(\""
"\")\n#{ $1 }\noutput_buffer.append(\""
end
end

def wrap_compiled
@result = "ERB.new('#@file_name') do |out|\nout.<<(\"#@result\")\nout.join\nend\n"
@result = "ERB.new('#@file_name') do |output_buffer|\noutput_buffer.append(\"#@result\")\noutput_buffer.join\nend\n"
end
end
end
6 changes: 6 additions & 0 deletions spec/opal/erb/erb_spec.rb
Original file line number Diff line number Diff line change
@@ -2,11 +2,13 @@

require File.expand_path('../simple', __FILE__)
require File.expand_path('../quoted', __FILE__)
require File.expand_path('../inline_block', __FILE__)

describe "ERB files" do
before :each do
@simple = Template['opal/erb/simple']
@quoted = Template['opal/erb/quoted']
@inline_block = Template['opal/erb/inline_block']
end

it "should be defined by their filename on Template namespace" do
@@ -22,4 +24,8 @@
@name = "adam"
@quoted.render(self).should == "<div class=\"foo\">hello there adam</div>\n"
end

it "should be able to handle inline blocks" do
@inline_block.should be_kind_of(ERB)
end
end
3 changes: 3 additions & 0 deletions spec/opal/erb/inline_block.opalerb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<%= some_helper do %>
<h1>Hi there</h1>
<% end %>
6 changes: 5 additions & 1 deletion stdlib/erb.rb
Original file line number Diff line number Diff line change
@@ -29,10 +29,14 @@ def initialize
@buffer = []
end

def <<(str)
def append(str)
@buffer << str
end

def append=(content)
@buffer << content
end

def join
@buffer.join
end

0 comments on commit 9f73f5d

Please sign in to comment.