Class: Snaptoken::Representations::Litdiff

Inherits:
BaseRepresentation show all
Defined in:
lib/snaptoken/representations/litdiff.rb

Instance Method Summary collapse

Methods inherited from BaseRepresentation

#exists?, #initialize, #modified?

Constructor Details

This class inherits a constructor from Snaptoken::Representations::BaseRepresentation

Instance Method Details

#load!(options = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/snaptoken/representations/litdiff.rb', line 26

def load!(options = {})
  step_num = 1
  @tutorial.clear
  Dir[File.join(path, "*.litdiff")].sort_by { |f| File.basename(f).to_i }.each do |diff_path|
    filename = File.basename(diff_path).sub(/\.litdiff$/, "").sub(/^\d+\./, "")
    page = Snaptoken::Page.new(filename)
    File.open(diff_path, "r") do |f|
      cur_text = ""
      cur_diff = nil
      cur_summary = nil
      while line = f.gets
        if line.start_with? "~~~"
          cur_summary = (line[3..-1] || "").strip
          cur_diff = ""
        elsif cur_diff
          if line.chomp.empty?
            step_diffs = Snaptoken::Diff.parse(cur_diff)
            page << Snaptoken::Step.new(step_num, cur_summary, cur_text.strip, step_diffs)

            yield step_num if block_given?
            step_num += 1

            cur_text = ""
            cur_summary = nil
            cur_diff = nil
          else
            cur_diff << line.sub(/^\|/, " ")
          end
        else
          cur_text << line
        end
      end
      if cur_diff
        step_diffs = Snaptoken::Diff.parse(cur_diff)
        page << Snaptoken::Step.new(step_num, cur_summary, cur_text.strip, step_diffs)
      elsif !cur_text.strip.empty?
        page.footer_text = cur_text.strip
      end
    end
    @tutorial << page
  end
  @tutorial
end

#pathObject



70
71
72
# File 'lib/snaptoken/representations/litdiff.rb', line 70

def path
  File.join(@tutorial.config[:path], "doc")
end

#save!(options = {}) ⇒ Object



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

def save!(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.summary}\n"
      output << step.to_patch(unchanged_char: "|") << "\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