Class: StepsRecorderAnalyser

Inherits:
Object
  • Object
show all
Defined in:
lib/sra2019.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(s, debug: false, savepath: '/tmp', title: 'Untitled') ⇒ StepsRecorderAnalyser

Returns a new instance of StepsRecorderAnalyser.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/sra2019.rb', line 41

def initialize(s, debug: false, savepath: '/tmp', title: 'Untitled')

  @savepath, @title, @debug = savepath, title, debug
  
  raw_content, type = RXFHelper.read(s)
  
  content = if type == :file and File.extname(s) == '.zip' then
    Zip::ZipFile.new(s).instance_eval {read(to_a[0].name)}
  else
    raw_content
  end

  puts ('content: ' + content.inspect).debug if @debug
  
  @actions = parse_report content
  @all_steps = parse_steps  content  
  @doc = build @all_steps
  steps = @all_steps.select {|x| x[:user_comment]}
  @steps = steps.any? ? steps : tidy(@all_steps)
  
end

Instance Attribute Details

#start_timeObject (readonly)

Returns the value of attribute start_time.



37
38
39
# File 'lib/sra2019.rb', line 37

def start_time
  @start_time
end

#stepsObject

Returns the value of attribute steps.



38
39
40
# File 'lib/sra2019.rb', line 38

def steps
  @steps
end

#stop_timeObject (readonly)

Returns the value of attribute stop_time.



37
38
39
# File 'lib/sra2019.rb', line 37

def stop_time
  @stop_time
end

Instance Method Details

#import(s) ⇒ Object



63
64
65
66
67
68
# File 'lib/sra2019.rb', line 63

def import(s)
  
  @dx = Dynarex.new
  @dx.import s
  
end

#tidy(steps) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/sra2019.rb', line 70

def tidy(steps)

  verbose_level = 0

  steps.each do |x|

    x.desc.gsub!(/\s*\([^\)]+\)\s*/,'')
    x.desc.sub!(/ in "\w+"$/,'')
    
    if x.desc =~ /User left click/ and verbose_level == 0 then

      x.desc.sub!(/User left click/, 'Using the mouse, left click')
      verbose_level = 1

    elsif x.desc =~ /User left click/ and verbose_level == 1

      x.desc.sub!(/User left click/, 'Left click')
      verbose_level = 2

    elsif x.desc =~ /User left click/ and verbose_level == 2

      x.desc.sub!(/User left click/, 'Click')

    else
      verbose_level = 0
    end

  end
  
end

#to_dxObject

Returns a Dynarex object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/sra2019.rb', line 103

def to_dx()
  
  @dx = Dynarex.new 'instructions/instruction(step, imgsrc, description)'
  @dx.summary[:rawdoc_type] = 'rowx'
  
  @steps.each.with_index do |h, i|
    
    @dx.create step: i+1, imgsrc: "screenshot#{i+1}.jpg", 
        description: h[:user_comment] || h[:desc]
    
  end
  
  @dx
  
end

#to_html(filepath = File.join(@savepath, 'sra' + Time.now.to_i.to_s)) ⇒ Object

Writes the steps to an HTML file



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/sra2019.rb', line 129

def to_html(filepath=File.join(@savepath, 'sra' + Time.now.to_i.to_s))
  
  # save the image files to a file directory.
  # name the sub-directory using the date and time?

  imgpath = File.join(filepath, 'images')
  csspath = File.join(filepath, 'css')
  
  [filepath, imgpath, csspath].each {|x|  FileUtils.mkdir x}

  @steps.each.with_index do |x, i|
    
    File.open(File.join(imgpath, "screenshot#{i}.jpg" ), 'wb') \
      {|f| f.write(x[:screenshot]) }

  end
  

  rows = @steps.map.with_index do |x, i|

li = "
    li
      markdown:
        #{x[:user_comment]}
        
        ![](images/screenshot#{i}.jpg)
"         
  
  end

@sliml="html\nhead\n  title \#{@title}: \#{@steps.length} Steps (with Pictures)    \n  link rel='stylesheet' type='text/css' href='css/layout.css' media='screen,  projection, tv'\n  link rel='stylesheet' type='text/css' href='css/style.css' media='screen,  projection, tv'\n  link rel='stylesheet' type='text/css' href='css/print.css' media='print'\nbody\n  h1 \#{@title}\n\n  ol\n    \#{rows.join(\"\\n\").lstrip}\n"

  html = Rexle.new(Hlt.new(@sliml).to_html)\
      .root.element('html').xml pretty: true
  File.write File.join(filepath, 'index.html'), html

  %w(layout style print).each \
      {|x| File.write File.join(csspath, "%s.css" % x), ''}
  
  'saved'
  
end

#to_kbml(options = {}) ⇒ Object



119
120
121
122
123
124
# File 'lib/sra2019.rb', line 119

def to_kbml(options={})


  @doc.xml options

end

#to_pdf(pdf_file = File.join(@savepath, 'sra' + Time.now.to_i.to_s, @title.gsub(/ /,'-') + '.pdf')) ⇒ Object

Writes the steps to a PDF file



225
226
227
228
229
230
231
232
233
234
235
# File 'lib/sra2019.rb', line 225

def to_pdf(pdf_file=File.join(@savepath, 'sra' + Time.now.to_i.to_s, 
                            @title.gsub(/ /,'-') + '.pdf'))
  
  dir = File.dirname(pdf_file)
  html_file = File.join(dir, 'index.html')
  
  to_html(dir)
  pdf = WickedPdf.new.pdf_from_html_file(html_file)
  File.write pdf_file, pdf
  
end

#to_slimlObject



184
185
186
# File 'lib/sra2019.rb', line 184

def to_sliml()
  @sliml
end

#to_srtObject



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/sra2019.rb', line 188

def to_srt()

  lines = to_subtitles.strip.lines.map.with_index do |x, i|

    raw_times, subtitle = x.split(/ /,2)
    
    start_time, end_time = raw_times.split('-',2)
    times = [("%02d:%02d:%02d" % ([0, 0 ] + start_time.split(/\D/)\
                                  .map(&:to_i)).reverse.take(3).reverse), \
             '-->', \
            ("%02d:%02d:%02d" % ([0, 0 ] + end_time.split(/\D/).map(&:to_i))\
             .reverse.take(3).reverse)].join(' ')

    [i+1, times, subtitle].join("\n")

  end

  lines.join("\n")    
  
end

#to_subtitlesObject



209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/sra2019.rb', line 209

def to_subtitles()
  
  times = @all_steps.map(&:time).each_cons(2).map do |x|

    x.map do |sec|
      a = Subunit.new(units={minutes:60, hours:60}, seconds: sec).to_h.to_a
      a.map {|x|"%d%s" % [x[1], x[0][0]] }.join('')
    end.join('-')
    
  end
  times.zip(@all_steps.map(&:desc)).map {|x| x.join(' ')}.join("\n")
                        
end

#to_zipObject

Compresses the HTML file directory to a ZIP file



239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/sra2019.rb', line 239

def to_zip()
  
  project = 'sra' + Time.now.to_i.to_s
  newdir = File.join(@savepath, project)
  zipfile = newdir + '.zip'
  
  to_html(File.join(newdir))    
  #to_pdf(File.join(newdir, @title.gsub(/ /,'-') + '.pdf'))    

  Archive::Zip.archive(zipfile, newdir)
  
  zipfile
  
end