jugyo-termcolor 0.2.5 → 0.2.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. data/lib/termcolor.rb +25 -5
  2. data/spec/termcolor_spec.rb +5 -2
  3. metadata +1 -1
data/lib/termcolor.rb CHANGED
@@ -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: jugyo-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