jugyo-termcolor 0.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.
data/History.txt ADDED
File without changes
data/README.rdoc ADDED
@@ -0,0 +1,48 @@
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
+
12
+ == SYNOPSIS:
13
+
14
+ == REQUIREMENTS:
15
+
16
+ * highline
17
+
18
+ == INSTALL:
19
+
20
+ gem source -a http://gems.github.com
21
+ sudo gem install jugyo-termcolor
22
+
23
+ == TODO:
24
+
25
+ == LICENSE:
26
+
27
+ (The MIT License)
28
+
29
+ Copyright (c) 2008-2009 jugyo
30
+
31
+ Permission is hereby granted, free of charge, to any person obtaining
32
+ a copy of this software and associated documentation files (the
33
+ 'Software'), to deal in the Software without restriction, including
34
+ without limitation the rights to use, copy, modify, merge, publish,
35
+ distribute, sublicense, and/or sell copies of the Software, and to
36
+ permit persons to whom the Software is furnished to do so, subject to
37
+ the following conditions:
38
+
39
+ The above copyright notice and this permission notice shall be
40
+ included in all copies or substantial portions of the Software.
41
+
42
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
43
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
44
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
45
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
46
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
47
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
48
+ 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,55 @@
1
+ require 'highline'
2
+ require 'cgi'
3
+ require 'rexml/parsers/streamparser'
4
+ require 'rexml/parsers/baseparser'
5
+ require 'rexml/streamlistener'
6
+
7
+ class TermColor
8
+ VERSION = '0.1.0'
9
+ include REXML
10
+
11
+ class ParseError < StandardError; end
12
+
13
+ class << self
14
+ @highline = HighLine.new
15
+ def print(text)
16
+ print parse(text)
17
+ end
18
+
19
+ def parse(text)
20
+ listener = MyListener.new
21
+ REXML::Parsers::StreamParser.new(text, listener).parse
22
+ listener.result
23
+ rescue REXML::ParseException => e
24
+ raise ParseError, e
25
+ end
26
+ end
27
+
28
+ class MyListener
29
+ include REXML::StreamListener
30
+
31
+ attr_reader :result
32
+ def initialize
33
+ @result = ''
34
+ @tag_stack = []
35
+ end
36
+
37
+ def tag_start(name, attrs)
38
+ esc_seq = HighLine.const_get(name.upcase)
39
+ @result << esc_seq
40
+ @tag_stack.push(name)
41
+ rescue NameError
42
+ end
43
+
44
+ def text(text)
45
+ @result << CGI.unescapeHTML(text)
46
+ end
47
+
48
+ def tag_end(name)
49
+ @tag_stack.pop
50
+ @result << HighLine::CLEAR
51
+ @result << HighLine.const_get(@tag_stack[-1].upcase) unless @tag_stack.empty?
52
+ end
53
+
54
+ end
55
+ end
@@ -0,0 +1,4 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
4
+ require 'termcolor'
@@ -0,0 +1,33 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require File.dirname(__FILE__) + '/spec_helper'
4
+
5
+ class 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
+ end
33
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jugyo-termcolor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - jugyo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-02-13 00:00:00 -08: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.2.0
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
+