Class: Gitlab::Ci::Ansi2html::Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/ci/ansi2html.rb

Constant Summary collapse

STATE_PARAMS =
[:offset, :n_open_tags, :fg_color, :bg_color, :style_mask, :sections, :lineno_in_section].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#bg_colorObject

Returns the value of attribute bg_color


234
235
236
# File 'lib/gitlab/ci/ansi2html.rb', line 234

def bg_color
  @bg_color
end

#fg_colorObject

Returns the value of attribute fg_color


234
235
236
# File 'lib/gitlab/ci/ansi2html.rb', line 234

def fg_color
  @fg_color
end

#lineno_in_sectionObject

Returns the value of attribute lineno_in_section


234
235
236
# File 'lib/gitlab/ci/ansi2html.rb', line 234

def lineno_in_section
  @lineno_in_section
end

#n_open_tagsObject

Returns the value of attribute n_open_tags


234
235
236
# File 'lib/gitlab/ci/ansi2html.rb', line 234

def n_open_tags
  @n_open_tags
end

#offsetObject

Returns the value of attribute offset


234
235
236
# File 'lib/gitlab/ci/ansi2html.rb', line 234

def offset
  @offset
end

#sectionsObject

Returns the value of attribute sections


234
235
236
# File 'lib/gitlab/ci/ansi2html.rb', line 234

def sections
  @sections
end

#style_maskObject

Returns the value of attribute style_mask


234
235
236
# File 'lib/gitlab/ci/ansi2html.rb', line 234

def style_mask
  @style_mask
end

Instance Method Details

#close_open_tagsObject


430
431
432
433
434
435
# File 'lib/gitlab/ci/ansi2html.rb', line 430

def close_open_tags
  while @n_open_tags > 0
    @out << %{</span>}
    @n_open_tags -= 1
  end
end

#convert(stream, new_state) ⇒ Object


238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/gitlab/ci/ansi2html.rb', line 238

def convert(stream, new_state)
  reset_state
  restore_state(new_state, stream) if new_state.present?

  append = false
  truncated = false

  cur_offset = stream.tell
  if cur_offset > @offset
    @offset = cur_offset
    truncated = true
  else
    stream.seek(@offset)
    append = @offset > 0
  end

  start_offset = @offset

  stream.each_line do |line|
    s = StringScanner.new(line)

    until s.eos?

      if s.scan(Gitlab::Regex.build_trace_section_regex)
        handle_section(s)
      elsif s.scan(/\e([@-_])(.*?)([@-~])/)
        handle_sequence(s)
      elsif s.scan(/\e(([@-_])(.*?)?)?$/)
        break
      elsif s.scan(/</)
        write_in_tag '&lt;'
      elsif s.scan(/\r?\n/)
        handle_new_line
      else
        write_in_tag s.scan(/./m)
      end

      @offset += s.matched_size
    end
  end

  close_open_tags

  # TODO: replace OpenStruct with a better type
  # https://gitlab.com/gitlab-org/gitlab/issues/34305
  OpenStruct.new(
    html: @out.force_encoding(Encoding.default_external),
    state: state,
    append: append,
    truncated: truncated,
    offset: start_offset,
    size: stream.tell - start_offset,
    total: stream.size
  )
end

#data_section_namesObject


339
340
341
# File 'lib/gitlab/ci/ansi2html.rb', line 339

def data_section_names
  @sections.join(" ")
end

#disable(flag) ⇒ Object


474
475
476
# File 'lib/gitlab/ci/ansi2html.rb', line 474

def disable(flag)
  @style_mask &= ~flag
end

#enable(flag) ⇒ Object


470
471
472
# File 'lib/gitlab/ci/ansi2html.rb', line 470

def enable(flag)
  @style_mask |= flag
end

#ensure_open_new_tagObject


383
384
385
# File 'lib/gitlab/ci/ansi2html.rb', line 383

def ensure_open_new_tag
  open_new_tag if @n_open_tags == 0
end

#evaluate_command_stack(stack) ⇒ Object


363
364
365
366
367
368
369
370
371
# File 'lib/gitlab/ci/ansi2html.rb', line 363

def evaluate_command_stack(stack)
  return unless command = stack.shift

  if self.respond_to?("on_#{command}", true)
    self.__send__("on_#{command}", stack) # rubocop:disable GitlabSecurity/PublicSend
  end

  evaluate_command_stack(stack)
end

#get_color_class(segments) ⇒ Object


517
518
519
# File 'lib/gitlab/ci/ansi2html.rb', line 517

def get_color_class(segments)
  [segments].flatten.compact.join('-')
end

#get_term_color_class(color_index, prefix) ⇒ Object


486
487
488
489
490
491
# File 'lib/gitlab/ci/ansi2html.rb', line 486

def get_term_color_class(color_index, prefix)
  color_name = COLOR[color_index]
  return if color_name.nil?

  get_color_class(["term", prefix, color_name])
end

#get_xterm_color_class(command_stack, prefix) ⇒ Object


503
504
505
506
507
508
509
510
511
512
513
514
515
# File 'lib/gitlab/ci/ansi2html.rb', line 503

def get_xterm_color_class(command_stack, prefix)
  # the 38 and 48 commands have to be followed by "5" and the color index
  return unless command_stack.length >= 2
  return unless command_stack[0] == "5"

  command_stack.shift # ignore the "5" command
  color_index = command_stack.shift.to_i

  return unless color_index >= 0
  return unless color_index <= 255

  get_color_class(["xterm", prefix, color_index])
end

#handle_new_lineObject


298
299
300
301
302
303
# File 'lib/gitlab/ci/ansi2html.rb', line 298

def handle_new_line
  write_in_tag %{<br/>}

  close_open_tags if @sections.any? && @lineno_in_section == 0
  @lineno_in_section += 1
end

#handle_section(scanner) ⇒ Object


305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/gitlab/ci/ansi2html.rb', line 305

def handle_section(scanner)
  action = scanner[1]
  timestamp = scanner[2]
  section = scanner[3]

  normalized_section = section_to_class_name(section)

  if action == "start"
    handle_section_start(normalized_section, timestamp)
  elsif action == "end"
    handle_section_end(normalized_section, timestamp)
  end
end

#handle_section_end(section, timestamp) ⇒ Object


327
328
329
330
331
332
333
334
335
336
337
# File 'lib/gitlab/ci/ansi2html.rb', line 327

def handle_section_end(section, timestamp)
  return unless @sections.include?(section)

  # close all sections up to section
  until @sections.empty?
    write_raw %{<div class="section-end" data-section="#{data_section_names}"></div>}

    last_section = @sections.pop
    break if section == last_section
  end
end

#handle_section_start(section, timestamp) ⇒ Object


319
320
321
322
323
324
325
# File 'lib/gitlab/ci/ansi2html.rb', line 319

def handle_section_start(section, timestamp)
  return if @sections.include?(section)

  @sections << section
  write_raw %{<div class="section-start" data-timestamp="#{timestamp}" data-section="#{data_section_names}" role="button"></div>}
  @lineno_in_section = 0
end

#handle_sequence(scanner) ⇒ Object


343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/gitlab/ci/ansi2html.rb', line 343

def handle_sequence(scanner)
  indicator = scanner[1]
  commands = scanner[2].split ';'
  terminator = scanner[3]

  # We are only interested in color and text style changes - triggered by
  # sequences starting with '\e[' and ending with 'm'. Any other control
  # sequence gets stripped (including stuff like "delete last line")
  return unless indicator == '[' && terminator == 'm'

  close_open_tags

  if commands.empty?
    reset
    return
  end

  evaluate_command_stack(commands)
end

#on_0(_) ⇒ Object


34
35
36
# File 'lib/gitlab/ci/ansi2html.rb', line 34

def on_0(_)
  reset
end

#on_1(_) ⇒ Object


38
39
40
# File 'lib/gitlab/ci/ansi2html.rb', line 38

def on_1(_)
  enable(STYLE_SWITCHES[:bold])
end

#on_100(_) ⇒ Object


198
199
200
# File 'lib/gitlab/ci/ansi2html.rb', line 198

def on_100(_)
  set_bg_color(0, 'l')
end

#on_101(_) ⇒ Object


202
203
204
# File 'lib/gitlab/ci/ansi2html.rb', line 202

def on_101(_)
  set_bg_color(1, 'l')
end

#on_102(_) ⇒ Object


206
207
208
# File 'lib/gitlab/ci/ansi2html.rb', line 206

def on_102(_)
  set_bg_color(2, 'l')
end

#on_103(_) ⇒ Object


210
211
212
# File 'lib/gitlab/ci/ansi2html.rb', line 210

def on_103(_)
  set_bg_color(3, 'l')
end

#on_104(_) ⇒ Object


214
215
216
# File 'lib/gitlab/ci/ansi2html.rb', line 214

def on_104(_)
  set_bg_color(4, 'l')
end

#on_105(_) ⇒ Object


218
219
220
# File 'lib/gitlab/ci/ansi2html.rb', line 218

def on_105(_)
  set_bg_color(5, 'l')
end

#on_106(_) ⇒ Object


222
223
224
# File 'lib/gitlab/ci/ansi2html.rb', line 222

def on_106(_)
  set_bg_color(6, 'l')
end

#on_107(_) ⇒ Object


226
227
228
# File 'lib/gitlab/ci/ansi2html.rb', line 226

def on_107(_)
  set_bg_color(7, 'l')
end

#on_109(_) ⇒ Object


230
231
232
# File 'lib/gitlab/ci/ansi2html.rb', line 230

def on_109(_)
  set_bg_color(9, 'l')
end

#on_21(_) ⇒ Object


58
59
60
# File 'lib/gitlab/ci/ansi2html.rb', line 58

def on_21(_)
  disable(STYLE_SWITCHES[:bold])
end

#on_22(_) ⇒ Object


62
63
64
# File 'lib/gitlab/ci/ansi2html.rb', line 62

def on_22(_)
  disable(STYLE_SWITCHES[:bold])
end

#on_23(_) ⇒ Object


66
67
68
# File 'lib/gitlab/ci/ansi2html.rb', line 66

def on_23(_)
  disable(STYLE_SWITCHES[:italic])
end

#on_24(_) ⇒ Object


70
71
72
# File 'lib/gitlab/ci/ansi2html.rb', line 70

def on_24(_)
  disable(STYLE_SWITCHES[:underline])
end

#on_28(_) ⇒ Object


74
75
76
# File 'lib/gitlab/ci/ansi2html.rb', line 74

def on_28(_)
  disable(STYLE_SWITCHES[:conceal])
end

#on_29(_) ⇒ Object


78
79
80
# File 'lib/gitlab/ci/ansi2html.rb', line 78

def on_29(_)
  disable(STYLE_SWITCHES[:cross])
end

#on_3(_) ⇒ Object


42
43
44
# File 'lib/gitlab/ci/ansi2html.rb', line 42

def on_3(_)
  enable(STYLE_SWITCHES[:italic])
end

#on_30(_) ⇒ Object


82
83
84
# File 'lib/gitlab/ci/ansi2html.rb', line 82

def on_30(_)
  set_fg_color(0)
end

#on_31(_) ⇒ Object


86
87
88
# File 'lib/gitlab/ci/ansi2html.rb', line 86

def on_31(_)
  set_fg_color(1)
end

#on_32(_) ⇒ Object


90
91
92
# File 'lib/gitlab/ci/ansi2html.rb', line 90

def on_32(_)
  set_fg_color(2)
end

#on_33(_) ⇒ Object


94
95
96
# File 'lib/gitlab/ci/ansi2html.rb', line 94

def on_33(_)
  set_fg_color(3)
end

#on_34(_) ⇒ Object


98
99
100
# File 'lib/gitlab/ci/ansi2html.rb', line 98

def on_34(_)
  set_fg_color(4)
end

#on_35(_) ⇒ Object


102
103
104
# File 'lib/gitlab/ci/ansi2html.rb', line 102

def on_35(_)
  set_fg_color(5)
end

#on_36(_) ⇒ Object


106
107
108
# File 'lib/gitlab/ci/ansi2html.rb', line 106

def on_36(_)
  set_fg_color(6)
end

#on_37(_) ⇒ Object


110
111
112
# File 'lib/gitlab/ci/ansi2html.rb', line 110

def on_37(_)
  set_fg_color(7)
end

#on_38(stack) ⇒ Object


114
115
116
# File 'lib/gitlab/ci/ansi2html.rb', line 114

def on_38(stack)
  set_fg_color_256(stack)
end

#on_39(_) ⇒ Object


118
119
120
# File 'lib/gitlab/ci/ansi2html.rb', line 118

def on_39(_)
  set_fg_color(9)
end

#on_4(_) ⇒ Object


46
47
48
# File 'lib/gitlab/ci/ansi2html.rb', line 46

def on_4(_)
  enable(STYLE_SWITCHES[:underline])
end

#on_40(_) ⇒ Object


122
123
124
# File 'lib/gitlab/ci/ansi2html.rb', line 122

def on_40(_)
  set_bg_color(0)
end

#on_41(_) ⇒ Object


126
127
128
# File 'lib/gitlab/ci/ansi2html.rb', line 126

def on_41(_)
  set_bg_color(1)
end

#on_42(_) ⇒ Object


130
131
132
# File 'lib/gitlab/ci/ansi2html.rb', line 130

def on_42(_)
  set_bg_color(2)
end

#on_43(_) ⇒ Object


134
135
136
# File 'lib/gitlab/ci/ansi2html.rb', line 134

def on_43(_)
  set_bg_color(3)
end

#on_44(_) ⇒ Object


138
139
140
# File 'lib/gitlab/ci/ansi2html.rb', line 138

def on_44(_)
  set_bg_color(4)
end

#on_45(_) ⇒ Object


142
143
144
# File 'lib/gitlab/ci/ansi2html.rb', line 142

def on_45(_)
  set_bg_color(5)
end

#on_46(_) ⇒ Object


146
147
148
# File 'lib/gitlab/ci/ansi2html.rb', line 146

def on_46(_)
  set_bg_color(6)
end

#on_47(_) ⇒ Object


150
151
152
# File 'lib/gitlab/ci/ansi2html.rb', line 150

def on_47(_)
  set_bg_color(7)
end

#on_48(stack) ⇒ Object


154
155
156
# File 'lib/gitlab/ci/ansi2html.rb', line 154

def on_48(stack)
  set_bg_color_256(stack)
end

#on_49(_) ⇒ Object


158
159
160
# File 'lib/gitlab/ci/ansi2html.rb', line 158

def on_49(_)
  set_bg_color(9)
end

#on_8(_) ⇒ Object


50
51
52
# File 'lib/gitlab/ci/ansi2html.rb', line 50

def on_8(_)
  enable(STYLE_SWITCHES[:conceal])
end

#on_9(_) ⇒ Object


54
55
56
# File 'lib/gitlab/ci/ansi2html.rb', line 54

def on_9(_)
  enable(STYLE_SWITCHES[:cross])
end

#on_90(_) ⇒ Object


162
163
164
# File 'lib/gitlab/ci/ansi2html.rb', line 162

def on_90(_)
  set_fg_color(0, 'l')
end

#on_91(_) ⇒ Object


166
167
168
# File 'lib/gitlab/ci/ansi2html.rb', line 166

def on_91(_)
  set_fg_color(1, 'l')
end

#on_92(_) ⇒ Object


170
171
172
# File 'lib/gitlab/ci/ansi2html.rb', line 170

def on_92(_)
  set_fg_color(2, 'l')
end

#on_93(_) ⇒ Object


174
175
176
# File 'lib/gitlab/ci/ansi2html.rb', line 174

def on_93(_)
  set_fg_color(3, 'l')
end

#on_94(_) ⇒ Object


178
179
180
# File 'lib/gitlab/ci/ansi2html.rb', line 178

def on_94(_)
  set_fg_color(4, 'l')
end

#on_95(_) ⇒ Object


182
183
184
# File 'lib/gitlab/ci/ansi2html.rb', line 182

def on_95(_)
  set_fg_color(5, 'l')
end

#on_96(_) ⇒ Object


186
187
188
# File 'lib/gitlab/ci/ansi2html.rb', line 186

def on_96(_)
  set_fg_color(6, 'l')
end

#on_97(_) ⇒ Object


190
191
192
# File 'lib/gitlab/ci/ansi2html.rb', line 190

def on_97(_)
  set_fg_color(7, 'l')
end

#on_99(_) ⇒ Object


194
195
196
# File 'lib/gitlab/ci/ansi2html.rb', line 194

def on_99(_)
  set_fg_color(9, 'l')
end

#open_new_tagObject


387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
# File 'lib/gitlab/ci/ansi2html.rb', line 387

def open_new_tag
  css_classes = []

  unless @fg_color.nil?
    fg_color = @fg_color
    # Most terminals show bold colored text in the light color variant
    # Let's mimic that here
    if @style_mask & STYLE_SWITCHES[:bold] != 0
      fg_color.sub!(/fg-([a-z]{2,}+)/, 'fg-l-\1')
    end

    css_classes << fg_color
  end

  css_classes << @bg_color unless @bg_color.nil?

  STYLE_SWITCHES.each do |css_class, flag|
    css_classes << "term-#{css_class}" if @style_mask & flag != 0
  end

  if @sections.any?
    css_classes << "section"

    css_classes << if @lineno_in_section == 0
                     "section-header"
                   else
                     "line"
                   end

    css_classes += sections.map { |section| "js-s-#{section}" }
  end

  close_open_tags

  @out << if css_classes.any?
            %{<span class="#{css_classes.join(' ')}">}
          else
            %{<span>}
          end

  @n_open_tags += 1
end

#resetObject


464
465
466
467
468
# File 'lib/gitlab/ci/ansi2html.rb', line 464

def reset
  @fg_color = nil
  @bg_color = nil
  @style_mask = 0
end

#reset_stateObject


437
438
439
440
441
442
443
444
# File 'lib/gitlab/ci/ansi2html.rb', line 437

def reset_state
  @offset = 0
  @n_open_tags = 0
  @out = +''
  @sections = []
  @lineno_in_section = 0
  reset
end

#restore_state(new_state, stream) ⇒ Object


454
455
456
457
458
459
460
461
462
# File 'lib/gitlab/ci/ansi2html.rb', line 454

def restore_state(new_state, stream)
  state = Base64.urlsafe_decode64(new_state)
  state = Gitlab::Json.parse(state, symbolize_names: true)
  return if state[:offset].to_i > stream.size

  STATE_PARAMS.each do |param|
    send("#{param}=".to_sym, state[param]) # rubocop:disable GitlabSecurity/PublicSend
  end
end

#section_to_class_name(section) ⇒ Object


294
295
296
# File 'lib/gitlab/ci/ansi2html.rb', line 294

def section_to_class_name(section)
  section.to_s.downcase.gsub(/[^a-z0-9]/, '-')
end

#set_bg_color(color_index, prefix = nil) ⇒ Object


482
483
484
# File 'lib/gitlab/ci/ansi2html.rb', line 482

def set_bg_color(color_index, prefix = nil)
  @bg_color = get_term_color_class(color_index, ["bg", prefix])
end

#set_bg_color_256(command_stack) ⇒ Object


498
499
500
501
# File 'lib/gitlab/ci/ansi2html.rb', line 498

def set_bg_color_256(command_stack)
  css_class = get_xterm_color_class(command_stack, "bg")
  @bg_color = css_class unless css_class.nil?
end

#set_fg_color(color_index, prefix = nil) ⇒ Object


478
479
480
# File 'lib/gitlab/ci/ansi2html.rb', line 478

def set_fg_color(color_index, prefix = nil)
  @fg_color = get_term_color_class(color_index, ["fg", prefix])
end

#set_fg_color_256(command_stack) ⇒ Object


493
494
495
496
# File 'lib/gitlab/ci/ansi2html.rb', line 493

def set_fg_color_256(command_stack)
  css_class = get_xterm_color_class(command_stack, "fg")
  @fg_color = css_class unless css_class.nil?
end

#stateObject


446
447
448
449
450
451
452
# File 'lib/gitlab/ci/ansi2html.rb', line 446

def state
  state = STATE_PARAMS.inject({}) do |h, param|
    h[param] = send(param) # rubocop:disable GitlabSecurity/PublicSend
    h
  end
  Base64.urlsafe_encode64(state.to_json)
end

#write_in_tag(data) ⇒ Object


373
374
375
376
# File 'lib/gitlab/ci/ansi2html.rb', line 373

def write_in_tag(data)
  ensure_open_new_tag
  @out << data
end

#write_raw(data) ⇒ Object


378
379
380
381
# File 'lib/gitlab/ci/ansi2html.rb', line 378

def write_raw(data)
  close_open_tags
  @out << data
end