Class: Mdtoc::Markdown::Parser

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/mdtoc/markdown/parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(depth, url, generator: FragmentGenerator::GitHub.new) ⇒ Parser

Returns a new instance of Parser.



20
21
22
23
24
25
26
# File 'lib/mdtoc/markdown/parser.rb', line 20

def initialize(depth, url, generator: FragmentGenerator::GitHub.new)
  @depth = depth
  @url = url
  @generator = generator
  @in_code_block = T.let(false, T::Boolean)
  @in_html_comment = T.let(false, T::Boolean)
end

Instance Method Details

#headers(lines) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/mdtoc/markdown/parser.rb', line 29

def headers(lines)
  @in_code_block = false
  @in_html_comment = false

  headers = T.let([], T::Array[Header])
  prev_line = T.let(nil, T.nilable(String))

  lines.each do |line|
    stripped = line.strip

    if skip_line?(line, stripped)
      prev_line = line
      next
    end

    if line.start_with?('#')
      headers << header(line)
    elsif (h = process_setext_header(stripped, prev_line))
      headers << h
    end

    prev_line = line
  end

  headers
end