termcolor 1.2.1 → 1.2.2

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 (5) hide show
  1. data/README.rdoc +3 -16
  2. data/Rakefile +6 -5
  3. data/lib/termcolor.rb +25 -20
  4. data/spec/termcolor_spec.rb +18 -4
  5. metadata +39 -55
@@ -1,6 +1,6 @@
1
1
  = termcolor
2
2
 
3
- http://wiki.github.com/jugyo/termcolor
3
+ https://github.com/jugyo/termcolor
4
4
 
5
5
  == DESCRIPTION:
6
6
 
@@ -10,25 +10,12 @@ Termcolor is a library for ANSI color formatting like HTML for output in termina
10
10
 
11
11
  == SYNOPSIS:
12
12
 
13
- TermColor.parse('<blue>Hello</blue> <red>World!</red>')
13
+ '<blue>Hello</blue> <red>World!</red>'.termcolor
14
14
  #=> "\e[34mHello\e[0m \e[31mWorld!\e[0m"
15
15
 
16
- == REQUIREMENTS:
17
-
18
- * highline
19
-
20
16
  == INSTALL:
21
17
 
22
- sudo gem install termcolor
23
-
24
- or
25
-
26
- gem source -a http://gems.github.com
27
- sudo gem install jugyo-termcolor
28
-
29
- == TODO:
30
-
31
- for windows...
18
+ gem install termcolor
32
19
 
33
20
  == LICENSE:
34
21
 
data/Rakefile CHANGED
@@ -1,12 +1,13 @@
1
1
  # -*- coding: utf-8 -*-
2
2
  $:.unshift File.dirname(__FILE__) + '/lib/'
3
3
  require 'termcolor'
4
- require 'spec/rake/spectask'
4
+ require 'rspec/core'
5
+ require 'rspec/core/rake_task'
5
6
 
6
- desc 'run all specs'
7
- Spec::Rake::SpecTask.new do |t|
8
- t.spec_files = FileList['spec/**/*_spec.rb']
9
- t.spec_opts = ['-c']
7
+ task :default => :spec
8
+
9
+ RSpec::Core::RakeTask.new(:spec) do |spec|
10
+ spec.pattern = FileList['spec/**/*_spec.rb']
10
11
  end
11
12
 
12
13
  desc 'Generate gemspec'
@@ -7,7 +7,7 @@ require 'rexml/parsers/baseparser'
7
7
  require 'rexml/streamlistener'
8
8
 
9
9
  module TermColor
10
- VERSION = '1.2.1'
10
+ VERSION = '1.2.2'
11
11
  include REXML
12
12
 
13
13
  class << self
@@ -18,31 +18,36 @@ module TermColor
18
18
  end
19
19
 
20
20
  def escape(text)
21
- text.gsub(/[&<>'"]/) do | match |
22
- case match
23
- when '&' then '&amp;'
24
- when '<' then '&lt;'
25
- when '>' then '&gt;'
26
- when "'" then '&apos;'
27
- when '"' then '&quot;'
28
- end
29
- end
21
+ escape_or_unescape(:escape, text)
30
22
  end
31
23
 
32
24
  def unescape(text)
33
- text.gsub(/&(lt|gt|amp|quot|apos);/) do | match |
34
- case match
35
- when '&amp;' then '&'
36
- when '&lt;' then '<'
37
- when '&gt;' then '>'
38
- when '&apos;' then "'"
39
- when '&quot;' then '"'
40
- end
41
- end
25
+ escape_or_unescape(:unescape, text)
26
+ end
27
+
28
+ def escape_or_unescape(dir=:escape, text)
29
+ h = Hash[*%w(& &amp; < &lt; > &gt; ' &apos; " &quot;)]
30
+ h = h.invert if dir == :unescape
31
+ text.gsub(/(#{h.keys.join('|')})/){ h[$1] }
42
32
  end
33
+ private :escape_or_unescape
43
34
 
44
35
  def prepare_parse(text)
45
- text.gsub(/<(\/?)(\d+)>/, '<\1_\2>')
36
+ tag_separate text.gsub(/<(\/?)(\d+)>/, '<\1_\2>')
37
+ end
38
+
39
+ def tag_separate(text)
40
+ re = /<(\/*)([^\W_]+)(?:_(on_[^\W_]+))*(?:_with_([^\W_]+))*(?:_and_([^\W_]+))*>/
41
+ text.gsub(re) do
42
+ matchs = $~.captures
43
+ if matchs.shift.empty?
44
+ tag = ->t{ "<#{t}>" }
45
+ else
46
+ matchs.reverse!
47
+ tag = ->t{ "</#{t}>" }
48
+ end
49
+ matchs.compact.map { |word| tag[word] }.join
50
+ end
46
51
  end
47
52
 
48
53
  def test(*args)
@@ -24,19 +24,19 @@ module TermColor
24
24
  text.should == "aa\e[34maaaaa<aaa\"aaa>aaa&aaaaa\e[0maaa"
25
25
  end
26
26
 
27
- it 'should parse 3' do
27
+ it 'should parse 4' do
28
28
  text = TermColor.parse('aa<30>bbbbbbb<32>cccc<90>ddd</90>c</32>b</30>aaa')
29
29
  puts text
30
- text.should == "aa\e[30mbbbbbbb\e[32mcccc\e[90mddd\e[0m\e[32mc\e[0m\e[30mb\e[0maaa"
30
+ text.should == "aa\e[30mbbbbbbb\e[32mcccc\e[90mddd\e[0m\e[30m\e[32mc\e[0m\e[30mb\e[0maaa"
31
31
  end
32
32
 
33
- it 'should parse 4' do
33
+ it 'should parse 5' do
34
34
  text = TermColor.parse('aa<f123>bbbbbbb<b321>ccccc</b321>b</f123>aaa')
35
35
  puts text
36
36
  text.should == "aa\e[38;5;67mbbbbbbb\e[48;5;137mccccc\e[0m\e[38;5;67mb\e[0maaa"
37
37
  end
38
38
 
39
- it 'should parse 5' do
39
+ it 'should parse 6' do
40
40
  text = TermColor.parse('aa<f12>bbbbbbb<b6>ccccc</b6>b</f12>aaa')
41
41
  puts text
42
42
  text.should == "aa\e[38;5;244mbbbbbbb\e[48;5;238mccccc\e[0m\e[38;5;244mb\e[0maaa"
@@ -75,5 +75,19 @@ module TermColor
75
75
  it 'should do colorize' do
76
76
  TermColor.colorize('test', :green).should == "\e[32mtest\e[0m"
77
77
  end
78
+
79
+ it 'should make separate tags for combined-style tag' do
80
+ h = { "<red_on_yellow>hello, world</red_on_yellow>" =>
81
+ "<red><on_yellow>hello, world</on_yellow></red>",
82
+ "<green_with_bold>hello, world</green_with_bold>" =>
83
+ "<green><bold>hello, world</bold></green>",
84
+ "<blue_on_white_with_bold_and_underline>hello</blue_on_white_with_bold_and_underline>" =>
85
+ "<blue><on_white><bold><underline>hello</underline></bold></on_white></blue>",
86
+ "<black_on_white>hello</black_on_white>term<green_with_bold>color</green_with_bold>" =>
87
+ "<black><on_white>hello</on_white></black>term<green><bold>color</bold></green>" }
88
+ h.each_pair do |combined, separated|
89
+ TermColor.prepare_parse(combined).should == separated
90
+ end
91
+ end
78
92
  end
79
93
  end
metadata CHANGED
@@ -1,49 +1,41 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: termcolor
3
- version: !ruby/object:Gem::Version
4
- hash: 29
5
- prerelease: false
6
- segments:
7
- - 1
8
- - 2
9
- - 1
10
- version: 1.2.1
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.2.2
5
+ prerelease:
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - jugyo
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2010-08-26 00:00:00 +09:00
19
- default_executable:
20
- dependencies:
21
- - !ruby/object:Gem::Dependency
12
+ date: 2012-08-15 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
22
15
  name: highline
23
- prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
25
17
  none: false
26
- requirements:
27
- - - ">="
28
- - !ruby/object:Gem::Version
29
- hash: 3
30
- segments:
31
- - 1
32
- - 5
33
- - 0
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
34
21
  version: 1.5.0
35
22
  type: :runtime
36
- version_requirements: *id001
37
- description: "Termcolor is a library for ANSI color formatting like HTML for output in terminal. '<red>red</red>'.termcolor #=> \"\e[31mred\e[0m\""
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 1.5.0
30
+ description: ! "Termcolor is a library for ANSI color formatting like HTML for output
31
+ in terminal. '<red>red</red>'.termcolor #=> \"\e[31mred\e[0m\""
38
32
  email: jugyo.org@gmail.com
39
33
  executables: []
40
-
41
34
  extensions: []
42
-
43
- extra_rdoc_files:
35
+ extra_rdoc_files:
44
36
  - README.rdoc
45
37
  - History.txt
46
- files:
38
+ files:
47
39
  - lib/termcolor.rb
48
40
  - spec/spec_helper.rb
49
41
  - spec/termcolor_spec.rb
@@ -51,42 +43,34 @@ files:
51
43
  - README.rdoc
52
44
  - History.txt
53
45
  - Rakefile
54
- has_rdoc: true
55
46
  homepage: http://github.com/jugyo/termcolor
56
47
  licenses: []
57
-
58
48
  post_install_message:
59
- rdoc_options:
49
+ rdoc_options:
60
50
  - --main
61
51
  - README.rdoc
62
52
  - --exclude
63
53
  - spec
64
- require_paths:
54
+ require_paths:
65
55
  - lib
66
- required_ruby_version: !ruby/object:Gem::Requirement
56
+ required_ruby_version: !ruby/object:Gem::Requirement
67
57
  none: false
68
- requirements:
69
- - - ">="
70
- - !ruby/object:Gem::Version
71
- hash: 3
72
- segments:
73
- - 0
74
- version: "0"
75
- required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
63
  none: false
77
- requirements:
78
- - - ">="
79
- - !ruby/object:Gem::Version
80
- hash: 3
81
- segments:
82
- - 0
83
- version: "0"
64
+ requirements:
65
+ - - ! '>='
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
84
68
  requirements: []
85
-
86
69
  rubyforge_project: termcolor
87
- rubygems_version: 1.3.7
70
+ rubygems_version: 1.8.23
88
71
  signing_key:
89
72
  specification_version: 3
90
- summary: Termcolor is a library for ANSI color formatting like HTML for output in terminal.
73
+ summary: Termcolor is a library for ANSI color formatting like HTML for output in
74
+ terminal.
91
75
  test_files: []
92
-
76
+ has_rdoc: true