Class: HamdownCore::IndentTracker

Inherits:
Object
  • Object
show all
Defined in:
lib/hamdown_core/indent_tracker.rb

Defined Under Namespace

Classes: HardTabNotAllowed, InconsistentIndent, IndentMismatch

Instance Method Summary collapse

Constructor Details

#initialize(on_enter: nil, on_leave: nil) ⇒ IndentTracker

Returns a new instance of IndentTracker.



32
33
34
35
36
37
# File 'lib/hamdown_core/indent_tracker.rb', line 32

def initialize(on_enter: nil, on_leave: nil)
  @indent_levels = [0]
  @on_enter = on_enter || lambda { |_level, _text| }
  @on_leave = on_leave || lambda { |_level, _text| }
  @comment_level = nil
end

Instance Method Details

#check_indent_level!(lineno) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/hamdown_core/indent_tracker.rb', line 69

def check_indent_level!(lineno)
  if @indent_levels.size >= 3
    previous_size = @indent_levels[-2] - @indent_levels[-3]
    current_size = @indent_levels[-1] - @indent_levels[-2]
    if previous_size != current_size
      raise InconsistentIndent.new(previous_size, current_size, lineno)
    end
  end
end

#current_levelObject



61
62
63
# File 'lib/hamdown_core/indent_tracker.rb', line 61

def current_level
  @indent_levels.last
end

#enter_comment!Object



65
66
67
# File 'lib/hamdown_core/indent_tracker.rb', line 65

def enter_comment!
  @comment_level = @indent_levels[-2]
end

#finishObject



57
58
59
# File 'lib/hamdown_core/indent_tracker.rb', line 57

def finish
  indent_leave(0, '', -1)
end

#process(line, lineno) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/hamdown_core/indent_tracker.rb', line 39

def process(line, lineno)
  if line.include?("\t")
    raise HardTabNotAllowed.new(lineno)
  end
  indent, text = split(line)
  indent_level = indent.size

  unless text.empty?
    track(indent_level, text, lineno)
  end
  [text, indent]
end

#split(line) ⇒ Object



52
53
54
55
# File 'lib/hamdown_core/indent_tracker.rb', line 52

def split(line)
  m = line.match(/\A( *)(.*)\z/)
  [m[1], m[2]]
end