Sha256: a52551c4fea14d14371514b3bc2a0a634a9543b594a181a42b5e97efae151aea

Contents?: true

Size: 1.22 KB

Versions: 7

Compression:

Stored size: 1.22 KB

Contents

module Faml
  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

7 entries across 7 versions & 1 rubygems

Version Path
faml-0.2.6 lib/faml/line_parser.rb
faml-0.2.5 lib/faml/line_parser.rb
faml-0.2.4 lib/faml/line_parser.rb
faml-0.2.3 lib/faml/line_parser.rb
faml-0.2.2 lib/faml/line_parser.rb
faml-0.2.1 lib/faml/line_parser.rb
faml-0.2.0 lib/faml/line_parser.rb