Class: Ci::Ansi2html::Converter

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

Instance Method Summary collapse

Instance Method Details

#close_open_tagsObject


159
160
161
162
163
164
# File 'lib/ci/ansi2html.rb', line 159

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

#convert(ansi) ⇒ Object


87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/ci/ansi2html.rb', line 87

def convert(ansi)
  @out = ""
  @n_open_tags = 0
  reset()

  s = StringScanner.new(ansi.gsub("<", "&lt;"))
  while(!s.eos?)
    if s.scan(/\e([@-_])(.*?)([@-~])/)
      handle_sequence(s)
    else
      @out << s.scan(/./m)
    end
  end

  close_open_tags()
  @out
end

#disable(flag) ⇒ Object


176
177
178
# File 'lib/ci/ansi2html.rb', line 176

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

#enable(flag) ⇒ Object


172
173
174
# File 'lib/ci/ansi2html.rb', line 172

def enable(flag)
  @style_mask |= flag
end

#evaluate_command_stack(stack) ⇒ Object


144
145
146
147
148
149
150
151
152
# File 'lib/ci/ansi2html.rb', line 144

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

#get_color_class(segments) ⇒ Object


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

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

#get_term_color_class(color_index, prefix) ⇒ Object


188
189
190
191
192
193
# File 'lib/ci/ansi2html.rb', line 188

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

#get_xterm_color_class(command_stack, prefix) ⇒ Object


205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/ci/ansi2html.rb', line 205

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_sequence(s) ⇒ Object


105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/ci/ansi2html.rb', line 105

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")
  return unless indicator == '[' and terminator == 'm'

  close_open_tags()

  if commands.empty?()
    reset()
    return
  end

  evaluate_command_stack(commands)

  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

  open_new_tag(css_classes) if css_classes.length > 0
end

#on_0(s) ⇒ Object


31
# File 'lib/ci/ansi2html.rb', line 31

def on_0(s) reset()                            end

#on_1(s) ⇒ Object


32
# File 'lib/ci/ansi2html.rb', line 32

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

#on_100(s) ⇒ Object


77
# File 'lib/ci/ansi2html.rb', line 77

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

#on_101(s) ⇒ Object


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

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

#on_102(s) ⇒ Object


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

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

#on_103(s) ⇒ Object


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

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

#on_104(s) ⇒ Object


81
# File 'lib/ci/ansi2html.rb', line 81

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

#on_105(s) ⇒ Object


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

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

#on_106(s) ⇒ Object


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

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

#on_107(s) ⇒ Object


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

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

#on_109(s) ⇒ Object


85
# File 'lib/ci/ansi2html.rb', line 85

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

#on_21(s) ⇒ Object


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

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

#on_22(s) ⇒ Object


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

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

#on_23(s) ⇒ Object


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

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

#on_24(s) ⇒ Object


41
# File 'lib/ci/ansi2html.rb', line 41

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

#on_28(s) ⇒ Object


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

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

#on_29(s) ⇒ Object


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

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

#on_3(s) ⇒ Object


33
# File 'lib/ci/ansi2html.rb', line 33

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

#on_30(s) ⇒ Object


45
# File 'lib/ci/ansi2html.rb', line 45

def on_30(s) set_fg_color(0) end

#on_31(s) ⇒ Object


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

def on_31(s) set_fg_color(1) end

#on_32(s) ⇒ Object


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

def on_32(s) set_fg_color(2) end

#on_33(s) ⇒ Object


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

def on_33(s) set_fg_color(3) end

#on_34(s) ⇒ Object


49
# File 'lib/ci/ansi2html.rb', line 49

def on_34(s) set_fg_color(4) end

#on_35(s) ⇒ Object


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

def on_35(s) set_fg_color(5) end

#on_36(s) ⇒ Object


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

def on_36(s) set_fg_color(6) end

#on_37(s) ⇒ Object


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

def on_37(s) set_fg_color(7) end

#on_38(s) ⇒ Object


53
# File 'lib/ci/ansi2html.rb', line 53

def on_38(s) set_fg_color_256(s) end

#on_39(s) ⇒ Object


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

def on_39(s) set_fg_color(9) end

#on_4(s) ⇒ Object


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

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

#on_40(s) ⇒ Object


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

def on_40(s) set_bg_color(0) end

#on_41(s) ⇒ Object


57
# File 'lib/ci/ansi2html.rb', line 57

def on_41(s) set_bg_color(1) end

#on_42(s) ⇒ Object


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

def on_42(s) set_bg_color(2) end

#on_43(s) ⇒ Object


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

def on_43(s) set_bg_color(3) end

#on_44(s) ⇒ Object


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

def on_44(s) set_bg_color(4) end

#on_45(s) ⇒ Object


61
# File 'lib/ci/ansi2html.rb', line 61

def on_45(s) set_bg_color(5) end

#on_46(s) ⇒ Object


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

def on_46(s) set_bg_color(6) end

#on_47(s) ⇒ Object


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

def on_47(s) set_bg_color(7) end

#on_48(s) ⇒ Object


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

def on_48(s) set_bg_color_256(s) end

#on_49(s) ⇒ Object


65
# File 'lib/ci/ansi2html.rb', line 65

def on_49(s) set_bg_color(9) end

#on_8(s) ⇒ Object


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

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

#on_9(s) ⇒ Object


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

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

#on_90(s) ⇒ Object


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

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

#on_91(s) ⇒ Object


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

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

#on_92(s) ⇒ Object


69
# File 'lib/ci/ansi2html.rb', line 69

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

#on_93(s) ⇒ Object


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

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

#on_94(s) ⇒ Object


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

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

#on_95(s) ⇒ Object


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

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

#on_96(s) ⇒ Object


73
# File 'lib/ci/ansi2html.rb', line 73

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

#on_97(s) ⇒ Object


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

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

#on_99(s) ⇒ Object


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

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

#open_new_tag(css_classes) ⇒ Object


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

def open_new_tag(css_classes)
  @out << %{<span class="#{css_classes.join(' ')}">}
  @n_open_tags += 1
end

#resetObject


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

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

#set_bg_color(color_index, prefix = nil) ⇒ Object


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

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


200
201
202
203
# File 'lib/ci/ansi2html.rb', line 200

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


180
181
182
# File 'lib/ci/ansi2html.rb', line 180

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


195
196
197
198
# File 'lib/ci/ansi2html.rb', line 195

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