Sha256: c3a375489633b6f7741b4405875a169ed8c3d1429525aac786a4548f285898f0

Contents?: true

Size: 1.22 KB

Versions: 11

Compression:

Stored size: 1.22 KB

Contents

module FastHaml
  class LineParser
    attr_reader :lineno

    def initialize(template_str)
      @lines = template_str.each_line.map { |line| line.chomp.rstrip }
      @lineno = 0
    end

    def next_line
      line = move_next
      if is_multiline?(line)
        next_multiline(line)
      else
        line
      end
    end

    def has_next?
      @lineno < @lines.size
    end

    private

    MULTILINE_SUFFIX = ' |'

    # Regex to check for blocks with spaces around arguments. Not to be confused
    # with multiline script.
    # For example:
    #     foo.each do | bar |
    #       = bar
    #
    BLOCK_WITH_SPACES = /do\s*\|\s*[^\|]*\s+\|\z/o

    def is_multiline?(line)
      line = line.lstrip
      line.end_with?(MULTILINE_SUFFIX) && line !~ BLOCK_WITH_SPACES
    end

    def move_next
      @lines[@lineno].tap do
        @lineno += 1
      end
    end

    def move_back
      @lineno -= 1
    end

    def next_multiline(line)
      buf = [line[0, line.size-1]]
      while @lineno < @lines.size
        line = move_next

        if is_multiline?(line)
          line = line[0, line.size-1]
          buf << line.lstrip
        else
          move_back
          break
        end
      end
      buf.join
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
fast_haml-0.1.10 lib/fast_haml/line_parser.rb
fast_haml-0.1.9 lib/fast_haml/line_parser.rb
fast_haml-0.1.8 lib/fast_haml/line_parser.rb
fast_haml-0.1.7 lib/fast_haml/line_parser.rb
fast_haml-0.1.6 lib/fast_haml/line_parser.rb
fast_haml-0.1.5 lib/fast_haml/line_parser.rb
fast_haml-0.1.4 lib/fast_haml/line_parser.rb
fast_haml-0.1.3 lib/fast_haml/line_parser.rb
fast_haml-0.1.2 lib/fast_haml/line_parser.rb
fast_haml-0.1.1 lib/fast_haml/line_parser.rb
fast_haml-0.1.0 lib/fast_haml/line_parser.rb