termcolorlight 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 28b23d731c9a8c265527f0a6677348b9d1239a2e
4
+ data.tar.gz: 8cef95a72714128501ab9efe486b2b26de59b78b
5
+ SHA512:
6
+ metadata.gz: 33ca0d6f75a7f5805238ea67eaafa72093988bc41fb9b508e5702ad2431c507214d5d141ec29d680a26e97c9f9ad26de32fb7e7b9350a8a54a9f32a700772f36
7
+ data.tar.gz: 21d0b782675993fb7cb93af9353695d027b43fea17c4f6ee0011356e7c1abf582a733f14440c53e982557ee9447bf5df41dab658b7c9e6c6cc2b18b4044122be
data/.gitignore ADDED
@@ -0,0 +1,16 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ *.swp
16
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in termcolorlight.gemspec
4
+ gemspec
5
+
6
+ gem "rspec"
7
+ gem "rake"
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 whiteleaf7
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,56 @@
1
+ # TermColorLight
2
+
3
+ This gem is library that convert color tags (e.g. `<red>str</red>` ) to
4
+ ansicolor(vt100 escape sequence).
5
+
6
+ Lightweight version of TermColor.gem.
7
+ No use other gems, it is very simply.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'termcolorlight'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install termcolorlight
24
+
25
+ ## Usage
26
+
27
+ ```ruby
28
+ require "termcolorlight"
29
+
30
+ TermColorLight.parse("<red>strings</red>") # => \e[31mstrings\e[0m
31
+ "<red>strings</red>".termcolor # a ditto
32
+
33
+ str = "<div>container</div>"
34
+ "<bold><green>#{str.escape}</green></bold>" # => \e[1m\e[32m<div>container</div>\e[0m\e[1m\e[0m
35
+ ```
36
+
37
+ You can use the following tags.
38
+
39
+ ```
40
+ # forground tags
41
+ black, red, green, yellow, blue, magenta, cyan, white, gray
42
+
43
+ # background tags
44
+ on_black, on_red, on_green, on_yellow, on_blue, on_magenta, on_cyan, on_white, on_gray
45
+
46
+ # decorations
47
+ bold, dark, underline, underscore, blink, reverse, concealed
48
+ ```
49
+
50
+ ## Contributing
51
+
52
+ 1. Fork it ( https://github.com/whiteleaf7/termcolorlight/fork )
53
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
54
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
55
+ 4. Push to the branch (`git push origin my-new-feature`)
56
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,111 @@
1
+ # -*- coding: utf-8 -*-
2
+ #
3
+ # Copyright 2014 whiteleaf. All rights reserved.
4
+ #
5
+
6
+ require "strscan"
7
+
8
+ module TermColorLight
9
+ extend self
10
+
11
+ VERSION = "1.0.0"
12
+
13
+ TAGS = {
14
+ # foreground colors
15
+ "black" => "30", "red" => "31", "green" => "32", "yellow" => "33",
16
+ "blue" => "34", "magenta" => "35", "cyan" => "36", "white" => "37",
17
+ "gray" => "37",
18
+ # background colors
19
+ "on_black" => "40", "on_red" => "41", "on_green" => "42", "on_yellow" => "43",
20
+ "on_blue" => "44", "on_magenta" => "45", "on_cyan" => "46", "on_white" => "47",
21
+ "on_gray" => "47",
22
+ # decorations
23
+ "bold" => "1", "dark" => "2", "underline" => "4", "underscore" => "4", "blink" => "5",
24
+ "reverse" => "7", "concealed" => "8",
25
+ }
26
+
27
+ ENTITIES = Hash[*%w(& &amp; < &lt; > &gt; " &quot; ' &apos;)]
28
+
29
+ class ParseError < StandardError; end
30
+
31
+ def parse(str)
32
+ stack = []
33
+ current_tag = ""
34
+ ss = StringScanner.new(str.to_s)
35
+ buffer = ""
36
+ until ss.eos?
37
+ case
38
+ when ss.scan(/[^<]+/)
39
+ buffer.concat(ss.matched)
40
+ when ss.scan(/<([a-z_]+?)>/i)
41
+ tag_name = ss[1].downcase
42
+ tag_index = TAGS[tag_name]
43
+ unless tag_index
44
+ raise ParseError, "Unknown tag name (got '#{tag_name}')"
45
+ end
46
+ stack.push(tag_index)
47
+ buffer.concat(create_escape_sequence(tag_index))
48
+ when ss.scan(/<\/([a-z_]+?)>/i)
49
+ tag_name = ss[1].downcase
50
+ expect_tag_index = stack.pop
51
+ if TAGS[tag_name] != expect_tag_index
52
+ expect_tag_name = tags_select_by_index(expect_tag_index)
53
+ raise ParseError, "Missing end tag for '#{expect_tag_name}' (got '#{tag_name}')"
54
+ end
55
+ buffer.concat("\e[0m#{create_escape_sequence(stack)}")
56
+ end
57
+ end
58
+ unless stack.empty?
59
+ for_tag_name = tags_select_by_index(stack.pop)
60
+ raise ParseError, "Missing end tag for '#{for_tag_name}'"
61
+ end
62
+ TermColorLight.unescape(buffer)
63
+ end
64
+
65
+ def escape(str)
66
+ buf = str.to_s.dup
67
+ ENTITIES.each do |entity|
68
+ buf.gsub!(*entity)
69
+ end
70
+ buf
71
+ end
72
+
73
+ def unescape(str)
74
+ buf = str.to_s.dup
75
+ ENTITIES.invert.each do |entity|
76
+ buf.gsub!(*entity)
77
+ end
78
+ buf
79
+ end
80
+
81
+ def strip_tag(str)
82
+ TermColorLight.unescape(str.gsub(/<.+?>/, ""))
83
+ end
84
+
85
+ def create_escape_sequence(indexes) # :nodoc:
86
+ unless indexes.kind_of?(Array)
87
+ return "\e[#{indexes}m"
88
+ end
89
+ result = ""
90
+ indexes.each do |index|
91
+ result.concat("\e[#{index}m")
92
+ end
93
+ result
94
+ end
95
+
96
+ def tags_select_by_index(index) # :nodoc:
97
+ TAGS.select { |_, idx|
98
+ idx == index
99
+ }.keys[0]
100
+ end
101
+ end
102
+
103
+ class String
104
+ def termcolor
105
+ TermColorLight.parse(self)
106
+ end
107
+
108
+ def escape
109
+ TermColorLight.escape(self)
110
+ end
111
+ end
@@ -0,0 +1,267 @@
1
+ # -*- coding: utf-8 -*-
2
+ #
3
+ # Copyright 2014 whiteleaf. All rights reserved.
4
+ #
5
+
6
+ require "termcolorlight"
7
+
8
+ describe TermColorLight do
9
+ describe ".parse" do
10
+ context "nil" do
11
+ it { expect(TermColorLight.parse(nil)).to eq "" }
12
+ end
13
+
14
+ context "give non-string" do
15
+ it "should be string" do
16
+ expect(TermColorLight.parse(0)).to eq "0"
17
+ end
18
+ end
19
+
20
+ context "blank" do
21
+ it { expect(TermColorLight.parse("")).to eq "" }
22
+ end
23
+
24
+ # memo:
25
+ # 0:black, 1:red, 2:green, 3:yellow, 4:blue, 5:magenta, 6:cyan, 7:white
26
+
27
+ context "color tag" do
28
+ it "black" do
29
+ res = TermColorLight.parse("<black>hoge</black>")
30
+ expect(res).to eq "\e[30mhoge\e[0m"
31
+ end
32
+ it "red" do
33
+ res = TermColorLight.parse("<red>hoge</red>")
34
+ expect(res).to eq "\e[31mhoge\e[0m"
35
+ end
36
+ it "green" do
37
+ res = TermColorLight.parse("<green>hoge</green>")
38
+ expect(res).to eq "\e[32mhoge\e[0m"
39
+ end
40
+ it "yellow" do
41
+ res = TermColorLight.parse("<yellow>hoge</yellow>")
42
+ expect(res).to eq "\e[33mhoge\e[0m"
43
+ end
44
+ it "blue" do
45
+ res = TermColorLight.parse("<blue>hoge</blue>")
46
+ expect(res).to eq "\e[34mhoge\e[0m"
47
+ end
48
+ it "magenta" do
49
+ res = TermColorLight.parse("<magenta>hoge</magenta>")
50
+ expect(res).to eq "\e[35mhoge\e[0m"
51
+ end
52
+ it "cyan" do
53
+ res = TermColorLight.parse("<cyan>hoge</cyan>")
54
+ expect(res).to eq "\e[36mhoge\e[0m"
55
+ end
56
+ it "white" do
57
+ res = TermColorLight.parse("<white>hoge</white>")
58
+ expect(res).to eq "\e[37mhoge\e[0m"
59
+ end
60
+ it "gray is white" do
61
+ res = TermColorLight.parse("<gray>hoge</gray>")
62
+ expect(res).to eq "\e[37mhoge\e[0m"
63
+ end
64
+ end
65
+
66
+ context "background color tag" do
67
+ it "black" do
68
+ res = TermColorLight.parse("<on_black>hoge</on_black>")
69
+ expect(res).to eq "\e[40mhoge\e[0m"
70
+ end
71
+ it "red" do
72
+ res = TermColorLight.parse("<on_red>hoge</on_red>")
73
+ expect(res).to eq "\e[41mhoge\e[0m"
74
+ end
75
+ it "green" do
76
+ res = TermColorLight.parse("<on_green>hoge</on_green>")
77
+ expect(res).to eq "\e[42mhoge\e[0m"
78
+ end
79
+ it "yellow" do
80
+ res = TermColorLight.parse("<on_yellow>hoge</on_yellow>")
81
+ expect(res).to eq "\e[43mhoge\e[0m"
82
+ end
83
+ it "blue" do
84
+ res = TermColorLight.parse("<on_blue>hoge</on_blue>")
85
+ expect(res).to eq "\e[44mhoge\e[0m"
86
+ end
87
+ it "magenta" do
88
+ res = TermColorLight.parse("<on_magenta>hoge</on_magenta>")
89
+ expect(res).to eq "\e[45mhoge\e[0m"
90
+ end
91
+ it "cyan" do
92
+ res = TermColorLight.parse("<on_cyan>hoge</on_cyan>")
93
+ expect(res).to eq "\e[46mhoge\e[0m"
94
+ end
95
+ it "white" do
96
+ res = TermColorLight.parse("<on_white>hoge</on_white>")
97
+ expect(res).to eq "\e[47mhoge\e[0m"
98
+ end
99
+ it "gray is white" do
100
+ res = TermColorLight.parse("<on_gray>hoge</on_gray>")
101
+ expect(res).to eq "\e[47mhoge\e[0m"
102
+ end
103
+ end
104
+
105
+ context "decoration tag" do
106
+ context "underline" do
107
+ it do
108
+ res = TermColorLight.parse("<underline>hoge</underline>")
109
+ expect(res).to eq "\e[4mhoge\e[0m"
110
+ end
111
+
112
+ it do
113
+ res = TermColorLight.parse("<underline><red>hoge</red></underline>")
114
+ expect(res).to eq "\e[4m\e[31mhoge\e[0m\e[4m\e[0m"
115
+ end
116
+ end
117
+
118
+ context "bold" do
119
+ it do
120
+ res = TermColorLight.parse("<bold>hoge</bold>")
121
+ expect(res).to eq "\e[1mhoge\e[0m"
122
+ end
123
+
124
+ context "with color tag" do
125
+ context "when before bold" do
126
+ it do
127
+ res = TermColorLight.parse("<bold><red>hoge</red></bold>")
128
+ expect(res).to eq "\e[1m\e[31mhoge\e[0m\e[1m\e[0m"
129
+ end
130
+ end
131
+
132
+ context "when after bold" do
133
+ it do
134
+ res = TermColorLight.parse("<red><bold>hoge</bold></red>")
135
+ expect(res).to eq "\e[31m\e[1mhoge\e[0m\e[31m\e[0m"
136
+ end
137
+ end
138
+ end
139
+ end
140
+
141
+ context "reverse" do
142
+ it do
143
+ res = TermColorLight.parse("<reverse>hoge</reverse>")
144
+ expect(res).to eq "\e[7mhoge\e[0m"
145
+ end
146
+ end
147
+
148
+ context "blink" do
149
+ it do
150
+ expect(TermColorLight.parse("<blink>hoge</blink>")).to eq "\e[5mhoge\e[0m"
151
+ end
152
+ end
153
+
154
+ context "dark" do
155
+ it do
156
+ expect(TermColorLight.parse("<dark>hoge</dark>")).to eq "\e[2mhoge\e[0m"
157
+ end
158
+ end
159
+
160
+ context "concealed" do
161
+ it do
162
+ expect(TermColorLight.parse("<concealed>hoge</concealed>")).to eq "\e[8mhoge\e[0m"
163
+ end
164
+ end
165
+
166
+ context "mixed decorate" do
167
+ it do
168
+ res = TermColorLight.parse("abc<reverse>d<bold>e<red>f" \
169
+ "<underline>g</underline>h</red>i</bold></reverse>jk")
170
+ expect(res).to eq "abc\e[7m\d\e[1me\e[31mf\e[4mg\e[0m" \
171
+ "\e[7m\e[1m\e[31mh\e[0m\e[7m\e[1mi\e[0m\e[7m\e[0mjk"
172
+ end
173
+ end
174
+ end
175
+
176
+ context "ignore case" do
177
+ it do
178
+ expect(TermColorLight.parse("<RED>hoge</RED>")).to eq "\e[31mhoge\e[0m"
179
+ end
180
+ end
181
+
182
+ context "with escaped tag" do
183
+ it do
184
+ res = TermColorLight.parse("&lt;red&gt;hoge&lt;/red&gt;")
185
+ expect(res).to eq "<red>hoge</red>"
186
+ end
187
+ end
188
+
189
+ describe "abnormal" do
190
+ context "invalid tag" do
191
+ it "raise error" do
192
+ expect { TermColorLight.parse("<unknown>hoge</unknown>") }
193
+ .to raise_error(TermColorLight::ParseError, "Unknown tag name (got 'unknown')")
194
+ end
195
+ end
196
+
197
+ context "invalid background color" do
198
+ it "raise error" do
199
+ expect { TermColorLight.parse("<on_unknown>hoge</on_unknown>") }
200
+ .to raise_error(TermColorLight::ParseError, "Unknown tag name (got 'on_unknown')")
201
+ end
202
+ end
203
+
204
+ context "invalid tag order" do
205
+ it "raise error" do
206
+ expect { TermColorLight.parse("<bold><red>hoge</bold></red>") }
207
+ .to raise_error(TermColorLight::ParseError, "Missing end tag for 'red' (got 'bold')")
208
+ end
209
+ end
210
+
211
+ context "when unclose tag" do
212
+ it "raise error" do
213
+ expect { TermColorLight.parse("<red>hoge") }
214
+ .to raise_error(TermColorLight::ParseError, "Missing end tag for 'red'")
215
+ end
216
+ end
217
+ end
218
+ end
219
+
220
+ describe ".escape" do
221
+ it "should be escaped string" do
222
+ res = TermColorLight.escape("<red>&hoge</red>")
223
+ expect(res).to eq "&lt;red&gt;&amp;hoge&lt;/red&gt;"
224
+ end
225
+
226
+ context "give non-string" do
227
+ context "when nil" do
228
+ it "should be ''" do
229
+ expect(TermColorLight.escape(nil)).to eq ""
230
+ end
231
+ end
232
+ context "when integer" do
233
+ it "should be string" do
234
+ expect(TermColorLight.escape(0)).to eq "0"
235
+ end
236
+ end
237
+ end
238
+ end
239
+
240
+ describe ".unescape" do
241
+ it do
242
+ expect(TermColorLight.unescape("&lt;red&gt;&amp;hoge&apos;&quot;&lt;/red&gt;"))
243
+ .to eq %!<red>&hoge'"</red>!
244
+ end
245
+ end
246
+
247
+ describe ".strip_tag" do
248
+ it do
249
+ expect(TermColorLight.strip_tag("<red>&quot;&apos;foo&amp;bar &lt;&gt;</red>"))
250
+ .to eq %!"'foo&bar <>!
251
+ end
252
+ end
253
+ end
254
+
255
+ describe String do
256
+ context "#termcolor" do
257
+ it do
258
+ expect("<red>hoge</red>".termcolor).to eq "\e[31mhoge\e[0m"
259
+ end
260
+ end
261
+
262
+ context "#escape" do
263
+ it "should be escaped string" do
264
+ expect("<red>hoge</red>".escape).to eq "&lt;red&gt;hoge&lt;/red&gt;"
265
+ end
266
+ end
267
+ end
@@ -0,0 +1,30 @@
1
+ # -*- coding: utf-8 -*-
2
+ #
3
+ # Copyright 2014 whiteleaf. All rights reserved.
4
+ #
5
+
6
+ lib = File.expand_path('../lib', __FILE__)
7
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
8
+ require 'termcolorlight'
9
+
10
+ Gem::Specification.new do |spec|
11
+ spec.name = "termcolorlight"
12
+ spec.version = TermColorLight::VERSION
13
+ spec.authors = ["whiteleaf7"]
14
+ spec.email = ["2nd.leaf@gmail.com"]
15
+ spec.summary = "convert color tags to ansicolor library"
16
+ spec.description = <<-EOS
17
+ This gem is library that convert color tags (e.g. `<red>str</red>` ) to
18
+ ansicolor(vt100 escape sequence).
19
+
20
+ Lightweight version of TermColor.gem.
21
+ No use other gems, it is very simply.
22
+ EOS
23
+ spec.homepage = "http://github.com/whiteleaf7/termcolorlight"
24
+ spec.license = "MIT"
25
+
26
+ spec.files = `git ls-files -z`.split("\x0")
27
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
28
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
29
+ spec.require_paths = ["lib"]
30
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: termcolorlight
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - whiteleaf7
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-05 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: |
14
+ This gem is library that convert color tags (e.g. `<red>str</red>` ) to
15
+ ansicolor(vt100 escape sequence).
16
+
17
+ Lightweight version of TermColor.gem.
18
+ No use other gems, it is very simply.
19
+ email:
20
+ - 2nd.leaf@gmail.com
21
+ executables: []
22
+ extensions: []
23
+ extra_rdoc_files: []
24
+ files:
25
+ - .gitignore
26
+ - Gemfile
27
+ - LICENSE.txt
28
+ - README.md
29
+ - Rakefile
30
+ - lib/termcolorlight.rb
31
+ - spec/termcolorlight_spec.rb
32
+ - termcolorlight.gemspec
33
+ homepage: http://github.com/whiteleaf7/termcolorlight
34
+ licenses:
35
+ - MIT
36
+ metadata: {}
37
+ post_install_message:
38
+ rdoc_options: []
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ requirements: []
52
+ rubyforge_project:
53
+ rubygems_version: 2.4.1
54
+ signing_key:
55
+ specification_version: 4
56
+ summary: convert color tags to ansicolor library
57
+ test_files:
58
+ - spec/termcolorlight_spec.rb