Module: Erubis::Basic::Converter

Includes:
Converter
Included in:
Basic::Engine
Defined in:
lib/erubis/converter.rb

Overview

basic converter which supports ‘<% … %>’ notation.

Instance Attribute Summary collapse

Attributes included from Converter

#escape, #postamble, #preamble

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Converter

#convert

Instance Attribute Details

#patternObject

Returns the value of attribute pattern.



102
103
104
# File 'lib/erubis/converter.rb', line 102

def pattern
  @pattern
end

#trimObject

Returns the value of attribute trim.



102
103
104
# File 'lib/erubis/converter.rb', line 102

def trim
  @trim
end

Class Method Details

.pattern_regexp(pattern) ⇒ Object

return regexp of pattern to parse eRuby script



113
114
115
116
117
118
# File 'lib/erubis/converter.rb', line 113

def pattern_regexp(pattern)
  @prefix, @postfix = pattern.split()   # '<% %>' => '<%', '%>'
  #return /(.*?)(^[ \t]*)?#{@prefix}(=+|\#)?(.*?)-?#{@postfix}([ \t]*\r?\n)?/m
  #return /(^[ \t]*)?#{@prefix}(=+|\#)?(.*?)-?#{@postfix}([ \t]*\r?\n)?/m
  return /#{@prefix}(=+|-|\#|%)?(.*?)([-=])?#{@postfix}([ \t]*\r?\n)?/m
end

.supported_propertiesObject

:nodoc:



95
96
97
98
99
100
# File 'lib/erubis/converter.rb', line 95

def self.supported_properties    # :nodoc:
  return [
          [:pattern,  '<% %>', "embed pattern"],
          [:trim,      true,   "trim spaces around <% ... %>"],
         ]
end

Instance Method Details

#add_expr(src, code, indicator) ⇒ Object

add expression code to src



176
177
178
179
180
181
182
183
184
185
# File 'lib/erubis/converter.rb', line 176

def add_expr(src, code, indicator)
  case indicator
  when '='
    @escape ? add_expr_escaped(src, code) : add_expr_literal(src, code)
  when '=='
    @escape ? add_expr_literal(src, code) : add_expr_escaped(src, code)
  when '==='
    add_expr_debug(src, code)
  end
end

#convert_input(src, input) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/erubis/converter.rb', line 128

def convert_input(src, input)
  pat = @pattern
  regexp = pat.nil? || pat == '<% %>' ? DEFAULT_REGEXP : pattern_regexp(pat)
  pos = 0
  is_bol = true     # is beginning of line
  input.scan(regexp) do |indicator, code, tailch, 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(src, text) if text && !text.empty?
    ## * when '<%= %>', do nothing
    ## * when '<% %>' or '<%# %>', delete spaces iff only spaces are around '<% %>'
    if ch == ?=              # <%= %>
      rspace = nil if tailch && !tailch.empty?
      add_text(src, lspace) if lspace
      add_expr(src, code, indicator)
      add_text(src, rspace) if rspace
    elsif ch == ?\#          # <%# %>
      n = code.count("\n") + (rspace ? 1 : 0)
      if @trim && lspace && rspace
        add_stmt(src, "\n" * n)
      else
        add_text(src, lspace) if lspace
        add_stmt(src, "\n" * n)
        add_text(src, rspace) if rspace
      end
    elsif ch == ?%           # <%% %>
      s = "#{lspace}#{@prefix||='<%'}#{code}#{tailch}#{@postfix||='%>'}#{rspace}"
      add_text(src, s)
    else                     # <% %>
      if @trim && lspace && rspace
        add_stmt(src, "#{lspace}#{code}#{rspace}")
      else
        add_text(src, lspace) if lspace
        add_stmt(src, code)
        add_text(src, rspace) if rspace
      end
    end
  end
  rest = $' || input     # add input when no matched
  add_text(src, rest)
end

#init_converter(properties = {}) ⇒ Object



104
105
106
107
108
# File 'lib/erubis/converter.rb', line 104

def init_converter(properties={})
  super(properties)
  @pattern = properties[:pattern]
  @trim    = properties[:trim] != false
end