class Ansi::To::Html

Constants

ESCAPE
PALLETE
STYLES
VERSION

Public Class Methods

new(input) click to toggle source
# 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

Public Instance Methods

each() { |data| ... } click to toggle source
# 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) click to toggle source
# File lib/ansi/to/html.rb, line 131
def push_style(style)
  push_tag "span", style
end
push_tag(tag, style=nil) click to toggle source
# 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_styles() click to toggle source
# 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) click to toggle source
# 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) { |:xterm256f, $to_i;| ... } click to toggle source
# 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