Sha256: 8b3fe786cf34545be15447cf2a29ec0cdb1fa2602531d167c35466043bbcc767

Contents?: true

Size: 1.08 KB

Versions: 6

Compression:

Stored size: 1.08 KB

Contents

require 'hamlit/concerns/error'

module Hamlit
  EOF = -1

  module Concerns
    module Indentable
      include Concerns::Error

      def reset_indent
        @current_indent = 0
      end

      # Return nearest line's indent level since next line. This method ignores
      # empty line. It returns -1 if next_line does not exist.
      def next_indent
        count_indent(next_line)
      end

      def next_width
        count_width(next_line)
      end

      def with_indented(&block)
        @current_indent += 1
        result = block.call
        @current_indent -= 1

        result
      end

      def count_indent(line, strict: false)
        return EOF unless line
        width = count_width(line)

        return (width + 1) / 2 unless strict
        compile_error!('Expected to count even-width indent') if width.odd?

        width / 2
      end

      def count_width(line)
        return EOF unless line
        line[/\A +/].to_s.length
      end

      def same_indent?(line)
        return false unless line
        count_indent(line) == @current_indent
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
hamlit-0.3.3 lib/hamlit/concerns/indentable.rb
hamlit-0.3.2 lib/hamlit/concerns/indentable.rb
hamlit-0.3.1 lib/hamlit/concerns/indentable.rb
hamlit-0.3.0 lib/hamlit/concerns/indentable.rb
hamlit-0.2.0 lib/hamlit/concerns/indentable.rb
hamlit-0.1.3 lib/hamlit/concerns/indentable.rb