Module: Erubis::InterpolationEnhancer

Included in:
FastEruby, InterpolationEruby
Defined in:
lib/erubis/enhancer.rb

Overview

convert “<h1><%=title%></h1>” into “_buf << %Q`<h1>#title</h1>‘”

this is only for Eruby.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.descObject

:nodoc:



571
572
573
# File 'lib/erubis/enhancer.rb', line 571

def self.desc   # :nodoc:
  "convert '<p><%=text%></p>' into '_buf << %Q`<p>\#{text}</p>`'"
end

Instance Method Details

#_add_text_to_str(str, text) ⇒ Object



639
640
641
642
643
# File 'lib/erubis/enhancer.rb', line 639

def _add_text_to_str(str, text)
  return if !text || text.empty?
  text.gsub!(/['\#\\]/, '\\\\\&')
  str << text
end

#add_expr_escaped(str, code) ⇒ Object



645
646
647
# File 'lib/erubis/enhancer.rb', line 645

def add_expr_escaped(str, code)
  str << "\#{#{escaped_expr(code)}}"
end

#add_expr_literal(str, code) ⇒ Object



649
650
651
# File 'lib/erubis/enhancer.rb', line 649

def add_expr_literal(str, code)
  str << "\#{#{code}}"
end

#add_text(src, text) ⇒ Object



628
629
630
631
632
633
634
635
636
637
# File 'lib/erubis/enhancer.rb', line 628

def add_text(src, text)
  return if !text || text.empty?
  #src << " _buf << %Q`" << text << "`;"
  if text[-1] == ?\n
    text[-1] = "\\n"
    src << " _buf << %Q`" << text << "`\n"
  else
    src << " _buf << %Q`" << text << "`;"
  end
end

#convert_input(src, input) ⇒ Object



575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
# File 'lib/erubis/enhancer.rb', line 575

def convert_input(src, input)
  pat = @pattern
  regexp = pat.nil? || pat == '<% %>' ? Basic::Converter::DEFAULT_REGEXP : pattern_regexp(pat)
  pos = 0
  is_bol = true     # is beginning of line
  str = ''
  input.scan(regexp) do |indicator, code, rspace|
    match = Regexp.last_match()
    len  = match.begin(0) - pos
    text = input[pos, len]
    pos  = match.end(0)
    ch   = indicator ? indicator[0] : nil
    lspace = ch == ?= ? nil : detect_spaces_at_bol(text, is_bol)
    is_bol = rspace ? true : false
    _add_text_to_str(str, text)
    ## * when '<%= %>', do nothing
    ## * when '<% %>' or '<%# %>', delete spaces iff only spaces are around '<% %>'
    if ch == ?=              # <%= %>
      str << lspace if lspace
      add_expr(str, code, indicator)
      str << rspace if rspace
    elsif ch == ?\#          # <%# %>
      n = code.count("\n") + (rspace ? 1 : 0)
      if @trim && lspace && rspace
        add_text(src, str)
        str = ''
        add_stmt(src, "\n" * n)
      else
        str << lspace if lspace
        add_text(src, str)
        str = ''
        add_stmt(src, "\n" * n)
        str << rspace if rspace
      end
    else                     # <% %>
      if @trim && lspace && rspace
        add_text(src, str)
        str = ''
        add_stmt(src, "#{lspace}#{code}#{rspace}")
      else
        str << lspace if lspace
        add_text(src, str)
        str = ''
        add_stmt(src, code)
        str << rspace if rspace
      end
    end
  end
  rest = $' || input     # add input when no matched
  _add_text_to_str(str, rest)
  add_text(src, str)
end