termcolor 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
File without changes
data/README.rdoc ADDED
@@ -0,0 +1,52 @@
1
+ = termcolor
2
+
3
+ http://wiki.github.com/jugyo/termcolor
4
+
5
+ == DESCRIPTION:
6
+
7
+ Termcolor is a library for ANSII color formatting like HTML for output in terminal.
8
+
9
+ == FEATURES/PROBLEMS:
10
+
11
+ == SYNOPSIS:
12
+
13
+ TermColor.parse('<blue>Hello</blue> <red>World!</red>')
14
+ #=> "\e[34mHello\e[0m \e[31mWorld!\e[0m"
15
+
16
+ == REQUIREMENTS:
17
+
18
+ * highline
19
+
20
+ == INSTALL:
21
+
22
+ gem source -a http://gems.github.com
23
+ sudo gem install jugyo-termcolor
24
+
25
+ == TODO:
26
+
27
+ for windows...
28
+
29
+ == LICENSE:
30
+
31
+ (The MIT License)
32
+
33
+ Copyright (c) 2008-2009 jugyo
34
+
35
+ Permission is hereby granted, free of charge, to any person obtaining
36
+ a copy of this software and associated documentation files (the
37
+ 'Software'), to deal in the Software without restriction, including
38
+ without limitation the rights to use, copy, modify, merge, publish,
39
+ distribute, sublicense, and/or sell copies of the Software, and to
40
+ permit persons to whom the Software is furnished to do so, subject to
41
+ the following conditions:
42
+
43
+ The above copyright notice and this permission notice shall be
44
+ included in all copies or substantial portions of the Software.
45
+
46
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
47
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
48
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
49
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
50
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
51
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
52
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,43 @@
1
+ $:.unshift File.dirname(__FILE__) + '/lib/'
2
+ require 'termcolor'
3
+ require 'spec/rake/spectask'
4
+
5
+ desc 'run all specs'
6
+ Spec::Rake::SpecTask.new do |t|
7
+ t.spec_files = FileList['spec/**/*_spec.rb']
8
+ t.spec_opts = ['-c']
9
+ end
10
+
11
+ desc 'Generate gemspec'
12
+ task :gemspec do |t|
13
+ open('termcolor.gemspec', "wb" ) do |file|
14
+ file << <<-EOS
15
+ Gem::Specification.new do |s|
16
+ s.name = 'termcolor'
17
+ s.version = '#{TermColor::VERSION}'
18
+ s.summary = "Termcolor is a library for ANSII color formatting like HTML for output in terminal."
19
+ s.description = "Termcolor is a library for ANSII color formatting like HTML for output in terminal."
20
+ s.files = %w( #{Dir['lib/**/*.rb'].join(' ')}
21
+ #{Dir['spec/**/*.rb'].join(' ')}
22
+ #{Dir['examples/**/*.rb'].join(' ')}
23
+ README.rdoc
24
+ History.txt
25
+ Rakefile )
26
+ s.add_dependency("highline", ">= 1.5.0")
27
+ s.author = 'jugyo'
28
+ s.email = 'jugyo.org@gmail.com'
29
+ s.homepage = 'http://github.com/jugyo/termcolor'
30
+ s.rubyforge_project = 'termcolor'
31
+ s.has_rdoc = true
32
+ s.rdoc_options = ["--main", "README.rdoc", "--exclude", "spec"]
33
+ s.extra_rdoc_files = ["README.rdoc", "History.txt"]
34
+ end
35
+ EOS
36
+ end
37
+ puts "Generate gemspec"
38
+ end
39
+
40
+ desc 'Generate gem'
41
+ task :gem => :gemspec do |t|
42
+ system 'gem', 'build', 'termcolor.gemspec'
43
+ end
data/lib/termcolor.rb ADDED
@@ -0,0 +1,54 @@
1
+ require 'highline'
2
+ require 'cgi'
3
+ require 'rexml/parsers/streamparser'
4
+ require 'rexml/parsers/baseparser'
5
+ require 'rexml/streamlistener'
6
+
7
+ module TermColor
8
+ VERSION = '0.2.0'
9
+ include REXML
10
+
11
+ class ParseError < StandardError; end
12
+
13
+ class << self
14
+ def escape(text)
15
+ CGI.escapeHTML(text)
16
+ end
17
+
18
+ def parse(text)
19
+ listener = MyListener.new
20
+ REXML::Parsers::StreamParser.new(text, listener).parse
21
+ listener.result
22
+ rescue REXML::ParseException => e
23
+ raise ParseError, e
24
+ end
25
+ end
26
+
27
+ class MyListener
28
+ include REXML::StreamListener
29
+
30
+ attr_reader :result
31
+ def initialize
32
+ @result = ''
33
+ @tag_stack = []
34
+ end
35
+
36
+ def tag_start(name, attrs)
37
+ esc_seq = HighLine.const_get(name.upcase)
38
+ @result << esc_seq
39
+ @tag_stack.push(name)
40
+ rescue NameError
41
+ end
42
+
43
+ def text(text)
44
+ @result << CGI.unescapeHTML(text)
45
+ end
46
+
47
+ def tag_end(name)
48
+ @tag_stack.pop
49
+ @result << HighLine::CLEAR
50
+ @result << HighLine.const_get(@tag_stack[-1].upcase) unless @tag_stack.empty?
51
+ end
52
+
53
+ end
54
+ end
@@ -0,0 +1,4 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
4
+ require 'termcolor'
@@ -0,0 +1,38 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require File.dirname(__FILE__) + '/spec_helper'
4
+
5
+ module TermColor
6
+ describe TermColor do
7
+ before do
8
+ end
9
+
10
+ it 'should parse 1' do
11
+ text = TermColor.parse('aaa<red>aaaa<bold>foo</bold>bb<blue>bbbb</blue>bbb</red>ccc<on_yellow>ccccc</on_yellow>ccc')
12
+ puts text
13
+ text.should == "aaa\e[31maaaa\e[1mfoo\e[0m\e[31mbb\e[34mbbbb\e[0m\e[31mbbb\e[0mccc\e[43mccccc\e[0mccc"
14
+ end
15
+
16
+ it 'should parse 2' do
17
+ text = TermColor.parse('aa<blue>a<foo>aaa<red>aa</red>aaaa</foo>a</blue>aaa')
18
+ puts text
19
+ text.should == "aa\e[34maaaa\e[31maa\e[0m\e[34maaaa\e[0ma\e[0maaa"
20
+ end
21
+
22
+ it 'should parse 3' do
23
+ text = TermColor.parse('aa<blue>aaaaa&lt;aaa&quot;aaa&gt;aaa&amp;aaaaa</blue>aaa')
24
+ puts text
25
+ text.should == "aa\e[34maaaaa<aaa\"aaa>aaa&aaaaa\e[0maaa"
26
+ end
27
+
28
+ it 'should raise Error' do
29
+ lambda{ TermColor.parse('aaaaa<red>aaaaa</blue>aaaaa') }.should raise_error(TermColor::ParseError)
30
+ lambda{ TermColor.parse('aaaaa<red>aaaaaaaaaa') }.should_not raise_error(TermColor::ParseError)
31
+ end
32
+
33
+ it 'should escape text' do
34
+ text = 'a<foo>&</foo>a'
35
+ TermColor.escape(text).should == "a&lt;foo&gt;&amp;&lt;/foo&gt;a"
36
+ end
37
+ end
38
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: termcolor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - jugyo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-02-24 00:00:00 +09:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: highline
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.5.0
24
+ version:
25
+ description: Termcolor is a library for ANSII color formatting like HTML for output in terminal.
26
+ email: jugyo.org@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - README.rdoc
33
+ - History.txt
34
+ files:
35
+ - lib/termcolor.rb
36
+ - spec/spec_helper.rb
37
+ - spec/termcolor_spec.rb
38
+ - README.rdoc
39
+ - History.txt
40
+ - Rakefile
41
+ has_rdoc: true
42
+ homepage: http://github.com/jugyo/termcolor
43
+ post_install_message:
44
+ rdoc_options:
45
+ - --main
46
+ - README.rdoc
47
+ - --exclude
48
+ - spec
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ version:
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ requirements: []
64
+
65
+ rubyforge_project: termcolor
66
+ rubygems_version: 1.3.1
67
+ signing_key:
68
+ specification_version: 2
69
+ summary: Termcolor is a library for ANSII color formatting like HTML for output in terminal.
70
+ test_files: []
71
+