Class: Ansi::To::Html

Inherits:
Object
  • Object
show all
Defined in:
lib/ansi/to/html.rb,
lib/ansi/to/html/version.rb

Constant Summary collapse

ESCAPE =
"\x1b"
STYLES =
{}
PALLETE =
{}
VERSION =
"0.0.3"

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ Html

Returns a new instance of Html.



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ansi/to/html.rb', line 62

def initialize(input)
  @input =
    if input.respond_to?(:to_str)
      [input]
    elsif !input.respond_to?(:each)
      raise ArgumentError, "input must respond to each"
    else
      input
    end
  @stack = []
end

Instance Method Details

#each {|buf| ... } ⇒ Object

Yields:

  • (buf)


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/ansi/to/html.rb', line 85

def each
  buf = ''
  @input.each do |chunk|
    buf << chunk
    tokenize(buf) do |tok, data|
      case tok
      when :text
        yield data
      when :display
        case code = data
        when 0,39,49        ; yield reset_styles if @stack.any? # NOTE: 39/49 is reset for fg/bg color only
        when 1        ; yield push_tag("b") # bright
        when 2        ; #dim
        when 3, 4     ; yield push_tag("u")
        when 5, 6     ; yield push_tag("blink")
        when 7        ; #reverse
        when 8        ; yield push_style("display:none")
        when 9        ; yield push_tag("strike")
        when 30..37   ; yield push_style("ef#{code - 30}")
        when 40..47   ; yield push_style("eb#{code - 40}")
        when 90..97   ; yield push_style("ef#{8 + code - 90}")
        when 100..107 ; yield push_style("eb#{8 + code - 100}")
        end 
      when :xterm256f
        code = data
        yield push_style("ef#{code}")
      when :xterm256b
        code = data
        yield push_style("eb#{code}")
      end
    end
  end
  yield buf if !buf.empty?
  yield reset_styles if @stack.any?
  self
end

#push_style(style) ⇒ Object



131
132
133
# File 'lib/ansi/to/html.rb', line 131

def push_style(style)
  push_tag "span", style
end

#push_tag(tag, style = nil) ⇒ Object



122
123
124
125
126
127
128
129
# File 'lib/ansi/to/html.rb', line 122

def push_tag(tag, style=nil)
  style = STYLES[style] || PALLETE[@pallete || :linux][style] if style && !style.include?(':')
  @stack.push tag
  [ "<#{tag}",
    (" style='#{style}'" if style),
    ">"
  ].join
end

#reset_stylesObject



135
136
137
138
# File 'lib/ansi/to/html.rb', line 135

def reset_styles
  stack, @stack = @stack, []
  stack.reverse.map { |tag| "</#{tag}>" }.join
end

#to_html(pallete = :linux) ⇒ Object



74
75
76
77
78
79
80
81
82
83
# File 'lib/ansi/to/html.rb', line 74

def to_html(pallete = :linux)
  buf = []
  if PALLETE.keys.include?(pallete)
    @pallete = pallete
  else
    warn "--pallet=#{pallete} is unknown."
  end
  each { |chunk| buf << chunk }
  buf.join
end

#tokenize(text) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/ansi/to/html.rb', line 140

def tokenize(text)
  tokens = [
    # characters to remove completely
    [/\A\x08+/, lambda { |m| '' }],

    [/\A\x1b\[38;5;(\d+)m/, lambda { |m| yield :xterm256f, $1.to_i; '' } ],
    [/\A\x1b\[48;5;(\d+)m/, lambda { |m| yield :xterm256b, $1.to_i; '' } ],

    # ansi escape sequences that mess with the display
    [/\A\x1b\[((?:\d{1,3};?)+|)m/, lambda { |m|
      m = '0' if m.strip.empty?
      m.chomp(';').split(';').
        each { |code| yield :display, code.to_i };
      '' }],

        # malformed sequences
        [/\A\x1b\[?[\d;]{0,3}/, lambda { |m| '' }],

        # real text
        [/\A([^\x1b\x08]+)/m, lambda { |m| yield :text, m; '' }]
  ]

  while (size = text.size) > 0
    tokens.each do |pattern, sub|
      break if text.sub!(pattern) { sub.call($1) }
    end
    break if text.size == size
  end
end