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

Inherits:
Object
  • Object
show all
Includes:
EncodingHelper
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

Constants included from EncodingHelper

EncodingHelper::BOM_UTF8, EncodingHelper::ENCODING_CONFIDENCE_THRESHOLD, EncodingHelper::ESCAPED_CHARS, EncodingHelper::UNICODE_REPLACEMENT_CHARACTER

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from EncodingHelper

#binary_io, #detect_binary?, #detect_encoding, #detect_libgit2_binary?, #encode!, #encode_binary, #encode_utf8, #encode_utf8_no_detect, #encode_utf8_with_escaping!, #encode_utf8_with_replacement_character, #strip_bom, #unquote_path

Instance Attribute Details

#bg_colorObject

Returns the value of attribute bg_color.



238
239
240
# File 'lib/gitlab/ci/ansi2html.rb', line 238

def bg_color
  @bg_color
end

#fg_colorObject

Returns the value of attribute fg_color.



238
239
240
# File 'lib/gitlab/ci/ansi2html.rb', line 238

def fg_color
  @fg_color
end

#lineno_in_sectionObject

Returns the value of attribute lineno_in_section.



238
239
240
# File 'lib/gitlab/ci/ansi2html.rb', line 238

def lineno_in_section
  @lineno_in_section
end

#n_open_tagsObject

Returns the value of attribute n_open_tags.



238
239
240
# File 'lib/gitlab/ci/ansi2html.rb', line 238

def n_open_tags
  @n_open_tags
end

#offsetObject

Returns the value of attribute offset.



238
239
240
# File 'lib/gitlab/ci/ansi2html.rb', line 238

def offset
  @offset
end

#sectionsObject

Returns the value of attribute sections.



238
239
240
# File 'lib/gitlab/ci/ansi2html.rb', line 238

def sections
  @sections
end

#style_maskObject

Returns the value of attribute style_mask.



238
239
240
# File 'lib/gitlab/ci/ansi2html.rb', line 238

def style_mask
  @style_mask
end

Instance Method Details

#close_open_tagsObject



434
435
436
437
438
439
# File 'lib/gitlab/ci/ansi2html.rb', line 434

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

#convert(stream, new_state) ⇒ Object



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
293
294
295
# File 'lib/gitlab/ci/ansi2html.rb', line 242

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|
    line = encode_utf8_no_detect(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

  Ansi2html::Result.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



343
344
345
# File 'lib/gitlab/ci/ansi2html.rb', line 343

def data_section_names
  @sections.join(" ")
end

#disable(flag) ⇒ Object



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

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

#enable(flag) ⇒ Object



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

def enable(flag)
  @style_mask |= flag
end

#ensure_open_new_tagObject



387
388
389
# File 'lib/gitlab/ci/ansi2html.rb', line 387

def ensure_open_new_tag
  open_new_tag if @n_open_tags == 0
end

#evaluate_command_stack(stack) ⇒ Object



367
368
369
370
371
372
373
374
375
# File 'lib/gitlab/ci/ansi2html.rb', line 367

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



520
521
522
# File 'lib/gitlab/ci/ansi2html.rb', line 520

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

#get_term_color_class(color_index, prefix) ⇒ Object



489
490
491
492
493
494
# File 'lib/gitlab/ci/ansi2html.rb', line 489

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



506
507
508
509
510
511
512
513
514
515
516
517
518
# File 'lib/gitlab/ci/ansi2html.rb', line 506

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



301
302
303
304
305
306
# File 'lib/gitlab/ci/ansi2html.rb', line 301

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



308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/gitlab/ci/ansi2html.rb', line 308

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

  normalized_section = section_to_class_name(section)

  case action
  when "start"
    handle_section_start(normalized_section, timestamp)
  when "end"
    handle_section_end(normalized_section, timestamp)
  end
end

#handle_section_end(section, timestamp) ⇒ Object



331
332
333
334
335
336
337
338
339
340
341
# File 'lib/gitlab/ci/ansi2html.rb', line 331

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



323
324
325
326
327
328
329
# File 'lib/gitlab/ci/ansi2html.rb', line 323

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



347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# File 'lib/gitlab/ci/ansi2html.rb', line 347

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



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

def on_0(_)
  reset
end

#on_1(_) ⇒ Object



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

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

#on_100(_) ⇒ Object



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

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

#on_101(_) ⇒ Object



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

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

#on_102(_) ⇒ Object



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

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

#on_103(_) ⇒ Object



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

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

#on_104(_) ⇒ Object



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

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

#on_105(_) ⇒ Object



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

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

#on_106(_) ⇒ Object



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

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

#on_107(_) ⇒ Object



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

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

#on_109(_) ⇒ Object



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

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

#on_21(_) ⇒ Object



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

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

#on_22(_) ⇒ Object



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

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

#on_23(_) ⇒ Object



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

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

#on_24(_) ⇒ Object



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

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

#on_28(_) ⇒ Object



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

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

#on_29(_) ⇒ Object



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

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

#on_3(_) ⇒ Object



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

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

#on_30(_) ⇒ Object



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

def on_30(_)
  set_fg_color(0)
end

#on_31(_) ⇒ Object



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

def on_31(_)
  set_fg_color(1)
end

#on_32(_) ⇒ Object



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

def on_32(_)
  set_fg_color(2)
end

#on_33(_) ⇒ Object



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

def on_33(_)
  set_fg_color(3)
end

#on_34(_) ⇒ Object



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

def on_34(_)
  set_fg_color(4)
end

#on_35(_) ⇒ Object



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

def on_35(_)
  set_fg_color(5)
end

#on_36(_) ⇒ Object



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

def on_36(_)
  set_fg_color(6)
end

#on_37(_) ⇒ Object



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

def on_37(_)
  set_fg_color(7)
end

#on_38(stack) ⇒ Object



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

def on_38(stack)
  set_fg_color_256(stack)
end

#on_39(_) ⇒ Object



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

def on_39(_)
  set_fg_color(9)
end

#on_4(_) ⇒ Object



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

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

#on_40(_) ⇒ Object



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

def on_40(_)
  set_bg_color(0)
end

#on_41(_) ⇒ Object



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

def on_41(_)
  set_bg_color(1)
end

#on_42(_) ⇒ Object



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

def on_42(_)
  set_bg_color(2)
end

#on_43(_) ⇒ Object



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

def on_43(_)
  set_bg_color(3)
end

#on_44(_) ⇒ Object



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

def on_44(_)
  set_bg_color(4)
end

#on_45(_) ⇒ Object



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

def on_45(_)
  set_bg_color(5)
end

#on_46(_) ⇒ Object



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

def on_46(_)
  set_bg_color(6)
end

#on_47(_) ⇒ Object



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

def on_47(_)
  set_bg_color(7)
end

#on_48(stack) ⇒ Object



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

def on_48(stack)
  set_bg_color_256(stack)
end

#on_49(_) ⇒ Object



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

def on_49(_)
  set_bg_color(9)
end

#on_8(_) ⇒ Object



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

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

#on_9(_) ⇒ Object



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

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

#on_90(_) ⇒ Object



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

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

#on_91(_) ⇒ Object



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

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

#on_92(_) ⇒ Object



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

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

#on_93(_) ⇒ Object



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

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

#on_94(_) ⇒ Object



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

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

#on_95(_) ⇒ Object



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

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

#on_96(_) ⇒ Object



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

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

#on_97(_) ⇒ Object



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

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

#on_99(_) ⇒ Object



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

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

#open_new_tagObject



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
429
430
431
432
# File 'lib/gitlab/ci/ansi2html.rb', line 391

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



467
468
469
470
471
# File 'lib/gitlab/ci/ansi2html.rb', line 467

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

#reset_stateObject



441
442
443
444
445
446
447
448
# File 'lib/gitlab/ci/ansi2html.rb', line 441

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

#restore_state(new_state, stream) ⇒ Object



457
458
459
460
461
462
463
464
465
# File 'lib/gitlab/ci/ansi2html.rb', line 457

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



297
298
299
# File 'lib/gitlab/ci/ansi2html.rb', line 297

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

#set_bg_color(color_index, prefix = nil) ⇒ Object



485
486
487
# File 'lib/gitlab/ci/ansi2html.rb', line 485

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



501
502
503
504
# File 'lib/gitlab/ci/ansi2html.rb', line 501

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



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

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



496
497
498
499
# File 'lib/gitlab/ci/ansi2html.rb', line 496

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



450
451
452
453
454
455
# File 'lib/gitlab/ci/ansi2html.rb', line 450

def state
  state = STATE_PARAMS.index_with do |param|
    send(param) # rubocop:disable GitlabSecurity/PublicSend
  end
  Base64.urlsafe_encode64(state.to_json)
end

#write_in_tag(data) ⇒ Object



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

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

#write_raw(data) ⇒ Object



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

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