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, #syntax_error!

Instance Method Details

#count_indent(line) ⇒ Object



29
30
31
32
33
34
# File 'lib/hamlit/concerns/indentable.rb', line 29

def count_indent(line)
  return EOF unless line
  return 0 if indent_rule == 0

  line.match(/\A[ \t]+/).to_s.length / indent_rule
end

#has_block?Boolean

Returns:

  • (Boolean)


60
61
62
63
64
65
# File 'lib/hamlit/concerns/indentable.rb', line 60

def has_block?
  return false unless next_line
  return fetch_indent(next_line).length > 0 unless defined?(@indent_space)

  next_indent > @current_indent
end

#next_indentObject

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



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

def next_indent
  return 1 if !defined?(@indent_space) && fetch_indent(next_line).length > 0
  count_indent(next_line)
end

#reset_indentObject



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

def reset_indent
  @indent_logs    = []
  @current_indent = 0
end

#same_indent?(line) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
39
# File 'lib/hamlit/concerns/indentable.rb', line 36

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

#validate_indentation!(ast) ⇒ Object

Validate current line’s indentation



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/hamlit/concerns/indentable.rb', line 42

def validate_indentation!(ast)
  return true unless next_line

  indent = fetch_indent(next_line)
  if indent.include?(' ') && indent.include?("\t")
    syntax_error!("Indentation can't use both tabs and spaces.")
  end
  @indent_logs << indent

  if !defined?(@indent_space) && @indent_logs.last != ''
    @indent_space = @indent_logs.last
  end
  validate_indentation_consistency!(indent)
  reject_too_deep_indentation!

  next_indent < @current_indent
end

#with_indented(&block) ⇒ Object



22
23
24
25
26
27
# File 'lib/hamlit/concerns/indentable.rb', line 22

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