Sha256: fb3136bc740621b5c09f4812a5684c13ce219efce1e967cb44169e989a9481c8

Contents?: true

Size: 1.34 KB

Versions: 9

Compression:

Stored size: 1.34 KB

Contents

# frozen_string_literal: true

module Tocer
  # Writes table of contents to a Markdown document.
  class Writer
    def self.add start_index:, old_lines:, new_lines:
      computed_new_lines = start_index.zero? ? new_lines : new_lines + "\n"
      old_lines.insert start_index, *computed_new_lines
    end

    def self.remove start_index, finish_index, lines
      range = (start_index - 1)..finish_index
      lines.reject.with_index { |_, index| range.include? index }
    end

    def initialize file_path, label: "## Table of Contents", builder: Builder.new(label: label)
      @file_path = file_path
      @builder = builder
    end

    def write
      lines = File.readlines file_path
      body = builder.prependable?(lines) ? prepend(lines) : replace(lines)
      File.open(file_path, "w") { |file| file.write body }
    end

    private

    attr_reader :file_path, :builder

    def content lines
      builder.build lines
    end

    def replace lines
      start_index = builder.start_index lines
      finish_index = builder.finish_index lines
      klass = self.class

      klass.add(
        start_index: start_index,
        old_lines: klass.remove(start_index, finish_index, lines),
        new_lines: content(lines[finish_index, lines.length])
      ).join
    end

    def prepend lines
      content(lines) + "\n" + lines.join
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
tocer-8.2.1 lib/tocer/writer.rb
tocer-8.2.0 lib/tocer/writer.rb
tocer-8.1.0 lib/tocer/writer.rb
tocer-8.0.0 lib/tocer/writer.rb
tocer-7.2.0 lib/tocer/writer.rb
tocer-7.1.0 lib/tocer/writer.rb
tocer-7.0.2 lib/tocer/writer.rb
tocer-7.0.1 lib/tocer/writer.rb
tocer-7.0.0 lib/tocer/writer.rb