Object
Converts ANSI color sequences to HTML.
The ANSI module is based on code from the following libraries:
ansi2html.sh:
http://code.google.com/p/wrdese/source/browse/trunk/b/ansi2html.sh?r=5
HTML::FromANSI:
http://cpansearch.perl.org/src/NUFFIN/HTML-FromANSI-2.03/lib/HTML/FromANSI.pm
# File lib/bcat/ansi.rb, line 92 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 ; yield reset_styles if @stack.any? when 1 ; yield push_tag("b") # bright when 2 ; #dim when 3 ; yield push_tag("u") when 5 ; 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 end end end yield buf if !buf.empty? yield reset_styles if @stack.any? self end
# File lib/bcat/ansi.rb, line 132 def push_style(style) push_tag "span", style end
# File lib/bcat/ansi.rb, line 123 def push_tag(tag, style=nil) style = STYLES[style] if style && !style.include?(':') @stack.push tag [ "<#{tag}", (" style='#{style}'" if style), ">" ].join end
# File lib/bcat/ansi.rb, line 136 def reset_styles stack, @stack = @stack, [] stack.reverse.map { |tag| "</#{tag}>" }.join end
# File lib/bcat/ansi.rb, line 86 def to_html buf = [] each { |chunk| buf << chunk } buf.join end
# File lib/bcat/ansi.rb, line 141 def tokenize(text) tokens = [ # characters to remove completely [/\A\x08+/, lambda { |m| '' }], # 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]+)/, lambda { |m| yield :text, m; '' }] ] while (size = text.size) > 0 tokens.each do |pattern, sub| while text.sub!(pattern) { sub.call($1) } end end break if text.size == size end end
Generated with the Darkfish Rdoc Generator 2.