module Mdtoc::Writer

Constants

COMMENT_BEGIN
COMMENT_END

Public Class Methods

write(toc, path, append, create) click to toggle source
# File lib/mdtoc/writer.rb, line 13
def write(toc, path, append, create)
  validate_path(path, create)
  new_content = content(toc, path, append)
  File.open(path, 'w') do |f|
    f.write(new_content)
  end
end

Private Class Methods

content(toc, path, append) click to toggle source
# File lib/mdtoc/writer.rb, line 24
def content(toc, path, append)
  toc = "#{COMMENT_BEGIN}\n#{toc}\n#{COMMENT_END}"

  begin
    f = File.open(path)
  rescue
    # If File.open failed because the file didn't exist, then we know that --create
    # was specified due to the validation in validate_path.
    return "#{toc}\n"
  end
  begin
    old_content = T.must(f.read)
  ensure
    f.close
  end

  if Regexp.new(Regexp.escape(COMMENT_BEGIN), Regexp::IGNORECASE).match?(old_content)
    return old_content.gsub(
      /#{Regexp.escape(COMMENT_BEGIN)}(.*#{Regexp.escape(COMMENT_END)})?/im, toc
    )
  elsif append
    return "#{old_content}\n#{toc}\n"
  end

  warn("Could not update #{path}, because the target HTML tag \"#{COMMENT_BEGIN}\" was not found")
  exit(1)
end
validate_path(path, create) click to toggle source
# File lib/mdtoc/writer.rb, line 53
def validate_path(path, create)
  if File.exist?(path)
    unless File.file?(path)
      warn("--output PATH \"#{path}\" is not a regular file")
      exit
    end
  elsif !create
    warn("--output PATH \"#{path}\" does not exist. Specify --create to create it.")
    exit
  end
end