Class: Leg::Representations::Litdiff

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

Instance Method Summary collapse

Methods inherited from BaseRepresentation

#exists?, #initialize, #modified?

Constructor Details

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

Instance Method Details

#load!(options = {}) ⇒ Object



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
69
70
# File 'lib/leg/representations/litdiff.rb', line 28

def load!(options = {})
  step_num = 1
  tutorial = Leg::Tutorial.new(@config)
  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 = Leg::Page.new(filename)
    File.open(diff_path, "r") do |f|
      cur_text = ""
      cur_diff = nil
      cur_summary = nil
      while line = f.gets
        if line =~ /^~~~\s*(\d+\.)?(.+)$/
          cur_summary = $2.strip
          cur_diff = ""
        elsif cur_diff
          if line.chomp.empty?
            step_diffs = Leg::Diff.parse(cur_diff)
            page << Leg::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 = Leg::Diff.parse(cur_diff)
        page << Leg::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



72
73
74
# File 'lib/leg/representations/litdiff.rb', line 72

def path
  File.join(@config.path, "doc")
end

#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