Module: Erubis::BiPatternEnhancer

Included in:
BiPatternEruby
Defined in:
lib/erubis/enhancer.rb

Overview

enable to use other embedded expression pattern (default is ‘[= =]’).

notice! this is an experimental. spec may change in the future.

ex.

input = <<END
<% for item in list %>
  <%= item %> : <%== item %>
  [= item =] : [== item =]
<% end %>
END

class BiPatternEruby
  include BiPatternEnhancer
end
eruby = BiPatternEruby.new(input, :bipattern=>'\[= =\]')
list = ['<a>', 'b&b', '"c"']
print eruby.result(binding())

## output
  <a> : &lt;a&gt;
  <a> : &lt;a&gt;
  b&b : b&amp;b
  b&b : b&amp;b
  "c" : &quot;c&quot;
  "c" : &quot;c&quot;

this is language independent.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.descObject

:nodoc:



404
405
406
# File 'lib/erubis/enhancer.rb', line 404

def self.desc   # :nodoc:
  "another embedded expression pattern (default '\[= =\]')."
end

Instance Method Details

#add_text(src, text) ⇒ Object



420
421
422
423
424
425
426
427
428
429
430
431
# File 'lib/erubis/enhancer.rb', line 420

def add_text(src, text)
  return unless text
  m = nil
  text.scan(@bipattern_regexp) do |txt, indicator, code|
    m = Regexp.last_match
    super(src, txt)
    add_expr(src, code, '=' + indicator)
  end
  #rest = $' || text                    # ruby1.8
  rest = m ? text[m.end(0)..-1] : text  # ruby1.9
  super(src, rest)
end

#bipattern=(pat) ⇒ Object

when pat is nil then ‘[= =]’ is used



414
415
416
417
418
# File 'lib/erubis/enhancer.rb', line 414

def bipattern=(pat)   # :nodoc:
  @bipattern = pat || '\[= =\]'
  pre, post = @bipattern.split()
  @bipattern_regexp = /(.*?)#{pre}(=*)(.*?)#{post}/m
end

#initialize(input, properties = {}) ⇒ Object



408
409
410
411
# File 'lib/erubis/enhancer.rb', line 408

def initialize(input, properties={})
  self.bipattern = properties[:bipattern]    # or '\$\{ \}'
  super
end