Module: TermColor

Includes:
REXML
Defined in:
lib/termcolor.rb

Defined Under Namespace

Classes: MyListener

Constant Summary collapse

VERSION =
'1.1.0'

Class Method Summary collapse

Class Method Details

.colorize(text, color) ⇒ Object



56
57
58
# File 'lib/termcolor.rb', line 56

def colorize(text, color)
  parse("<#{color}>#{escape(text)}</#{color}>")
end

.escape(text) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/termcolor.rb', line 20

def escape(text)
  text.gsub(/[&<>'"]/) do | match |
    case match
      when '&' then '&amp;'
      when '<' then '&lt;'
      when '>' then '&gt;'
      when "'" then '&apos;'
      when '"' then '&quot;'
    end
  end
end

.parse(text) ⇒ Object



14
15
16
17
18
# File 'lib/termcolor.rb', line 14

def parse(text)
  listener = MyListener.new 
  REXML::Parsers::StreamParser.new(prepare_parse(text), listener).parse
  listener.result
end

.prepare_parse(text) ⇒ Object



44
45
46
# File 'lib/termcolor.rb', line 44

def prepare_parse(text)
  text.gsub(/<(\/?)(\d+)>/, '<\1_\2>')
end

.test(*args) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/termcolor.rb', line 48

def test(*args)
  args = (0..109).to_a if args.empty?
  args.each_with_index do |color, index|
    print parse("<#{color}> #{color} </#{color}>") + "\t"
    puts if (index + 1) % 10 == 0
  end
end

.unescape(text) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/termcolor.rb', line 32

def unescape(text)
  text.gsub(/&(lt|gt|amp|quot|apos);/) do | match |
    case match
      when '&amp;' then '&'
      when '&lt;' then '<'
      when '&gt;' then '>'
      when '&apos;' then "'"
      when '&quot;' then '"'
    end
  end
end