Method: Leg::Representations::Litdiff#save!

Defined in:
lib/leg/representations/litdiff.rb

#save!(tutorial, options = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/leg/representations/litdiff.rb', line 4

def save!(tutorial, options = {})
  FileUtils.mkdir_p(path)
  FileUtils.rm_rf(File.join(path, "."), secure: true)

  step_num = 1
  tutorial.pages.each.with_index do |page, page_idx|
    output = ""
    page.steps.each do |step|
      output << step.text << "\n\n" unless step.text.empty?
      output << "~~~ #{step_num}. #{step.summary}\n"
      output << step.to_patch(unchanged_char: "|", strip_git_lines: true) << "\n"

      yield step_num if block_given?
      step_num += 1
    end
    output << page.footer_text << "\n" if page.footer_text

    filename = page.filename + ".litdiff"
    filename = "%02d.%s" % [page_idx + 1, filename] if tutorial.pages.length > 1

    File.write(File.join(path, filename), output)
  end
end