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 terminate_expression 40 @src << "begin; (#{@bufstack} ||= []) << #{@bufvar}; #{@bufvar} = #{@bufval}; #{@bufstack}.last << #{@escapefunc if escape_capture}((" << code 41 @buffer_on_stack = false 42 add_text(rspace) if rspace 43 when '|' 44 rspace = nil if tailch && !tailch.empty? 45 add_text(lspace) if lspace 46 if @yield_returns_buffer 47 terminate_expression 48 @src << " #{@bufvar}; " 49 end 50 @src << code << ")).to_s; ensure; #{@bufvar} = #{@bufstack}.pop; end;" 51 @buffer_on_stack = false 52 add_text(rspace) if rspace 53 else 54 super 55 end 56 end