termcolor 0.2.5 → 0.2.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/termcolor.rb +25 -5
  2. data/spec/termcolor_spec.rb +5 -2
  3. metadata +2 -2
@@ -7,16 +7,12 @@ require 'rexml/parsers/baseparser'
7
7
  require 'rexml/streamlistener'
8
8
 
9
9
  module TermColor
10
- VERSION = '0.2.5'
10
+ VERSION = '0.2.6'
11
11
  include REXML
12
12
 
13
13
  class ParseError < StandardError; end
14
14
 
15
15
  class << self
16
- def escape(text)
17
- CGI.escapeHTML(text)
18
- end
19
-
20
16
  def parse(text)
21
17
  listener = MyListener.new
22
18
  REXML::Parsers::StreamParser.new(text, listener).parse
@@ -24,6 +20,30 @@ module TermColor
24
20
  rescue REXML::ParseException => e
25
21
  raise ParseError, e
26
22
  end
23
+
24
+ def escape(text)
25
+ text.gsub(/[&<>'"]/) do | match |
26
+ case match
27
+ when '&' then '&amp;'
28
+ when '<' then '&lt;'
29
+ when '>' then '&gt;'
30
+ when "'" then '&apos;'
31
+ when '"' then '&quote;'
32
+ end
33
+ end
34
+ end
35
+
36
+ def unescape(text)
37
+ text.gsub(/&(lt|gt|amp|quote|apos);/) do | match |
38
+ case match
39
+ when '&amp;' then '&'
40
+ when '&lt;' then '<'
41
+ when '&gt;' then '>'
42
+ when '&apos;' then "'"
43
+ when '&quote;' then '"'
44
+ end
45
+ end
46
+ end
27
47
  end
28
48
 
29
49
  class MyListener
@@ -36,8 +36,11 @@ module TermColor
36
36
  end
37
37
 
38
38
  it 'should escape text' do
39
- text = 'a<foo>&</foo>a'
40
- TermColor.escape(text).should == "a&lt;foo&gt;&amp;&lt;/foo&gt;a"
39
+ TermColor.escape('<>&"\'').should == "&lt;&gt;&amp;&quote;&apos;"
40
+ end
41
+
42
+ it 'should unescape text' do
43
+ TermColor.unescape("&lt;&gt;&amp;&quote;&apos;").should == '<>&"\''
41
44
  end
42
45
 
43
46
  it 'should convert to escape sequence' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: termcolor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5
4
+ version: 0.2.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - jugyo
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-02-28 00:00:00 +09:00
12
+ date: 2009-03-02 00:00:00 +09:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency