ERB::Compiler (Class)

In: libraries/erb.rb
Parent: Object

Methods

Attributes

percent  [R] 
post_cmd  [RW] 
pre_cmd  [RW] 
put_cmd  [RW] 
trim_mode  [R] 

Public Class methods

[Source]

     # File libraries/erb.rb, line 391
391:     def initialize(trim_mode)
392:       @percent, @trim_mode = prepare_trim_mode(trim_mode)
393:       @put_cmd = 'print'
394:       @pre_cmd = []
395:       @post_cmd = []
396:     end

Public Instance methods

[Source]

     # File libraries/erb.rb, line 303
303:     def compile(s)
304:       out = make_buffer
305: 
306:       content = ''
307:       scanner = make_scanner(s)
308:       scanner.scan do |token|
309:         if scanner.stag.nil?
310:           case token
311:           when PercentLine
312:             out.push("#{@put_cmd} #{content.dump}") if content.size > 0
313:             content = ''
314:             out.push(token.to_s)
315:             out.cr
316:           when :cr
317:             out.cr
318:           when '<%', '<%=', '<%#'
319:             scanner.stag = token
320:             out.push("#{@put_cmd} #{content.dump}") if content.size > 0
321:             content = ''
322:           when "\n"
323:             content << "\n"
324:             out.push("#{@put_cmd} #{content.dump}")
325:             out.cr
326:             content = ''
327:           when '<%%'
328:             content << '<%'
329:           else
330:             content << token
331:           end
332:         else
333:           case token
334:           when '%>'
335:             case scanner.stag
336:             when '<%'
337:               if content[-1] == ?\n
338:                 content.chop!
339:                 out.push(content)
340:                 out.cr
341:               else
342:                 out.push(content)
343:               end
344:             when '<%='
345:               out.push("#{@put_cmd}((#{content}).to_s)")
346:             when '<%#'
347:               # out.push("# #{content.dump}")

348:             end
349:             scanner.stag = nil
350:             content = ''
351:           when '%%>'
352:             content << '%>'
353:           else
354:             content << token
355:           end
356:         end
357:       end
358:       out.push("#{@put_cmd} #{content.dump}") if content.size > 0
359:       out.close
360:       out.script
361:     end

[Source]

     # File libraries/erb.rb, line 295
295:     def make_buffer
296:       if RUBY_VERSION > '1.8'
297:         Buffer.new(self)
298:       else
299:         Buffer16.new(self)
300:       end
301:     end

[Source]

     # File libraries/erb.rb, line 387
387:     def make_scanner(src)
388:       Scanner.make_scanner(src, @trim_mode, @percent)
389:     end

[Source]

     # File libraries/erb.rb, line 363
363:     def prepare_trim_mode(mode)
364:       case mode
365:       when 1
366:         return [false, '>']
367:       when 2
368:         return [false, '<>']
369:       when 0
370:         return [false, nil]
371:       when String
372:         perc = mode.include?('%')
373:         if mode.include?('-')
374:           return [perc, '-']
375:         elsif mode.include?('<>')
376:           return [perc, '<>']
377:         elsif mode.include?('>')
378:           return [perc, '>']
379:         else
380:           [perc, nil]
381:         end
382:       else
383:         return [false, nil]
384:       end
385:     end

[Validate]