Class: ERB::Compiler::TrimScanner

Inherits:
Scanner show all
Defined in:
lib/framework/erb.rb

Overview

:nodoc:

Constant Summary collapse

ERB_STAG =
%w(<%= <%# <%)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Scanner

default_scanner=, make_scanner, regist_scanner

Constructor Details

#initialize(src, trim_mode, percent) ⇒ TrimScanner

Returns a new instance of TrimScanner.



307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/framework/erb.rb', line 307

def initialize(src, trim_mode, percent)
	super
	@trim_mode = trim_mode
	@percent = percent
	if @trim_mode == '>'
	  @scan_line = self.method(:trim_line1)
	elsif @trim_mode == '<>'
	  @scan_line = self.method(:trim_line2)
	elsif @trim_mode == '-'
	  @scan_line = self.method(:explicit_trim_line)
	else
	  @scan_line = self.method(:scan_line)
	end
end

Instance Attribute Details

#stagObject

Returns the value of attribute stag.



321
322
323
# File 'lib/framework/erb.rb', line 321

def stag
  @stag
end

Instance Method Details

#explicit_trim_line(line) ⇒ Object



393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
# File 'lib/framework/erb.rb', line 393

def explicit_trim_line(line)
  line.scan(/(.*?)(^[ \t]*<%\-|<%\-|<%%|%%>|<%=|<%#|<%|-%>\n|-%>|%>|\z)/m) do |tokens|
    tokens.each do |token|
      next if token.empty?
      if @stag.nil? && /[ \t]*<%-/ =~ token
        yield('<%')
      elsif @stag && token == "-%>\n"
        yield('%>')
        yield(:cr)
      elsif @stag && token == '-%>'
        yield('%>')
      else
        yield(token)
      end
    end
  end
end

#is_erb_stag?(s) ⇒ Boolean

Returns:

  • (Boolean)


412
413
414
# File 'lib/framework/erb.rb', line 412

def is_erb_stag?(s)
	ERB_STAG.member?(s)
end

#percent_line(line, &block) ⇒ Object



335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/framework/erb.rb', line 335

def percent_line(line, &block)
	if @stag || line[0] != ?%
	  return @scan_line.call(line, &block)
	end

	line[0] = ''
	if line[0] == ?%
	  @scan_line.call(line, &block)
	else
    yield(PercentLine.new(line.chomp))
	end
end

#scan(&block) ⇒ Object



323
324
325
326
327
328
329
330
331
332
333
# File 'lib/framework/erb.rb', line 323

def scan(&block)
	@stag = nil
	if @percent
	  @src.each_line do |line|
	    percent_line(line, &block)
	  end
	else
    @scan_line.call(@src, &block)
	end
	nil
end

#scan_line(line) ⇒ Object



348
349
350
351
352
353
354
355
# File 'lib/framework/erb.rb', line 348

def scan_line(line)
  line.scan(/(.*?)(<%%|%%>|<%=|<%#|<%|%>|\n|\z)/m) do |tokens|
    tokens.each do |token|
      next if token.empty?
      yield(token)
    end
	end
end

#trim_line1(line) ⇒ Object



357
358
359
360
361
362
363
364
365
366
367
368
369
# File 'lib/framework/erb.rb', line 357

def trim_line1(line)
  line.scan(/(.*?)(<%%|%%>|<%=|<%#|<%|%>\n|%>|\n|\z)/m) do |tokens|
    tokens.each do |token|
      next if token.empty?
      if token == "%>\n"
        yield('%>')
        yield(:cr)
      else
        yield(token)
      end
    end
	end
end

#trim_line2(line) ⇒ Object



371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# File 'lib/framework/erb.rb', line 371

def trim_line2(line)
	head = nil
  line.scan(/(.*?)(<%%|%%>|<%=|<%#|<%|%>\n|%>|\n|\z)/m) do |tokens|
    tokens.each do |token|
      next if token.empty?
      head = token unless head
      if token == "%>\n"
        yield('%>')
        if is_erb_stag?(head)
          yield(:cr)
        else
          yield("\n")
        end
        head = nil
      else
        yield(token)
        head = nil if token == "\n"
      end
    end
	end
end