class Erubi::CaptureEndEngine
An engine class that supports capturing blocks via the <%|=
and <%|==
tags, explicitly ending the captures using <%|
end %>
blocks.
Public Class Methods
Initializes the engine. Accepts the same arguments as ::Erubi::Engine
, and these additional options:
- :escape_capture
-
Whether to make
<%|=
escape by default, and<%|==
not escape by default, defaults to the same value as :escape. - :yield_returns_buffer
-
Whether to have
<%|
tags insert the buffer as an expression, so that<%| end %>
tags will have the buffer be the last expression inside the block, and therefore have the buffer be returned by the yield expression. Normally the buffer will be returned anyway, but there are cases where the last expression will not be the buffer, and therefore a different object will be returned.
Erubi::Engine::new
# File lib/erubi/capture_end.rb 19 def initialize(input, properties={}) 20 properties = Hash[properties] 21 escape = properties.fetch(:escape){properties.fetch(:escape_html, false)} 22 @escape_capture = properties.fetch(:escape_capture, escape) 23 @yield_returns_buffer = properties.fetch(:yield_returns_buffer, false) 24 @bufval = properties[:bufval] ||= '::String.new' 25 @bufstack = '__erubi_stack' 26 properties[:regexp] ||= /<%(\|?={1,2}|-|\#|%|\|)?(.*?)([-=])?%>([ \t]*\r?\n)?/m 27 super 28 end
Private Instance Methods
Handle the <%|= and <%|== tags
Erubi::Engine#handle
# File lib/erubi/capture_end.rb 33 def handle(indicator, code, tailch, rspace, lspace) 34 case indicator 35 when '|=', '|==' 36 rspace = nil if tailch && !tailch.empty? 37 add_text(lspace) if lspace 38 escape_capture = !((indicator == '|=') ^ @escape_capture) 39 src << "begin; (#{@bufstack} ||= []) << #{@bufvar}; #{@bufvar} = #{@bufval}; #{@bufstack}.last << #{@escapefunc if escape_capture}((" << code 40 add_text(rspace) if rspace 41 when '|' 42 rspace = nil if tailch && !tailch.empty? 43 add_text(lspace) if lspace 44 result = @yield_returns_buffer ? " #{@bufvar}; " : "" 45 src << result << code << ")).to_s; ensure; #{@bufvar} = #{@bufstack}.pop; end;" 46 add_text(rspace) if rspace 47 else 48 super 49 end 50 end