class CodeRay::Encoders::Terminal

MIT License (www.opensource.org/licenses/mit-license.php)
Based on idea by Nathan Weizenbaum (nex-3.com)
By Rob Aldred (robaldred.co.uk)
== Authors & License
Alias: term
Note: This encoder is in beta. It currently doesn’t use the Styles.
Outputs code highlighted for a color terminal.

def begin_group kind

def begin_group kind
  @opened << kind
  @out << open_token(kind)
end

def end_group kind

def end_group kind
  if @opened.pop
    @color_scopes.pop
    @out << "\e[0m"
    if outer_color = @color_scopes.last[:self]
      @out << outer_color
    end
  end
end

def end_line kind

def end_line kind
  @out << (@line_filler ||= "\t" * 100)
  end_group kind
end

def open_token kind

def open_token kind
  if color = @color_scopes.last[kind]
    if color.is_a? Hash
      @color_scopes << color
      color[:self]
    else
      @color_scopes << @color_scopes.last
      color
    end
  else
    @color_scopes << @color_scopes.last
    ''
  end
end

def setup(options)

def setup(options)
  super
  @opened = []
  @color_scopes = [TOKEN_COLORS]
end

def text_token text, kind

def text_token text, kind
  if color = @color_scopes.last[kind]
    color = color[:self] if color.is_a? Hash
    
    @out << color
    @out << (text.index("\n") ? text.gsub("\n", "\e[0m\n" + color) : text)
    @out << "\e[0m"
    if outer_color = @color_scopes.last[:self]
      @out << outer_color
    end
  else
    @out << text
  end
end