class ColorCode::Ruby

Constants

TOKENIZER

Public Class Methods

new(text) click to toggle source
# File lib/ruby_color.rb, line 8
def initialize(text)
  @start_line = 1
  @text       = text
end

Public Instance Methods

colorize() click to toggle source
# File lib/ruby_color.rb, line 13
def colorize
  @text.lines.map.with_index do |line, idx|
    line_no = idx + @start_line
    code_line = ""
    TOKENIZER.tokenize( line ) do |token|
      case token.group.to_s
      when "string"   then code_line += token.colorize(:light_yellow)
      when "ident"    then code_line += token.colorize(:white)
      when "normal"   then code_line += token.colorize(:cyan)
      when "keyword"  then code_line += token.colorize(:blue)
      when "punct"    then code_line += token.colorize(:white)
      when "symbol"   then code_line += token.colorize(:light_black)
      when "number"   then code_line += token.colorize(:light_black)
      when "expr"     then code_line += token.colorize(:white)
      when "comment"  then code_line += token.colorize(:magenta)
      when "constant" then code_line += token.colorize(:yellow) 
      else code_line += token.group.to_s
      end
    end
    (line_no.to_s.rjust(4) + ": ").colorize(:light_black) + code_line
  end.join()
end