Class: Stitcher

Inherits:
HtmlGenerator show all
Defined in:
lib/jirametrics/stitcher.rb

Defined Under Namespace

Classes: StitchContent

Instance Attribute Summary collapse

Attributes inherited from HtmlGenerator

#file_system, #settings

Instance Method Summary collapse

Methods inherited from HtmlGenerator

#create_html, #load_css

Constructor Details

#initialize(file_system:) ⇒ Stitcher

Returns a new instance of Stitcher.



19
20
21
22
23
24
# File 'lib/jirametrics/stitcher.rb', line 19

def initialize file_system:
  super()
  self.file_system = file_system
  @all_stitches = []
  @loaded_files = []
end

Instance Attribute Details

#all_stitchesObject (readonly)

Returns the value of attribute all_stitches.



17
18
19
# File 'lib/jirametrics/stitcher.rb', line 17

def all_stitches
  @all_stitches
end

#loaded_filesObject (readonly)

Returns the value of attribute loaded_files.



17
18
19
# File 'lib/jirametrics/stitcher.rb', line 17

def loaded_files
  @loaded_files
end

Instance Method Details

#grab_by_title(title, from_file:, type: 'chart') ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/jirametrics/stitcher.rb', line 42

def grab_by_title title, from_file:, type: 'chart'
  parse_file from_file
  stitch_content = @all_stitches.find { |s| s.file == from_file && s.title == title && s.type == type }
  return stitch_content.content if stitch_content

  file_system.error "Unable to find content in file #{from_file.inspect} matching title: #{title.inspect}"
  ''
end

#make_output_filename(input_filename) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/jirametrics/stitcher.rb', line 34

def make_output_filename input_filename
  if /^(.+)\.erb$/ =~ input_filename
    "#{$1}.html"
  else
    "#{input_filename}.html"
  end
end

#parse_file(filename) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/jirametrics/stitcher.rb', line 51

def parse_file filename
  return false if @loaded_files.include? filename

  # To match: <!-- seam-start | chart78 | GithubPrScatterplot | PR Scatterplot | chart -->
  regex = /^<!-- seam-(?<seam>start|end) \| (?<id>[^|]+) \| (?<clazz>[^|]+) \| (?<title>[^|]+) \| (?<type>[^|]+) -->$/
  content = nil
  file_system.load(filename).lines do |line|
    matches = line.match(regex)
    if matches
      if matches[:seam] == 'start'
        content = +''
      else
        if content.nil? || content.strip.empty?
          file_system.warning "Seam found with no content in #{filename.inspect}: " \
            "id=#{matches[:id].strip.inspect}, class=#{matches[:clazz].strip.inspect}, " \
            "title=#{matches[:title].strip.inspect}"
        end
        @all_stitches << Stitcher::StitchContent.new(
          file: filename, title: matches[:title], type: matches[:type], content: content
        )
        content = nil
      end
    elsif content
      content << line
    end
  end

  @loaded_files << filename
  true
end

#run(stitch_file:) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/jirametrics/stitcher.rb', line 26

def run stitch_file:
  output_filename = make_output_filename stitch_file
  file_system.log "Creating file #{output_filename.inspect}", also_write_to_stderr: true
  erb = ERB.new file_system.load(stitch_file)
  @sections = [[erb.result(binding), :body]]
  create_html output_filename: output_filename, settings: {}
end