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.
- data/lib/termcolor.rb +25 -5
- data/spec/termcolor_spec.rb +5 -2
- 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.
|
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 '&'
|
28
|
+
when '<' then '<'
|
29
|
+
when '>' then '>'
|
30
|
+
when "'" then '''
|
31
|
+
when '"' then '"e;'
|
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 '&' then '&'
|
40
|
+
when '<' then '<'
|
41
|
+
when '>' then '>'
|
42
|
+
when ''' then "'"
|
43
|
+
when '"e;' then '"'
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
27
47
|
end
|
28
48
|
|
29
49
|
class MyListener
|
data/spec/termcolor_spec.rb
CHANGED
@@ -36,8 +36,11 @@ module TermColor
|
|
36
36
|
end
|
37
37
|
|
38
38
|
it 'should escape text' do
|
39
|
-
|
40
|
-
|
39
|
+
TermColor.escape('<>&"\'').should == "<>&"e;'"
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'should unescape text' do
|
43
|
+
TermColor.unescape("<>&"e;'").should == '<>&"\''
|
41
44
|
end
|
42
45
|
|
43
46
|
it 'should convert to escape sequence' do
|