Module: Mdtoc::Writer

Extended by:
T::Sig
Defined in:
lib/mdtoc/writer.rb

Constant Summary collapse

COMMENT_BEGIN =
'<!-- mdtoc -->'
COMMENT_END =
'<!-- mdtoc-end -->'

Class Method Summary collapse

Class Method Details

.write(toc, path, append, create) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/mdtoc/writer.rb', line 16

def write(toc, path, append, create)
  validate_path(path, create)
  new_content = content(toc, path, append)

  # Write to a temporary file and rename it to the target path to ensure atomic writing.
  temp = Tempfile.new(File.basename(path), File.dirname(path))
  begin
    temp.write(new_content)
    temp.close
    FileUtils.mv(temp.path, path)
  ensure
    temp.close!
  end
end