Class: Tocer::Writer

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

Overview

Writes table of contents to a Markdown document.

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Writer.



6
7
8
9
10
11
12
# File 'lib/tocer/writer.rb', line 6

def initialize file_path, label: "# Table of Contents", builder: Builder, comment_block: Elements::CommentBlock
  @file_path = file_path
  @file_lines = File.open(file_path).to_a
  @label = label
  @builder = builder
  setup_indexes comment_block.new, @file_lines
end

Instance Method Details

#writeObject



14
15
16
17
# File 'lib/tocer/writer.rb', line 14

def write
  body = start_index ? replace_toc : prepend_toc
  File.open(file_path, "w") { |file| file.write body }
end