Class: ANSI2HTML::Main

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

Constant Summary collapse

COLOR =
{
   '1' => 'bold',
  '30' => 'black',
  '31' => 'red',
  '32' => 'green',
  '33' => 'yellow',
  '34' => 'blue',
  '35' => 'magenta',
  '36' => 'cyan',
  '37' => 'white',
  '90' => 'grey'
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ansi, out, envelope = false, black = false) ⇒ Main

Returns a new instance of Main.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ansi2html/main.rb', line 22

def initialize(ansi, out, envelope=false, black=false)
  if(envelope)
    background, color = black ? %w(black white) : %w(white black)
    out.print %{<!doctype html>
<html>
<head>
  <meta charset="utf-8" />
  <style>
body {
  background-color: #{background}; color: #{color};
}
.bold {
  font-weight: bold;
}
.black {
  color: black;
}
.red {
  color: red;
}
.green {
  color: green;
}
.yellow {
  color: yellow;
}
.blue {
  color: blue;
}
.magenta {
  color: magenta;
}
.cyan {
  color: cyan;
}
.white {
  color: white;
}
.grey {
  color: grey;
}
  </style>
</head>
<body><pre><code>}
  end
  s = StringScanner.new(ansi.gsub("<", "&lt;"))
  while(!s.eos?)
    if s.scan(/\e\[(3[0-7]|90|1)m/)
      out.print(%{<span class="#{COLOR[s[1]]}">})
    else
      if s.scan(/\e\[0m/)
        out.print(%{</span>})
      else
        out.print(s.scan(/./m))
      end
    end
  end

  if(envelope)
    out.print %{</code></pre></body></html>}
  end
end

Class Method Details

.executeObject



18
19
20
# File 'lib/ansi2html/main.rb', line 18

def self.execute
  new(STDIN.read, STDOUT, ARGV.index('--envelope'), ARGV.index('--black'))
end