Module | Erubis::Basic::Converter |
In: |
lib/erubis/converter.rb
|
basic converter which supports ’<% … %>’ notation.
DEFAULT_REGEXP | = | pattern_regexp('<% %>') | DEFAULT_REGEXP = /(.*?)(^[ \t]*)?<%(=+|\#)?(.*?)-?%>([ \t]*\r?\n)?/m DEFAULT_REGEXP = /(^[ \t]*)?<%(=+|\#)?(.*?)-?%>([ \t]*\r?\n)?/m DEFAULT_REGEXP = /<%(=+|\#)?(.*?)-?%>([ \t]*\r?\n)?/m |
pattern | [RW] | |
trim | [RW] |
add expression code to src
# File lib/erubis/converter.rb, line 177 177: def add_expr(src, code, indicator) 178: case indicator 179: when '=' 180: @escape ? add_expr_escaped(src, code) : add_expr_literal(src, code) 181: when '==' 182: @escape ? add_expr_literal(src, code) : add_expr_escaped(src, code) 183: when '===' 184: add_expr_debug(src, code) 185: end 186: end
# File lib/erubis/converter.rb, line 128 128: def convert_input(src, input) 129: pat = @pattern 130: regexp = pat.nil? || pat == '<% %>' ? DEFAULT_REGEXP : pattern_regexp(pat) 131: pos = 0 132: is_bol = true # is beginning of line 133: input.scan(regexp) do |indicator, code, tailch, rspace| 134: match = Regexp.last_match() 135: len = match.begin(0) - pos 136: text = input[pos, len] 137: pos = match.end(0) 138: ch = indicator ? indicator[0] : nil 139: lspace = ch == ?= ? nil : detect_spaces_at_bol(text, is_bol) 140: is_bol = rspace ? true : false 141: add_text(src, text) if text && !text.empty? 142: ## * when '<%= %>', do nothing 143: ## * when '<% %>' or '<%# %>', delete spaces iff only spaces are around '<% %>' 144: if ch == ?= # <%= %> 145: rspace = nil if tailch && !tailch.empty? 146: add_text(src, lspace) if lspace 147: add_expr(src, code, indicator) 148: add_text(src, rspace) if rspace 149: elsif ch == ?\# # <%# %> 150: n = code.count("\n") + (rspace ? 1 : 0) 151: if @trim && lspace && rspace 152: add_stmt(src, "\n" * n) 153: else 154: add_text(src, lspace) if lspace 155: add_stmt(src, "\n" * n) 156: add_text(src, rspace) if rspace 157: end 158: elsif ch == ?% # <%% %> 159: s = "#{lspace}#{@prefix||='<%'}#{code}#{tailch}#{@postfix||='%>'}#{rspace}" 160: add_text(src, s) 161: else # <% %> 162: if @trim && lspace && rspace 163: add_stmt(src, "#{lspace}#{code}#{rspace}") 164: else 165: add_text(src, lspace) if lspace 166: add_stmt(src, code) 167: add_text(src, rspace) if rspace 168: end 169: end 170: end 171: #rest = $' || input # ruby1.8 172: rest = pos == 0 ? input : input[pos..-1] # ruby1.9 173: add_text(src, rest) 174: end
# File lib/erubis/converter.rb, line 104 104: def init_converter(properties={}) 105: super(properties) 106: @pattern = properties[:pattern] 107: @trim = properties[:trim] != false 108: end
return regexp of pattern to parse eRuby script
# File lib/erubis/converter.rb, line 113 113: def pattern_regexp(pattern) 114: @prefix, @postfix = pattern.split() # '<% %>' => '<%', '%>' 115: #return /(.*?)(^[ \t]*)?#{@prefix}(=+|\#)?(.*?)-?#{@postfix}([ \t]*\r?\n)?/m 116: #return /(^[ \t]*)?#{@prefix}(=+|\#)?(.*?)-?#{@postfix}([ \t]*\r?\n)?/m 117: return /#{@prefix}(=+|-|\#|%)?(.*?)([-=])?#{@postfix}([ \t]*\r?\n)?/m 118: end