termcolorlight 1.0.1 → 1.1.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ffa13ca897e6881bb281555422245409cfc950ca
4
- data.tar.gz: 8e182856b0a394e7481238d0ba6313df8f889321
3
+ metadata.gz: ac7c7270e7a4af913ceb1bc407d1ab29218a981a
4
+ data.tar.gz: ef1686b75a7a44bede28fe31e9445c5b2cb28b48
5
5
  SHA512:
6
- metadata.gz: 0f123d1175bef301adf5e07f8b85d8fdef1e517a748a392b63ec27a41a017553d213db30fca74c995c90a76d496f550a3a41f8a69f144b6553f9ae19839203b0
7
- data.tar.gz: a36cb8766a5c4c014902f2ba377b072a915701be0f77ab8fb5fe1e5a2f9ff89946b91c6b3eb365c3623a36dcc58126afb73c5b175a419122709e35ba1b7927df
6
+ metadata.gz: a7edf680a26cc6dd614573001e6bf4b247649ab8c9fe9de64483a97621d4bf13c2e1a95703642257588ac3076cc5a4de05453da0d91bf7cc321037a1c177d547
7
+ data.tar.gz: 7b8207e7e7041eca074828ebb34be141a01e05cce4c2bdb35473d5a7eaa2e2a6d4cc5327340d11d92ad444a9f039ae585c4e68b4d6250d18fee65e447d7f3c01
data/.gitignore CHANGED
@@ -14,3 +14,5 @@
14
14
  mkmf.log
15
15
  *.swp
16
16
  *.gem
17
+ .DS_Store
18
+
data/README.md CHANGED
@@ -29,9 +29,18 @@ TermColorLight.parse("<red>strings</red>") # => \e[31mstrings\e[0m
29
29
  "<red>strings</red>".termcolor # a ditto
30
30
 
31
31
  str = "<div>container</div>"
32
- "<bold><green>#{str.escape}</green></bold>" # => \e[1m\e[32m<div>container</div>\e[0m\e[1m\e[0m
32
+ "<bold><green>#{str.escape}</green></bold>".termcolor # => \e[1m\e[32m<div>container</div>\e[0m\e[1m\e[0m
33
33
  ```
34
34
 
35
+ ```ruby
36
+ require "termcolorlight/html"
37
+ str = "<div>container</div>"
38
+ TermColorLight.to_html("<bold><green>#{str.escape}</green></bold>")
39
+ # => <span style="font-weight:bold"><span style="color:lime">&lt;div&gt;container&lt;/div&gt;</span></span>
40
+ ```
41
+
42
+ ![HTML ScreenCapture](html_ss.png)
43
+
35
44
  You can use the following tags.
36
45
 
37
46
  ```
@@ -94,6 +103,13 @@ TermColorLight.parse 0.850000 0.000000 0.850000 ( 0.853439)
94
103
 
95
104
  Oops...
96
105
 
106
+ ## Changelogs
107
+
108
+ ### version 1.1.0 (2014/10/03)
109
+
110
+ + add a method **TermColorLight.to_html**
111
+ - Perfect emuration of terminal
112
+
97
113
  ## Contributing
98
114
 
99
115
  1. Fork it ( https://github.com/whiteleaf7/termcolorlight/fork )
data/html_ss.png ADDED
Binary file
@@ -8,7 +8,7 @@ require "strscan"
8
8
  module TermColorLight
9
9
  module_function
10
10
 
11
- VERSION = "1.0.1"
11
+ VERSION = "1.1.0"
12
12
 
13
13
  TAGS = {
14
14
  # foreground colors
@@ -30,7 +30,6 @@ module TermColorLight
30
30
 
31
31
  def parse(str)
32
32
  stack = []
33
- current_tag = ""
34
33
  ss = StringScanner.new(str.to_s)
35
34
  buffer = ""
36
35
  until ss.eos?
@@ -78,8 +77,10 @@ module TermColorLight
78
77
  buf
79
78
  end
80
79
 
81
- def strip_tag(str)
82
- TermColorLight.unescape(str.gsub(/<.+?>/, ""))
80
+ def strip_tag(str, do_unescape = true)
81
+ result = str.gsub(/<.+?>/, "")
82
+ result = TermColorLight.unescape(result) if do_unescape
83
+ result
83
84
  end
84
85
 
85
86
  def create_escape_sequence(indexes) # :nodoc:
@@ -0,0 +1,176 @@
1
+ # -*- coding: utf-8 -*-
2
+ #
3
+ # Copyright 2014 whiteleaf. All rights reserved.
4
+ #
5
+
6
+ require_relative "../termcolorlight"
7
+
8
+ #
9
+ # TermColorLight.to_html 実装用エクステンション
10
+ #
11
+ module TermColorLight
12
+ module_function
13
+
14
+ # 色定義
15
+ # フォーマット: "name" => ["暗い色", "明るい色"]
16
+ @@colors = {
17
+ "black" => %w(black #888), "white" => %w(#bbb white),
18
+ "red" => %w(indianred red), "green" => %w(green lime),
19
+ "yellow" => %w(goldenrod yellow), "blue" => %w(#33c #33f),
20
+ "magenta" => %w(darkmagenta magenta), "cyan" => %w(darkcyan cyan),
21
+ "fg_default" => %w(white white),
22
+ "bg_default" => %w(#333 #333)
23
+ }
24
+ @@decoration_styles = {
25
+ "underline" => "text-decoration:underline",
26
+ "underscore" => "text-decoration:underline",
27
+ "bold" => "font-weight:bold",
28
+ "blink" => "text-decoration:blink",
29
+ "concealed" => "visibility:hidden",
30
+ }
31
+
32
+ def set_colors(hash)
33
+ @@colors.merge!(hash)
34
+ end
35
+
36
+ def set_styles(hash)
37
+ @@decoration_styles.merge!(hash)
38
+ end
39
+
40
+ def default_foreground_color=(color)
41
+ @@colors["fg_default"] = [color, color]
42
+ end
43
+
44
+ def default_background_color=(color)
45
+ @@colors["bg_default"] = [color, color]
46
+ end
47
+
48
+ def to_html(str)
49
+ @@before_fg_color = @@before_bg_color = nil
50
+ ss = StringScanner.new(str.to_s)
51
+ result = parse_for_html(ss)
52
+ end
53
+
54
+ def parse_for_html(ss, fgcolor = "fg_default", bgcolor = "bg_default",
55
+ intensity = false, reverse = false, parent = nil)
56
+ buffer = ""
57
+ until ss.eos?
58
+ case
59
+ when ss.scan(/[^<]+/)
60
+ buffer.concat(ss.matched)
61
+ when ss.scan(/<([a-z_]+?)>/i)
62
+ tag = ss[1].downcase
63
+ unless valid_tag?(tag)
64
+ raise ParseError, "Unknown tag name (got '#{tag}')"
65
+ end
66
+ tmp_fg = fgcolor
67
+ tmp_bg = bgcolor
68
+ tmp_intensity = intensity
69
+ tmp_reverse = reverse
70
+ influence_of_color = false
71
+ styles = []
72
+ case
73
+ when color_tag?(tag)
74
+ tmp_fg = tag
75
+ influence_of_color = true
76
+ when tag =~ /^on_(.+)$/
77
+ tmp_bg = $1
78
+ unless valid_tag?(tmp_bg)
79
+ raise ParseError, "Unknown tag name (got '#{tag}')"
80
+ end
81
+ influence_of_color = true
82
+ when tag == "bold"
83
+ tmp_intensity = true
84
+ influence_of_color = true
85
+ when tag == "reverse"
86
+ tmp_reverse = true
87
+ influence_of_color = true
88
+ end
89
+ if influence_of_color
90
+ styles += build_color(tmp_fg, tmp_bg, tmp_intensity, tmp_reverse)
91
+ end
92
+ decoration = @@decoration_styles[tag]
93
+ styles << decoration if decoration
94
+ buffer.concat("<span style=\"#{styles.join(";")}\">")
95
+ buffer.concat(parse_for_html(ss, tmp_fg, tmp_bg, tmp_intensity, tmp_reverse, tag))
96
+ when ss.scan(/<\/([a-z_]+?)>/i)
97
+ tag = ss[1].downcase
98
+ if tag != parent
99
+ raise ParseError, "Missing end tag for '#{parent}' (got '#{tag}')"
100
+ end
101
+ return buffer.concat("</span>")
102
+ end
103
+ end
104
+ if parent
105
+ raise ParseError, "Missing end tag for '#{parent}'"
106
+ end
107
+ buffer
108
+ end
109
+
110
+ def valid_tag?(tag)
111
+ TAGS.include?(tag)
112
+ end
113
+
114
+ def color_tag?(tag)
115
+ @@colors.include?(tag)
116
+ end
117
+
118
+ def influence_of_color?(tag)
119
+ TAGS_INFLUENCE_OF_COLOR.include?(tag)
120
+ end
121
+
122
+ def build_color(fgcolor, bgcolor, intensity, reverse)
123
+ style = []
124
+ if reverse
125
+ fg_intensity = 0
126
+ bg_intensity = intensity ? 1 : 0
127
+ tmp = fgcolor
128
+ fgcolor = bgcolor
129
+ bgcolor = tmp
130
+ else
131
+ fg_intensity = intensity ? 1 : 0
132
+ bg_intensity = 0
133
+ end
134
+ if fgcolor != "fg_default"
135
+ color = @@colors[fgcolor][fg_intensity]
136
+ if @@before_fg_color != color
137
+ style << "color:#{color}"
138
+ @@before_fg_color = color
139
+ end
140
+ end
141
+ if bgcolor != "bg_default"
142
+ color = @@colors[bgcolor][bg_intensity]
143
+ if @@before_bg_color != color
144
+ style << "background:#{color}"
145
+ @@before_bg_color = color
146
+ end
147
+ end
148
+ style
149
+ end
150
+
151
+ def output_test_sheets
152
+ puts <<-HTML
153
+ <html><head>
154
+ <style>body{color:#{@@colors["fg_default"][0]};background:#{@@colors["bg_default"][0]}}</style>
155
+ </head><body><table>
156
+ HTML
157
+ @@colors.each_key do |color|
158
+ next if color =~ /default/
159
+ puts "<tr><td>"
160
+ puts to_html("<#{color}>#{color}</#{color}>")
161
+ puts "</td><td>"
162
+ puts to_html("<bold><#{color}>#{color}</#{color}></bold>")
163
+ puts "</td><td>"
164
+ puts to_html("<on_#{color}>on_#{color}</on_#{color}>")
165
+ puts "</td><td>"
166
+ puts to_html("<reverse><bold><#{color}>#{color}</#{color}></bold></reverse>")
167
+ puts "</td></tr>"
168
+ end
169
+ puts "</table></body></html>"
170
+ end
171
+ end
172
+
173
+ if $0 == __FILE__
174
+ TermColorLight.output_test_sheets
175
+ end
176
+
@@ -0,0 +1,170 @@
1
+ # -*- coding: utf-8 -*-
2
+ #
3
+ # Copyright 2014 whiteleaf. All rights reserved.
4
+ #
5
+
6
+ require "termcolorlight/html"
7
+
8
+ describe TermColorLight do
9
+ describe ".to_html" do
10
+ context "give color tag" do
11
+ it do
12
+ expect(TermColorLight.to_html("<red>red</red>"))
13
+ .to eq '<span style="color:indianred">red</span>'
14
+ end
15
+
16
+ it "white is not white" do
17
+ expect(TermColorLight.to_html("<white>white?</white>"))
18
+ .to eq '<span style="color:#bbb">white?</span>'
19
+ end
20
+ end
21
+
22
+ context "give bold tag" do
23
+ it do
24
+ expect(TermColorLight.to_html("<bold>bold</bold>"))
25
+ .to eq '<span style="font-weight:bold">bold</span>'
26
+ end
27
+
28
+ it "should be bold when double bold" do
29
+ expect(TermColorLight.to_html("<bold><bold>bold</bold>bold</bold>"))
30
+ .to eq '<span style="font-weight:bold"><span style="font-weight:bold">bold</span>bold</span>'
31
+ end
32
+ end
33
+
34
+ context "give bold and color tag" do
35
+ it do
36
+ expect(TermColorLight.to_html("<bold><red>str</red>bold</bold>"))
37
+ .to eq '<span style="font-weight:bold"><span style="color:red">str</span>bold</span>'
38
+ end
39
+
40
+ it do
41
+ expect(TermColorLight.to_html("<red><bold>BOLD RED</bold> NORMAL RED</red>"))
42
+ .to eq '<span style="color:indianred"><span style="color:red;font-weight:bold">BOLD RED</span> NORMAL RED</span>'
43
+ end
44
+ end
45
+
46
+ context "give underline" do
47
+ it do
48
+ expect(TermColorLight.to_html("<underline>str</underline>"))
49
+ .to eq '<span style="text-decoration:underline">str</span>'
50
+ end
51
+ end
52
+
53
+ context "give background color" do
54
+ it do
55
+ expect(TermColorLight.to_html("<on_red>str</on_red>"))
56
+ .to eq '<span style="background:indianred">str</span>'
57
+ end
58
+
59
+ it "with bold" do
60
+ expect(TermColorLight.to_html("<bold><on_green>str</on_green></bold>"))
61
+ .to eq '<span style="font-weight:bold"><span style="background:green">str</span></span>'
62
+ end
63
+
64
+ it "with bold" do
65
+ expect(TermColorLight.to_html("<on_green><bold>str</bold> non bold</on_green>"))
66
+ .to eq '<span style="background:green"><span style="font-weight:bold">str</span> non bold</span>'
67
+ end
68
+
69
+ it "with color tag" do
70
+ expect(TermColorLight.to_html("<on_cyan>bgcyan <yellow>yellow</yellow> bgcyan</on_cyan>"))
71
+ .to eq '<span style="background:darkcyan">bgcyan <span style="color:goldenrod">yellow</span> bgcyan</span>'
72
+ end
73
+ end
74
+
75
+ context "give reverse" do
76
+ it do
77
+ expect(TermColorLight.to_html("<reverse>str</reverse>"))
78
+ .to eq '<span style="color:#333;background:white">str</span>'
79
+ end
80
+
81
+ it "do nothing when double reverse" do
82
+ expect(TermColorLight.to_html("<reverse><reverse>foo</reverse>bar</reverse>"))
83
+ .to eq '<span style="color:#333;background:white"><span style="">foo</span>bar</span>'
84
+ end
85
+
86
+ context "with color tag" do
87
+ it do
88
+ expect(TermColorLight.to_html("<reverse><white>str</white></reverse>"))
89
+ .to eq '<span style="color:#333;background:white"><span style="background:#bbb">str</span></span>'
90
+ end
91
+
92
+ it do
93
+ expect(TermColorLight.to_html("<reverse><blue>str</blue> foo</reverse>"))
94
+ .to eq '<span style="color:#333;background:white"><span style="background:#33c">str</span> foo</span>'
95
+ end
96
+
97
+ it do
98
+ expect(TermColorLight.to_html("<blue><reverse>str</reverse></blue>"))
99
+ .to eq '<span style="color:#33c"><span style="color:#333;background:#33c">str</span></span>'
100
+ end
101
+ end
102
+
103
+ context "with background tag" do
104
+ it do
105
+ expect(TermColorLight.to_html("<reverse><on_red>str</on_red></reverse>"))
106
+ .to eq '<span style="color:#333;background:white"><span style="color:indianred">str</span></span>'
107
+ end
108
+ end
109
+
110
+ context "with bold tag" do
111
+ it do
112
+ expect(TermColorLight.to_html("<reverse><bold>str</bold></reverse>"))
113
+ .to eq '<span style="color:#333;background:white"><span style="font-weight:bold">str</span></span>'
114
+ end
115
+
116
+ it do
117
+ expect(TermColorLight.to_html("<reverse><bold><red>str</red></bold></reverse>"))
118
+ .to eq '<span style="color:#333;background:white"><span style="font-weight:bold"><span style="background:red">str</span></span></span>'
119
+ end
120
+
121
+ it do
122
+ expect(TermColorLight.to_html("<reverse><red><bold>str</bold></red></reverse>"))
123
+ .to eq '<span style="color:#333;background:white"><span style="background:indianred"><span style="background:red;font-weight:bold">str</span></span></span>'
124
+ end
125
+
126
+ it do
127
+ expect(TermColorLight.to_html("<bold><green><reverse>str</reverse></green></bold>"))
128
+ .to eq '<span style="font-weight:bold"><span style="color:lime"><span style="color:#333;background:lime">str</span></span></span>'
129
+ end
130
+ end
131
+ end
132
+
133
+ context "give entities" do
134
+ it "should be keep entities" do
135
+ expect(TermColorLight.to_html("&lt;&gt;&amp;%apos;&nbsp;")).to eq "&lt;&gt;&amp;%apos;&nbsp;"
136
+ end
137
+ end
138
+
139
+ describe "abnormal" do
140
+ context "invalid tag" do
141
+ it "raise error" do
142
+ expect { TermColorLight.parse("<unknown>hoge</unknown>") }
143
+ .to raise_error(TermColorLight::ParseError, "Unknown tag name (got 'unknown')")
144
+ end
145
+ end
146
+
147
+ context "invalid background color" do
148
+ it "raise error" do
149
+ expect { TermColorLight.parse("<on_unknown>hoge</on_unknown>") }
150
+ .to raise_error(TermColorLight::ParseError, "Unknown tag name (got 'on_unknown')")
151
+ end
152
+ end
153
+
154
+ context "invalid tag order" do
155
+ it "raise error" do
156
+ expect { TermColorLight.parse("<bold><red>hoge</bold></red>") }
157
+ .to raise_error(TermColorLight::ParseError, "Missing end tag for 'red' (got 'bold')")
158
+ end
159
+ end
160
+
161
+ context "when unclose tag" do
162
+ it "raise error" do
163
+ expect { TermColorLight.parse("<red>hoge") }
164
+ .to raise_error(TermColorLight::ParseError, "Missing end tag for 'red'")
165
+ end
166
+ end
167
+ end
168
+ end
169
+ end
170
+
@@ -245,10 +245,15 @@ describe TermColorLight do
245
245
  end
246
246
 
247
247
  describe ".strip_tag" do
248
- it do
248
+ it "should be strop tag and unescaped entity" do
249
249
  expect(TermColorLight.strip_tag("<red>&quot;&apos;foo&amp;bar &lt;&gt;</red>"))
250
250
  .to eq %!"'foo&bar <>!
251
251
  end
252
+
253
+ it "shoul be strip tag and NOT unescaped entity" do
254
+ expect(TermColorLight.strip_tag("<red>&quot;&apos;foo&amp;bar &lt;&gt;</red>", false))
255
+ .to eq "&quot;&apos;foo&amp;bar &lt;&gt;"
256
+ end
252
257
  end
253
258
  end
254
259
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: termcolorlight
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - whiteleaf7
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-10 00:00:00.000000000 Z
11
+ date: 2014-10-02 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |
14
14
  This gem is library that convert color tags (e.g. <red>str</red>) to
@@ -26,7 +26,10 @@ files:
26
26
  - README.md
27
27
  - Rakefile
28
28
  - benchmark.rb
29
+ - html_ss.png
29
30
  - lib/termcolorlight.rb
31
+ - lib/termcolorlight/html.rb
32
+ - spec/termcolorlight_html_spec.rb
30
33
  - spec/termcolorlight_spec.rb
31
34
  - termcolorlight.gemspec
32
35
  homepage: http://github.com/whiteleaf7/termcolorlight
@@ -54,4 +57,5 @@ signing_key:
54
57
  specification_version: 4
55
58
  summary: convert color tags to ansicolor library
56
59
  test_files:
60
+ - spec/termcolorlight_html_spec.rb
57
61
  - spec/termcolorlight_spec.rb