Sha256: 1b2dc8dd1326ba596bf4dabf632a04fcb129f926ba93ba22ceaac1511de56bd9

Contents?: true

Size: 1.27 KB

Versions: 37

Compression:

Stored size: 1.27 KB

Contents

module Hamlit
  module Concerns
    module LineReader
      def reset_lines(lines)
        @lines = lines
        @current_lineno = -1
      end

      def current_line
        @lines[@current_lineno]
      end

      # Return nearest line ignoring empty lines.
      def next_line
        lineno = @current_lineno + 1
        while @lines[lineno] && empty_line?(@lines[lineno])
          lineno += 1
        end
        @lines[lineno]
      end

      def empty_line?(line)
        line =~ /\A *\Z/
      end

      def skip_lines
        while next_indent >= @current_indent
          @current_lineno += 1
        end
      end

      def read_lines
        lines = []
        while read_line?
          lines << @lines[@current_lineno + 1]
          @current_lineno += 1
        end
        trim_lines(lines)
      end

      private

      def trim_lines(lines)
        size = (lines.first || '').index(/[^\s]/) || 0
        lines.map { |line| line.gsub(/\A {#{size}}/, '') }
      end

      def read_line?
        return true if count_indent(next_line, strict: false) >= @current_indent

        line = @lines[@current_lineno + 1]
        return false unless line

        # NOTE: preserve filter also requires an empty line
        line.gsub(/ /, '').length == 0
      end
    end
  end
end

Version data entries

37 entries across 37 versions & 1 rubygems

Version Path
hamlit-1.5.2 lib/hamlit/concerns/line_reader.rb
hamlit-1.5.1 lib/hamlit/concerns/line_reader.rb
hamlit-1.5.0 lib/hamlit/concerns/line_reader.rb
hamlit-1.4.7 lib/hamlit/concerns/line_reader.rb
hamlit-1.4.6 lib/hamlit/concerns/line_reader.rb
hamlit-1.4.5 lib/hamlit/concerns/line_reader.rb
hamlit-1.4.3 lib/hamlit/concerns/line_reader.rb
hamlit-1.4.2 lib/hamlit/concerns/line_reader.rb
hamlit-1.4.1 lib/hamlit/concerns/line_reader.rb
hamlit-1.3.2 lib/hamlit/concerns/line_reader.rb
hamlit-1.3.1 lib/hamlit/concerns/line_reader.rb
hamlit-1.3.0 lib/hamlit/concerns/line_reader.rb
hamlit-1.2.1 lib/hamlit/concerns/line_reader.rb
hamlit-1.2.0 lib/hamlit/concerns/line_reader.rb
hamlit-1.1.1 lib/hamlit/concerns/line_reader.rb
hamlit-1.1.0 lib/hamlit/concerns/line_reader.rb
hamlit-1.0.0 lib/hamlit/concerns/line_reader.rb
hamlit-0.6.2 lib/hamlit/concerns/line_reader.rb
hamlit-0.6.1 lib/hamlit/concerns/line_reader.rb
hamlit-0.6.0 lib/hamlit/concerns/line_reader.rb