Class: Tocer::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/tocer/builder.rb

Overview

Builds a table of contents for a Markdown document.

Constant Summary collapse

CODE_BLOCK_PUNCTUATION =
"```"

Instance Method Summary collapse

Constructor Details

#initialize(lines, label: "# Table of Contents", comment_block: Elements::CommentBlock) ⇒ Builder



8
9
10
11
12
13
14
# File 'lib/tocer/builder.rb', line 8

def initialize lines, label: "# Table of Contents", comment_block: Elements::CommentBlock
  @lines = lines
  @label = label
  @comment_block = comment_block.new
  @url_count = Hash.new { |hash, key| hash[key] = 0 }
  @code_block = false
end

Instance Method Details

#buildObject



23
24
25
26
27
28
29
30
31
32
# File 'lib/tocer/builder.rb', line 23

def build
  return "" if headers.empty?

  [
    "#{comment_block.start}\n\n",
    "#{label}\n\n",
    headers_as_links.join("\n"),
    "\n\n#{comment_block.finish}\n\n"
  ].join
end

#headersObject



16
17
18
19
20
21
# File 'lib/tocer/builder.rb', line 16

def headers
  lines.select do |line|
    toggle_code_block line
    line.start_with?(Parsers::Header::PUNCTUATION) && !code_block
  end
end