epitron-colorize 0.6.2 → 0.6.3

Sign up to get free protection for your applications and to get access to all the features.
data/lib/colorize.rb CHANGED
@@ -213,6 +213,10 @@ class String
213
213
  15 => :light_white,
214
214
  }
215
215
 
216
+ #
217
+ def valid_color?(string)
218
+ COLORS.include?(string.to_sym) or (string =~ /^\d+$/ and BBS_COLOR_TABLE.include?(string.to_i))
219
+ end
216
220
 
217
221
  #
218
222
  # Colorize a string that has "color tags".
@@ -228,19 +232,23 @@ class String
228
232
  def tagged_colors
229
233
  stack = []
230
234
 
235
+ # matchers for just the color part of the tag
231
236
  open_tag_re = /<([\w\d_]+)>/
232
237
  close_tag_re = /<\/([\w\d_]+)>/
233
- tokens = self.scan(/(<\/?[\w\d_]+>|[^<>]+)/).flatten
234
238
 
239
+ # split the string into tags and texts
240
+ tokens = self.split(/(<\/?[\w\d_]+>)/)
241
+ tokens.delete_if { |token| token.size == 0 }
242
+
235
243
  result = ""
236
244
 
237
245
  tokens.each do |token|
238
246
 
239
- if open_tag_re =~ token
247
+ if open_tag_re =~ token and valid_color?($1)
240
248
 
241
249
  stack.push $1
242
250
 
243
- elsif close_tag_re =~ token
251
+ elsif close_tag_re =~ token and valid_color?($1)
244
252
 
245
253
  # if this color is on the stack somwehere...
246
254
  if pos = stack.rindex($1)
@@ -0,0 +1,32 @@
1
+ require 'test_helper'
2
+
3
+ class TaggedColors < Test::Unit::TestCase
4
+
5
+ def test_display_tagged_colors
6
+ ## Visual inspection seemed the easiest way to test this. :)
7
+ puts
8
+ puts "##########################################################################"
9
+ puts "## VISUAL TEST. Make sure these all look okay!"
10
+ puts "##########################################################################"
11
+ puts
12
+ puts "* Testing various features of the tagged colorizer..."
13
+ puts
14
+ puts " <light_blue>I am light blue.".colorize
15
+ puts " <yellow>Yellow <green>Green <light_blue>Light blue".colorize
16
+ puts " <8>grey <13>light magenta <4>red".colorize
17
+ puts " <1>w<9>o<3>o<11>o<15>ooohoo<11>o<3>o<9>o<1>o".colorize
18
+ puts " <yellow>yellow <light_yellow>light_yellow</light_yellow> yellow</yellow> default.".colorize
19
+ puts " <yellow>yellow <light_yellow>light_yellow</yellow> light_yellow</light_yellow> default.".colorize
20
+ puts
21
+ puts "* Broken tags (shouldn't change colors, or raise errors):"
22
+ puts
23
+ puts " <light_green>Light green!</light_green> < > <Fsdfa> <69></69> <<light_red>Light Red!</light_red>>".colorize
24
+ end
25
+
26
+ def test_bad_tags
27
+ assert_raise RuntimeError do
28
+ puts "Closing unopened tag: </blue>".colorize
29
+ end
30
+ end
31
+
32
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: epitron-colorize
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.2
4
+ version: 0.6.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - fazibear
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2009-09-11 00:00:00 -07:00
13
+ date: 2009-09-18 00:00:00 -07:00
14
14
  default_executable:
15
15
  dependencies: []
16
16
 
@@ -54,5 +54,5 @@ signing_key:
54
54
  specification_version: 3
55
55
  summary: Colorize your console output (via extensions to the String class).
56
56
  test_files:
57
- - test/test_colorize.rb
57
+ - test/tagged_colors.rb
58
58
  - test/test_helper.rb
@@ -1,11 +0,0 @@
1
- require File.dirname(__FILE__) + '/test_helper.rb'
2
-
3
- class TestColorize < Test::Unit::TestCase
4
-
5
- def setup
6
- end
7
-
8
- def test_truth
9
- assert true
10
- end
11
- end