Class: BRB::Parser

Inherits:
ActionView::Template::Handlers::ERB::Erubi
  • Object
show all
Defined in:
lib/brb.rb

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ Parser



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
# File 'lib/brb.rb', line 62

def initialize(input, ...)
  frontmatter = $1 if input.sub! /\A(.*?)~~~\n/m, ""
  backmatter  = $1 if input.sub! /~~~\n(.*?)\z/m, ""

  @scanner = StringScanner.new(input)
  input    = +""
  @mode    = :start

  if @mode == :start
    if scan = @scanner.scan_until(/(?=\\)/)
      input << scan
      @scanner.skip(/\\/)
      @mode = :open
    else
      input << @scanner.rest
      @scanner.terminate
    end
  else
    case token = @scanner.scan(/#|=|#{Sigil.names.join("\\b|")}\b/)
    when "#"          then @scanner.scan_until(/\n/)
    when *Sigil.names then input << Sigil.replace(@scanner, token)
    when "="          then input << "<%=" << @scanner.scan_until(/(?=<\/|\r?\n)/) << " %>"
    else
      @scanner.scan_until(/(?=\r?\n)/)&.then { input << "<% " << _1 << " %>" }
    end

    @mode = :start
  end until @scanner.eos?

  super
end