Class: ColorCode::Ruby

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_color.rb

Constant Summary collapse

TOKENIZER =
Syntax.load "ruby"

Instance Method Summary collapse

Constructor Details

#initialize(text) ⇒ Ruby

Returns a new instance of Ruby.



8
9
10
11
# File 'lib/ruby_color.rb', line 8

def initialize(text)
  @start_line = 1
  @text       = text
end

Instance Method Details

#colorizeObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 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