Module: Hamlit::Concerns::Indentable

Includes:
Error
Included in:
Parsers::Comment, Parsers::Filter, Parsers::Script, Parsers::Tag
Defined in:
lib/hamlit/concerns/indentable.rb

Instance Method Summary collapse

Methods included from Error

#assert_scan!, #copmile_error!, #syntax_error

Instance Method Details

#count_indent(line, strict: false) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/hamlit/concerns/indentable.rb', line 32

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

#count_width(line) ⇒ Object



42
43
44
45
# File 'lib/hamlit/concerns/indentable.rb', line 42

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

#next_indent ⇒ Object

Return nearest line's indent level since next line. This method ignores empty line. It returns -1 if next_line does not exist.



16
17
18
# File 'lib/hamlit/concerns/indentable.rb', line 16

def next_indent
  count_indent(next_line)
end

#next_width ⇒ Object



20
21
22
# File 'lib/hamlit/concerns/indentable.rb', line 20

def next_width
  count_width(next_line)
end

#reset_indent ⇒ Object



10
11
12
# File 'lib/hamlit/concerns/indentable.rb', line 10

def reset_indent
  @current_indent = 0
end

#same_indent?(line) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
50
# File 'lib/hamlit/concerns/indentable.rb', line 47

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

#with_indented(&block) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/hamlit/concerns/indentable.rb', line 24

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

  result
end