/usr/lib/ruby/vendor_ruby/erubis/converter.rb is in ruby-erubis 2.7.0-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 | ##
## $Release: 2.7.0 $
## copyright(c) 2006-2011 kuwata-lab.com all rights reserved.
##
require 'erubis/util'
module Erubis
##
## convert
##
module Converter
attr_accessor :preamble, :postamble, :escape
def self.supported_properties # :nodoc:
return [
[:preamble, nil, "preamble (no preamble when false)"],
[:postamble, nil, "postamble (no postamble when false)"],
[:escape, nil, "escape expression or not in default"],
]
end
def init_converter(properties={})
@preamble = properties[:preamble]
@postamble = properties[:postamble]
@escape = properties[:escape]
end
## convert input string into target language
def convert(input)
codebuf = "" # or []
@preamble.nil? ? add_preamble(codebuf) : (@preamble && (codebuf << @preamble))
convert_input(codebuf, input)
@postamble.nil? ? add_postamble(codebuf) : (@postamble && (codebuf << @postamble))
@_proc = nil # clear cached proc object
return codebuf # or codebuf.join()
end
protected
##
## detect spaces at beginning of line
##
def detect_spaces_at_bol(text, is_bol)
lspace = nil
if text.empty?
lspace = "" if is_bol
elsif text[-1] == ?\n
lspace = ""
else
rindex = text.rindex(?\n)
if rindex
s = text[rindex+1..-1]
if s =~ /\A[ \t]*\z/
lspace = s
#text = text[0..rindex]
text[rindex+1..-1] = ''
end
else
if is_bol && text =~ /\A[ \t]*\z/
#lspace = text
#text = nil
lspace = text.dup
text[0..-1] = ''
end
end
end
return lspace
end
##
## (abstract) convert input to code
##
def convert_input(codebuf, input)
not_implemented
end
end
module Basic
end
##
## basic converter which supports '<% ... %>' notation.
##
module Basic::Converter
include Erubis::Converter
def self.supported_properties # :nodoc:
return [
[:pattern, '<% %>', "embed pattern"],
[:trim, true, "trim spaces around <% ... %>"],
]
end
attr_accessor :pattern, :trim
def init_converter(properties={})
super(properties)
@pattern = properties[:pattern]
@trim = properties[:trim] != false
end
protected
## return regexp of pattern to parse eRuby script
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
module_function :pattern_regexp
#DEFAULT_REGEXP = /(.*?)(^[ \t]*)?<%(=+|\#)?(.*?)-?%>([ \t]*\r?\n)?/m
#DEFAULT_REGEXP = /(^[ \t]*)?<%(=+|\#)?(.*?)-?%>([ \t]*\r?\n)?/m
#DEFAULT_REGEXP = /<%(=+|\#)?(.*?)-?%>([ \t]*\r?\n)?/m
DEFAULT_REGEXP = pattern_regexp('<% %>')
public
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 # ruby1.8
rest = pos == 0 ? input : input[pos..-1] # ruby1.9
add_text(src, rest)
end
## add expression code to src
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
end
module PI
end
##
## Processing Instructions (PI) converter for XML.
## this class converts '<?rb ... ?>' and '${...}' notation.
##
module PI::Converter
include Erubis::Converter
def self.desc # :nodoc:
"use processing instructions (PI) instead of '<% %>'"
end
def self.supported_properties # :nodoc:
return [
[:trim, true, "trim spaces around <% ... %>"],
[:pi, 'rb', "PI (Processing Instrunctions) name"],
[:embchar, '@', "char for embedded expression pattern('@{...}@')"],
[:pattern, '<% %>', "embed pattern"],
]
end
attr_accessor :pi, :prefix
def init_converter(properties={})
super(properties)
@trim = properties.fetch(:trim, true)
@pi = properties[:pi] if properties[:pi]
@embchar = properties[:embchar] || '@'
@pattern = properties[:pattern]
@pattern = '<% %>' if @pattern.nil? #|| @pattern == true
end
def convert(input)
code = super(input)
return @header || @footer ? "#{@header}#{code}#{@footer}" : code
end
protected
def convert_input(codebuf, input)
unless @regexp
@pi ||= 'e'
ch = Regexp.escape(@embchar)
if @pattern
left, right = @pattern.split(' ')
@regexp = /<\?#{@pi}(?:-(\w+))?(\s.*?)\?>([ \t]*\r?\n)?|#{ch}(!*)?\{(.*?)\}#{ch}|#{left}(=+)(.*?)#{right}/m
else
@regexp = /<\?#{@pi}(?:-(\w+))?(\s.*?)\?>([ \t]*\r?\n)?|#{ch}(!*)?\{(.*?)\}#{ch}/m
end
end
#
is_bol = true
pos = 0
input.scan(@regexp) do |pi_arg, stmt, rspace,
indicator1, expr1, indicator2, expr2|
match = Regexp.last_match
len = match.begin(0) - pos
text = input[pos, len]
pos = match.end(0)
lspace = stmt ? detect_spaces_at_bol(text, is_bol) : nil
is_bol = stmt && rspace ? true : false
add_text(codebuf, text) # unless text.empty?
#
if stmt
if @trim && lspace && rspace
add_pi_stmt(codebuf, "#{lspace}#{stmt}#{rspace}", pi_arg)
else
add_text(codebuf, lspace) if lspace
add_pi_stmt(codebuf, stmt, pi_arg)
add_text(codebuf, rspace) if rspace
end
else
add_pi_expr(codebuf, expr1 || expr2, indicator1 || indicator2)
end
end
#rest = $' || input # ruby1.8
rest = pos == 0 ? input : input[pos..-1] # ruby1.9
add_text(codebuf, rest)
end
#--
#def convert_input(codebuf, input)
# parse_stmts(codebuf, input)
# #parse_stmts2(codebuf, input)
#end
#
#def parse_stmts(codebuf, input)
# #regexp = pattern_regexp(@pattern)
# @pi ||= 'e'
# @stmt_pattern ||= /<\?#{@pi}(?:-(\w+))?(\s.*?)\?>([ \t]*\r?\n)?/m
# is_bol = true
# pos = 0
# input.scan(@stmt_pattern) do |pi_arg, code, rspace|
# match = Regexp.last_match
# len = match.begin(0) - pos
# text = input[pos, len]
# pos = match.end(0)
# lspace = detect_spaces_at_bol(text, is_bol)
# is_bol = rspace ? true : false
# parse_exprs(codebuf, text) # unless text.empty?
# if @trim && lspace && rspace
# add_pi_stmt(codebuf, "#{lspace}#{code}#{rspace}", pi_arg)
# else
# add_text(codebuf, lspace)
# add_pi_stmt(codebuf, code, pi_arg)
# add_text(codebuf, rspace)
# end
# end
# rest = $' || input
# parse_exprs(codebuf, rest)
#end
#
#def parse_exprs(codebuf, input)
# unless @expr_pattern
# ch = Regexp.escape(@embchar)
# if @pattern
# left, right = @pattern.split(' ')
# @expr_pattern = /#{ch}(!*)?\{(.*?)\}#{ch}|#{left}(=+)(.*?)#{right}/
# else
# @expr_pattern = /#{ch}(!*)?\{(.*?)\}#{ch}/
# end
# end
# pos = 0
# input.scan(@expr_pattern) do |indicator1, code1, indicator2, code2|
# indicator = indicator1 || indicator2
# code = code1 || code2
# match = Regexp.last_match
# len = match.begin(0) - pos
# text = input[pos, len]
# pos = match.end(0)
# add_text(codebuf, text) # unless text.empty?
# add_pi_expr(codebuf, code, indicator)
# end
# rest = $' || input
# add_text(codebuf, rest)
#end
#++
def add_pi_stmt(codebuf, code, pi_arg) # :nodoc:
case pi_arg
when nil ; add_stmt(codebuf, code)
when 'header' ; @header = code
when 'footer' ; @footer = code
when 'comment'; add_stmt(codebuf, "\n" * code.count("\n"))
when 'value' ; add_expr_literal(codebuf, code)
else ; add_stmt(codebuf, code)
end
end
def add_pi_expr(codebuf, code, indicator) # :nodoc:
case indicator
when nil, '', '==' # @{...}@ or <%== ... %>
@escape == false ? add_expr_literal(codebuf, code) : add_expr_escaped(codebuf, code)
when '!', '=' # @!{...}@ or <%= ... %>
@escape == false ? add_expr_escaped(codebuf, code) : add_expr_literal(codebuf, code)
when '!!', '===' # @!!{...}@ or <%=== ... %>
add_expr_debug(codebuf, code)
else
# ignore
end
end
end
end
|