/usr/lib/ruby/vendor_ruby/erubis/enhancer.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 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 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 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 | ##
## $Release: 2.7.0 $
## copyright(c) 2006-2011 kuwata-lab.com all rights reserved.
##
module Erubis
##
## switch '<%= ... %>' to escaped and '<%== ... %>' to unescaped
##
## ex.
## class XmlEruby < Eruby
## include EscapeEnhancer
## end
##
## this is language-indenedent.
##
module EscapeEnhancer
def self.desc # :nodoc:
"switch '<%= %>' to escaped and '<%== %>' to unescaped"
end
#--
#def self.included(klass)
# klass.class_eval <<-END
# alias _add_expr_literal add_expr_literal
# alias _add_expr_escaped add_expr_escaped
# alias add_expr_literal _add_expr_escaped
# alias add_expr_escaped _add_expr_literal
# END
#end
#++
def add_expr(src, code, indicator)
case indicator
when '='
@escape ? add_expr_literal(src, code) : add_expr_escaped(src, code)
when '=='
@escape ? add_expr_escaped(src, code) : add_expr_literal(src, code)
when '==='
add_expr_debug(src, code)
end
end
end
#--
## (obsolete)
#module FastEnhancer
#end
#++
##
## use $stdout instead of string
##
## this is only for Eruby.
##
module StdoutEnhancer
def self.desc # :nodoc:
"use $stdout instead of array buffer or string buffer"
end
def add_preamble(src)
src << "#{@bufvar} = $stdout;"
end
def add_postamble(src)
src << "\n''\n"
end
end
##
## use print statement instead of '_buf << ...'
##
## this is only for Eruby.
##
module PrintOutEnhancer
def self.desc # :nodoc:
"use print statement instead of '_buf << ...'"
end
def add_preamble(src)
end
def add_text(src, text)
src << " print '#{escape_text(text)}';" unless text.empty?
end
def add_expr_literal(src, code)
src << " print((#{code}).to_s);"
end
def add_expr_escaped(src, code)
src << " print #{escaped_expr(code)};"
end
def add_postamble(src)
src << "\n" unless src[-1] == ?\n
end
end
##
## enable print function
##
## Notice: use Eruby#evaluate() and don't use Eruby#result()
## to be enable print function.
##
## this is only for Eruby.
##
module PrintEnabledEnhancer
def self.desc # :nodoc:
"enable to use print function in '<% %>'"
end
def add_preamble(src)
src << "@_buf = "
super
end
def print(*args)
args.each do |arg|
@_buf << arg.to_s
end
end
def evaluate(context=nil)
_src = @src
if context.is_a?(Hash)
context.each do |key, val| instance_variable_set("@#{key}", val) end
elsif context
context.instance_variables.each do |name|
instance_variable_set(name, context.instance_variable_get(name))
end
end
return instance_eval(_src, (@filename || '(erubis)'))
end
end
##
## return array instead of string
##
## this is only for Eruby.
##
module ArrayEnhancer
def self.desc # :nodoc:
"return array instead of string"
end
def add_preamble(src)
src << "#{@bufvar} = [];"
end
def add_postamble(src)
src << "\n" unless src[-1] == ?\n
src << "#{@bufvar}\n"
end
end
##
## use an Array object as buffer (included in Eruby by default)
##
## this is only for Eruby.
##
module ArrayBufferEnhancer
def self.desc # :nodoc:
"use an Array object for buffering (included in Eruby class)"
end
def add_preamble(src)
src << "_buf = [];"
end
def add_postamble(src)
src << "\n" unless src[-1] == ?\n
src << "_buf.join\n"
end
end
##
## use String class for buffering
##
## this is only for Eruby.
##
module StringBufferEnhancer
def self.desc # :nodoc:
"use a String object for buffering"
end
def add_preamble(src)
src << "#{@bufvar} = '';"
end
def add_postamble(src)
src << "\n" unless src[-1] == ?\n
src << "#{@bufvar}.to_s\n"
end
end
##
## use StringIO class for buffering
##
## this is only for Eruby.
##
module StringIOEnhancer # :nodoc:
def self.desc # :nodoc:
"use a StringIO object for buffering"
end
def add_preamble(src)
src << "#{@bufvar} = StringIO.new;"
end
def add_postamble(src)
src << "\n" unless src[-1] == ?\n
src << "#{@bufvar}.string\n"
end
end
##
## set buffer variable name to '_erbout' as well as '_buf'
##
## this is only for Eruby.
##
module ErboutEnhancer
def self.desc # :nodoc:
"set '_erbout = _buf = \"\";' to be compatible with ERB."
end
def add_preamble(src)
src << "_erbout = #{@bufvar} = '';"
end
def add_postamble(src)
src << "\n" unless src[-1] == ?\n
src << "#{@bufvar}.to_s\n"
end
end
##
## remove text and leave code, especially useful when debugging.
##
## ex.
## $ erubis -s -E NoText file.eruby | more
##
## this is language independent.
##
module NoTextEnhancer
def self.desc # :nodoc:
"remove text and leave code (useful when debugging)"
end
def add_text(src, text)
src << ("\n" * text.count("\n"))
if text[-1] != ?\n
text =~ /^(.*?)\z/
src << (' ' * $1.length)
end
end
end
##
## remove code and leave text, especially useful when validating HTML tags.
##
## ex.
## $ erubis -s -E NoCode file.eruby | tidy -errors
##
## this is language independent.
##
module NoCodeEnhancer
def self.desc # :nodoc:
"remove code and leave text (useful when validating HTML)"
end
def add_preamble(src)
end
def add_postamble(src)
end
def add_text(src, text)
src << text
end
def add_expr(src, code, indicator)
src << "\n" * code.count("\n")
end
def add_stmt(src, code)
src << "\n" * code.count("\n")
end
end
##
## get convert faster, but spaces around '<%...%>' are not trimmed.
##
## this is language-independent.
##
module SimplifyEnhancer
def self.desc # :nodoc:
"get convert faster but leave spaces around '<% %>'"
end
#DEFAULT_REGEXP = /(^[ \t]*)?<%(=+|\#)?(.*?)-?%>([ \t]*\r?\n)?/m
SIMPLE_REGEXP = /<%(=+|\#)?(.*?)-?%>/m
def convert(input)
src = ""
add_preamble(src)
#regexp = pattern_regexp(@pattern)
pos = 0
input.scan(SIMPLE_REGEXP) do |indicator, code|
match = Regexp.last_match
index = match.begin(0)
text = input[pos, index - pos]
pos = match.end(0)
add_text(src, text)
if !indicator # <% %>
add_stmt(src, code)
elsif indicator[0] == ?\# # <%# %>
n = code.count("\n")
add_stmt(src, "\n" * n)
else # <%= %>
add_expr(src, code, indicator)
end
end
#rest = $' || input # ruby1.8
rest = pos == 0 ? input : input[pos..-1] # ruby1.9
add_text(src, rest)
add_postamble(src)
return src
end
end
##
## 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> : <a>
## <a> : <a>
## b&b : b&b
## b&b : b&b
## "c" : "c"
## "c" : "c"
##
## this is language independent.
##
module BiPatternEnhancer
def self.desc # :nodoc:
"another embedded expression pattern (default '\[= =\]')."
end
def initialize(input, properties={})
self.bipattern = properties[:bipattern] # or '\$\{ \}'
super
end
## when pat is nil then '\[= =\]' is used
def bipattern=(pat) # :nodoc:
@bipattern = pat || '\[= =\]'
pre, post = @bipattern.split()
@bipattern_regexp = /(.*?)#{pre}(=*)(.*?)#{post}/m
end
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
end
##
## regards lines starting with '^[ \t]*%' as program code
##
## in addition you can specify prefix character (default '%')
##
## this is language-independent.
##
module PrefixedLineEnhancer
def self.desc # :nodoc:
"regard lines matched to '^[ \t]*%' as program code"
end
def init_generator(properties={})
super
@prefixchar = properties[:prefixchar]
end
def add_text(src, text)
unless @prefixrexp
@prefixchar ||= '%'
@prefixrexp = Regexp.compile("^([ \\t]*)\\#{@prefixchar}(.*?\\r?\\n)")
end
pos = 0
text2 = ''
text.scan(@prefixrexp) do
space = $1
line = $2
space, line = '', $1 unless $2
match = Regexp.last_match
len = match.begin(0) - pos
str = text[pos, len]
pos = match.end(0)
if text2.empty?
text2 = str
else
text2 << str
end
if line[0, 1] == @prefixchar
text2 << space << line
else
super(src, text2)
text2 = ''
add_stmt(src, space + line)
end
end
#rest = pos == 0 ? text : $' # ruby1.8
rest = pos == 0 ? text : text[pos..-1] # ruby1.9
unless text2.empty?
text2 << rest if rest
rest = text2
end
super(src, rest)
end
end
##
## regards lines starting with '%' as program code
##
## this is for compatibility to eruby and ERB.
##
## this is language-independent.
##
module PercentLineEnhancer
include PrefixedLineEnhancer
def self.desc # :nodoc:
"regard lines starting with '%' as program code"
end
#--
#def init_generator(properties={})
# super
# @prefixchar = '%'
# @prefixrexp = /^\%(.*?\r?\n)/
#end
#++
def add_text(src, text)
unless @prefixrexp
@prefixchar = '%'
@prefixrexp = /^\%(.*?\r?\n)/
end
super(src, text)
end
end
##
## [experimental] allow header and footer in eRuby script
##
## ex.
## ====================
## ## without header and footer
## $ cat ex1.eruby
## <% def list_items(list) %>
## <% for item in list %>
## <li><%= item %></li>
## <% end %>
## <% end %>
##
## $ erubis -s ex1.eruby
## _buf = []; def list_items(list)
## ; for item in list
## ; _buf << '<li>'; _buf << ( item ).to_s; _buf << '</li>
## '; end
## ; end
## ;
## _buf.join
##
## ## with header and footer
## $ cat ex2.eruby
## <!--#header:
## def list_items(list)
## #-->
## <% for item in list %>
## <li><%= item %></li>
## <% end %>
## <!--#footer:
## end
## #-->
##
## $ erubis -s -c HeaderFooterEruby ex4.eruby
##
## def list_items(list)
## _buf = []; _buf << '
## '; for item in list
## ; _buf << '<li>'; _buf << ( item ).to_s; _buf << '</li>
## '; end
## ; _buf << '
## ';
## _buf.join
## end
##
## ====================
##
## this is language-independent.
##
module HeaderFooterEnhancer
def self.desc # :nodoc:
"allow header/footer in document (ex. '<!--#header: #-->')"
end
HEADER_FOOTER_PATTERN = /(.*?)(^[ \t]*)?<!--\#(\w+):(.*?)\#-->([ \t]*\r?\n)?/m
def add_text(src, text)
m = nil
text.scan(HEADER_FOOTER_PATTERN) do |txt, lspace, word, content, rspace|
m = Regexp.last_match
flag_trim = @trim && lspace && rspace
super(src, txt)
content = "#{lspace}#{content}#{rspace}" if flag_trim
super(src, lspace) if !flag_trim && lspace
instance_variable_set("@#{word}", content)
super(src, rspace) if !flag_trim && rspace
end
#rest = $' || text # ruby1.8
rest = m ? text[m.end(0)..-1] : text # ruby1.9
super(src, rest)
end
attr_accessor :header, :footer
def convert(input)
source = super
return @src = "#{@header}#{source}#{@footer}"
end
end
##
## delete indentation of HTML.
##
## this is language-independent.
##
module DeleteIndentEnhancer
def self.desc # :nodoc:
"delete indentation of HTML."
end
def convert_input(src, input)
input = input.gsub(/^[ \t]+</, '<')
super(src, input)
end
end
##
## convert "<h1><%=title%></h1>" into "_buf << %Q`<h1>#{title}</h1>`"
##
## this is only for Eruby.
##
module InterpolationEnhancer
def self.desc # :nodoc:
"convert '<p><%=text%></p>' into '_buf << %Q`<p>\#{text}</p>`'"
end
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, 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_to_str(str, text)
## * when '<%= %>', do nothing
## * when '<% %>' or '<%# %>', delete spaces iff only spaces are around '<% %>'
if ch == ?= # <%= %>
rspace = nil if tailch && !tailch.empty?
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 # ruby1.8
rest = pos == 0 ? input : input[pos..-1] # ruby1.9
_add_text_to_str(str, rest)
add_text(src, str)
end
def add_text(src, text)
return if !text || text.empty?
#src << " _buf << %Q`" << text << "`;"
if text[-1] == ?\n
text[-1] = "\\n"
src << " #{@bufvar} << %Q`#{text}`\n"
else
src << " #{@bufvar} << %Q`#{text}`;"
end
end
def _add_text_to_str(str, text)
return if !text || text.empty?
str << text.gsub(/[`\#\\]/, '\\\\\&')
end
def add_expr_escaped(str, code)
str << "\#{#{escaped_expr(code)}}"
end
def add_expr_literal(str, code)
str << "\#{#{code}}"
end
end
end
|