debian-mirror-gitlab/lib/ci/ansi2html.rb

332 lines
8.2 KiB
Ruby
Raw Normal View History

2015-09-25 12:07:36 +05:30
# ANSI color library
#
# Implementation per http://en.wikipedia.org/wiki/ANSI_escape_code
module Ci
module Ansi2html
# keys represent the trailing digit in color changing command (30-37, 40-47, 90-97. 100-107)
COLOR = {
0 => 'black', # not that this is gray in the intense color table
1 => 'red',
2 => 'green',
3 => 'yellow',
4 => 'blue',
5 => 'magenta',
6 => 'cyan',
7 => 'white', # not that this is gray in the dark (aka default) color table
2017-08-17 22:00:37 +05:30
}.freeze
2015-09-25 12:07:36 +05:30
STYLE_SWITCHES = {
bold: 0x01,
italic: 0x02,
underline: 0x04,
conceal: 0x08,
cross: 0x10,
2017-08-17 22:00:37 +05:30
}.freeze
2015-09-25 12:07:36 +05:30
2016-06-02 11:05:42 +05:30
def self.convert(ansi, state = nil)
Converter.new.convert(ansi, state)
2015-09-25 12:07:36 +05:30
end
class Converter
def on_0(s) reset() end
2017-08-17 22:00:37 +05:30
2015-09-25 12:07:36 +05:30
def on_1(s) enable(STYLE_SWITCHES[:bold]) end
2017-08-17 22:00:37 +05:30
2015-09-25 12:07:36 +05:30
def on_3(s) enable(STYLE_SWITCHES[:italic]) end
2017-08-17 22:00:37 +05:30
2015-09-25 12:07:36 +05:30
def on_4(s) enable(STYLE_SWITCHES[:underline]) end
2017-08-17 22:00:37 +05:30
2015-09-25 12:07:36 +05:30
def on_8(s) enable(STYLE_SWITCHES[:conceal]) end
2017-08-17 22:00:37 +05:30
2015-09-25 12:07:36 +05:30
def on_9(s) enable(STYLE_SWITCHES[:cross]) end
def on_21(s) disable(STYLE_SWITCHES[:bold]) end
2017-08-17 22:00:37 +05:30
2015-09-25 12:07:36 +05:30
def on_22(s) disable(STYLE_SWITCHES[:bold]) end
2017-08-17 22:00:37 +05:30
2015-09-25 12:07:36 +05:30
def on_23(s) disable(STYLE_SWITCHES[:italic]) end
2017-08-17 22:00:37 +05:30
2015-09-25 12:07:36 +05:30
def on_24(s) disable(STYLE_SWITCHES[:underline]) end
2017-08-17 22:00:37 +05:30
2015-09-25 12:07:36 +05:30
def on_28(s) disable(STYLE_SWITCHES[:conceal]) end
2017-08-17 22:00:37 +05:30
2015-09-25 12:07:36 +05:30
def on_29(s) disable(STYLE_SWITCHES[:cross]) end
def on_30(s) set_fg_color(0) end
2017-08-17 22:00:37 +05:30
2015-09-25 12:07:36 +05:30
def on_31(s) set_fg_color(1) end
2017-08-17 22:00:37 +05:30
2015-09-25 12:07:36 +05:30
def on_32(s) set_fg_color(2) end
2017-08-17 22:00:37 +05:30
2015-09-25 12:07:36 +05:30
def on_33(s) set_fg_color(3) end
2017-08-17 22:00:37 +05:30
2015-09-25 12:07:36 +05:30
def on_34(s) set_fg_color(4) end
2017-08-17 22:00:37 +05:30
2015-09-25 12:07:36 +05:30
def on_35(s) set_fg_color(5) end
2017-08-17 22:00:37 +05:30
2015-09-25 12:07:36 +05:30
def on_36(s) set_fg_color(6) end
2017-08-17 22:00:37 +05:30
2015-09-25 12:07:36 +05:30
def on_37(s) set_fg_color(7) end
2017-08-17 22:00:37 +05:30
2015-09-25 12:07:36 +05:30
def on_38(s) set_fg_color_256(s) end
2017-08-17 22:00:37 +05:30
2015-09-25 12:07:36 +05:30
def on_39(s) set_fg_color(9) end
def on_40(s) set_bg_color(0) end
2017-08-17 22:00:37 +05:30
2015-09-25 12:07:36 +05:30
def on_41(s) set_bg_color(1) end
2017-08-17 22:00:37 +05:30
2015-09-25 12:07:36 +05:30
def on_42(s) set_bg_color(2) end
2017-08-17 22:00:37 +05:30
2015-09-25 12:07:36 +05:30
def on_43(s) set_bg_color(3) end
2017-08-17 22:00:37 +05:30
2015-09-25 12:07:36 +05:30
def on_44(s) set_bg_color(4) end
2017-08-17 22:00:37 +05:30
2015-09-25 12:07:36 +05:30
def on_45(s) set_bg_color(5) end
2017-08-17 22:00:37 +05:30
2015-09-25 12:07:36 +05:30
def on_46(s) set_bg_color(6) end
2017-08-17 22:00:37 +05:30
2015-09-25 12:07:36 +05:30
def on_47(s) set_bg_color(7) end
2017-08-17 22:00:37 +05:30
2015-09-25 12:07:36 +05:30
def on_48(s) set_bg_color_256(s) end
2017-08-17 22:00:37 +05:30
2015-09-25 12:07:36 +05:30
def on_49(s) set_bg_color(9) end
def on_90(s) set_fg_color(0, 'l') end
2017-08-17 22:00:37 +05:30
2015-09-25 12:07:36 +05:30
def on_91(s) set_fg_color(1, 'l') end
2017-08-17 22:00:37 +05:30
2015-09-25 12:07:36 +05:30
def on_92(s) set_fg_color(2, 'l') end
2017-08-17 22:00:37 +05:30
2015-09-25 12:07:36 +05:30
def on_93(s) set_fg_color(3, 'l') end
2017-08-17 22:00:37 +05:30
2015-09-25 12:07:36 +05:30
def on_94(s) set_fg_color(4, 'l') end
2017-08-17 22:00:37 +05:30
2015-09-25 12:07:36 +05:30
def on_95(s) set_fg_color(5, 'l') end
2017-08-17 22:00:37 +05:30
2015-09-25 12:07:36 +05:30
def on_96(s) set_fg_color(6, 'l') end
2017-08-17 22:00:37 +05:30
2015-09-25 12:07:36 +05:30
def on_97(s) set_fg_color(7, 'l') end
2017-08-17 22:00:37 +05:30
2015-09-25 12:07:36 +05:30
def on_99(s) set_fg_color(9, 'l') end
def on_100(s) set_bg_color(0, 'l') end
2017-08-17 22:00:37 +05:30
2015-09-25 12:07:36 +05:30
def on_101(s) set_bg_color(1, 'l') end
2017-08-17 22:00:37 +05:30
2015-09-25 12:07:36 +05:30
def on_102(s) set_bg_color(2, 'l') end
2017-08-17 22:00:37 +05:30
2015-09-25 12:07:36 +05:30
def on_103(s) set_bg_color(3, 'l') end
2017-08-17 22:00:37 +05:30
2015-09-25 12:07:36 +05:30
def on_104(s) set_bg_color(4, 'l') end
2017-08-17 22:00:37 +05:30
2015-09-25 12:07:36 +05:30
def on_105(s) set_bg_color(5, 'l') end
2017-08-17 22:00:37 +05:30
2015-09-25 12:07:36 +05:30
def on_106(s) set_bg_color(6, 'l') end
2017-08-17 22:00:37 +05:30
2015-09-25 12:07:36 +05:30
def on_107(s) set_bg_color(7, 'l') end
2017-08-17 22:00:37 +05:30
2015-09-25 12:07:36 +05:30
def on_109(s) set_bg_color(9, 'l') end
2016-06-02 11:05:42 +05:30
attr_accessor :offset, :n_open_tags, :fg_color, :bg_color, :style_mask
2017-08-17 22:00:37 +05:30
STATE_PARAMS = [:offset, :n_open_tags, :fg_color, :bg_color, :style_mask].freeze
2016-06-02 11:05:42 +05:30
2017-08-17 22:00:37 +05:30
def convert(stream, new_state)
2016-06-02 11:05:42 +05:30
reset_state
2017-08-17 22:00:37 +05:30
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
2016-06-02 11:05:42 +05:30
open_new_tag
2015-09-25 12:07:36 +05:30
2017-08-17 22:00:37 +05:30
stream.each_line do |line|
s = StringScanner.new(line)
until s.eos?
if s.scan(/\e([@-_])(.*?)([@-~])/)
handle_sequence(s)
elsif s.scan(/\e(([@-_])(.*?)?)?$/)
break
elsif s.scan(/</)
@out << '&lt;'
elsif s.scan(/\r?\n/)
@out << '<br>'
else
@out << s.scan(/./m)
end
@offset += s.matched_size
2015-09-25 12:07:36 +05:30
end
end
close_open_tags()
2016-06-02 11:05:42 +05:30
2017-08-17 22:00:37 +05:30
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
)
2015-09-25 12:07:36 +05:30
end
def handle_sequence(s)
indicator = s[1]
commands = s[2].split ';'
terminator = s[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")
2017-08-17 22:00:37 +05:30
return unless indicator == '[' && terminator == 'm'
2015-09-25 12:07:36 +05:30
close_open_tags()
if commands.empty?()
reset()
return
end
evaluate_command_stack(commands)
2016-06-02 11:05:42 +05:30
open_new_tag
end
def evaluate_command_stack(stack)
return unless command = stack.shift()
if self.respond_to?("on_#{command}", true)
self.send("on_#{command}", stack)
end
evaluate_command_stack(stack)
end
def open_new_tag
2015-09-25 12:07:36 +05:30
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-(\w{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
2016-06-02 11:05:42 +05:30
return if css_classes.empty?
2015-09-25 12:07:36 +05:30
@out << %{<span class="#{css_classes.join(' ')}">}
@n_open_tags += 1
end
def close_open_tags
while @n_open_tags > 0
@out << %{</span>}
@n_open_tags -= 1
end
end
2016-06-02 11:05:42 +05:30
def reset_state
@offset = 0
@n_open_tags = 0
@out = ''
reset
end
def state
state = STATE_PARAMS.inject({}) do |h, param|
h[param] = send(param)
h
end
Base64.urlsafe_encode64(state.to_json)
end
2017-08-17 22:00:37 +05:30
def restore_state(new_state, stream)
2016-06-02 11:05:42 +05:30
state = Base64.urlsafe_decode64(new_state)
state = JSON.parse(state, symbolize_names: true)
2017-08-17 22:00:37 +05:30
return if state[:offset].to_i > stream.size
2016-06-02 11:05:42 +05:30
STATE_PARAMS.each do |param|
send("#{param}=".to_sym, state[param])
end
end
2015-09-25 12:07:36 +05:30
def reset
@fg_color = nil
@bg_color = nil
@style_mask = 0
end
def enable(flag)
@style_mask |= flag
end
def disable(flag)
@style_mask &= ~flag
end
def set_fg_color(color_index, prefix = nil)
@fg_color = get_term_color_class(color_index, ["fg", prefix])
end
def set_bg_color(color_index, prefix = nil)
@bg_color = get_term_color_class(color_index, ["bg", prefix])
end
def get_term_color_class(color_index, prefix)
color_name = COLOR[color_index]
return nil if color_name.nil?
get_color_class(["term", prefix, color_name])
end
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
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
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
def get_color_class(segments)
[segments].flatten.compact.join('-')
end
end
end
end