Class: ERB::Compiler::TrimScanner

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

Overview

:nodoc:

Constant Summary collapse

TrimSplitRegexp =
/(<%%)|(%%>)|(<%=)|(<%#)|(<%)|(%>\n)|(%>)|(\n)/
ERB_STAG =
%w(<%=

Constants inherited from Scanner

Scanner::SplitRegexp

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.



288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/erb.rb', line 288

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



302
303
304
# File 'lib/erb.rb', line 302

def stag
  @stag
end

Instance Method Details

#explicit_trim_line(line) ⇒ Object



368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
# File 'lib/erb.rb', line 368

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)


387
388
389
# File 'lib/erb.rb', line 387

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

#percent_line(line, &block) ⇒ Object



318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/erb.rb', line 318

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



304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/erb.rb', line 304

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

#scan_line(line) ⇒ Object



331
332
333
334
335
336
# File 'lib/erb.rb', line 331

def scan_line(line)
	line.split(SplitRegexp).each do |token|
	  next if token.empty?
	  yield(token)
	end
end

#trim_line1(line) ⇒ Object



338
339
340
341
342
343
344
345
346
347
348
# File 'lib/erb.rb', line 338

def trim_line1(line)
	line.split(TrimSplitRegexp).each do |token|
	  next if token.empty?
	  if token == "%>\n"
	    yield('%>')
	    yield(:cr)
	    break
	  end
	  yield(token)
	end
end

#trim_line2(line) ⇒ Object



350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# File 'lib/erb.rb', line 350

def trim_line2(line)
	head = nil
	line.split(TrimSplitRegexp).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
	    break
	  end
	  yield(token)
	end
end