ansitags 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +5 -0
- data/LICENSE +20 -0
- data/README.rdoc +33 -0
- data/Rakefile +49 -0
- data/VERSION +1 -0
- data/ansitags.gemspec +53 -0
- data/lib/ansitags.rb +91 -0
- data/spec/ansitags_spec.rb +85 -0
- data/spec/spec_helper.rb +9 -0
- metadata +75 -0
data/.document
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 David Trasbo
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
= ANSI Tags
|
2
|
+
|
3
|
+
Converts ANSI codes to HTML tags.
|
4
|
+
|
5
|
+
== Installation
|
6
|
+
|
7
|
+
sudo gem install thisisdato-ansitags --source http://gems.github.com/
|
8
|
+
|
9
|
+
== Usage
|
10
|
+
|
11
|
+
"\e[92m\e[1mgreen bold text\e[0m".ansi_to_html
|
12
|
+
=> "<pre><span style="color: #00FF00"><span style="font-weight: bold;">green bold text</span></span></pre>"
|
13
|
+
|
14
|
+
That's basically it. ANSI Tags is capable of converting nearly all the ANSI
|
15
|
+
codes mentioned on Wikipedia[http://en.wikipedia.org/wiki/ANSI_escape_code]
|
16
|
+
under "SGR (Select Graphic Rendition) parameters" except "Intensity: Faint",
|
17
|
+
Blink: Slow", "Blink: Rapid", "Underline: Double", "Blink: off", and "Image:
|
18
|
+
Positive". But you won't need any of them, so that's fine.
|
19
|
+
|
20
|
+
== Contribution
|
21
|
+
|
22
|
+
1. Fork the project.
|
23
|
+
2. Make your feature addition or bug fix.
|
24
|
+
3. Add tests for it. This is important so I don't break it in a
|
25
|
+
future version unintentionally.
|
26
|
+
4. Commit, do not mess with rakefile, version, or history.
|
27
|
+
(if you want to have your own version, that is fine but
|
28
|
+
bump version in a commit by itself I can ignore when I pull)
|
29
|
+
5. Send me a pull request. Bonus points for topic branches.
|
30
|
+
|
31
|
+
== Copyright
|
32
|
+
|
33
|
+
Copyright (c) 2009 David Trasbo. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "ansitags"
|
8
|
+
gem.summary = %Q{Converts ANSI codes to HTML tags}
|
9
|
+
gem.description = %Q{Converts ANSI codes to HTML tags}
|
10
|
+
gem.email = "thisisdato@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/thisisdato/ansitags"
|
12
|
+
gem.authors = ["David Trasbo"]
|
13
|
+
gem.add_development_dependency "rspec"
|
14
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
|
+
end
|
16
|
+
Jeweler::GemcutterTasks.new
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'spec/rake/spectask'
|
22
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
23
|
+
spec.libs << 'lib' << 'spec'
|
24
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
25
|
+
end
|
26
|
+
|
27
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
28
|
+
spec.libs << 'lib' << 'spec'
|
29
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
30
|
+
spec.rcov = true
|
31
|
+
end
|
32
|
+
|
33
|
+
task :spec => :check_dependencies
|
34
|
+
|
35
|
+
task :default => :spec
|
36
|
+
|
37
|
+
require 'rake/rdoctask'
|
38
|
+
Rake::RDocTask.new do |rdoc|
|
39
|
+
if File.exist?('VERSION')
|
40
|
+
version = File.read('VERSION')
|
41
|
+
else
|
42
|
+
version = ""
|
43
|
+
end
|
44
|
+
|
45
|
+
rdoc.rdoc_dir = 'rdoc'
|
46
|
+
rdoc.title = "ansitags #{version}"
|
47
|
+
rdoc.rdoc_files.include('README*')
|
48
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
49
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/ansitags.gemspec
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{ansitags}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["David Trasbo"]
|
12
|
+
s.date = %q{2009-10-01}
|
13
|
+
s.description = %q{Converts ANSI codes to HTML tags}
|
14
|
+
s.email = %q{thisisdato@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"LICENSE",
|
23
|
+
"README.rdoc",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"ansitags.gemspec",
|
27
|
+
"lib/ansitags.rb",
|
28
|
+
"spec/ansitags_spec.rb",
|
29
|
+
"spec/spec_helper.rb"
|
30
|
+
]
|
31
|
+
s.homepage = %q{http://github.com/thisisdato/ansitags}
|
32
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
33
|
+
s.require_paths = ["lib"]
|
34
|
+
s.rubygems_version = %q{1.3.5}
|
35
|
+
s.summary = %q{Converts ANSI codes to HTML tags}
|
36
|
+
s.test_files = [
|
37
|
+
"spec/spec_helper.rb",
|
38
|
+
"spec/ansitags_spec.rb"
|
39
|
+
]
|
40
|
+
|
41
|
+
if s.respond_to? :specification_version then
|
42
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
43
|
+
s.specification_version = 3
|
44
|
+
|
45
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
46
|
+
s.add_development_dependency(%q<rspec>, [">= 0"])
|
47
|
+
else
|
48
|
+
s.add_dependency(%q<rspec>, [">= 0"])
|
49
|
+
end
|
50
|
+
else
|
51
|
+
s.add_dependency(%q<rspec>, [">= 0"])
|
52
|
+
end
|
53
|
+
end
|
data/lib/ansitags.rb
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
class String
|
2
|
+
def ansi_to_html
|
3
|
+
start_tags = 0
|
4
|
+
|
5
|
+
low_intensity_colors = [
|
6
|
+
'000000',
|
7
|
+
'800000',
|
8
|
+
'008000',
|
9
|
+
'808000',
|
10
|
+
'000080',
|
11
|
+
'000080',
|
12
|
+
'008080',
|
13
|
+
'C0C0C0'
|
14
|
+
]
|
15
|
+
|
16
|
+
high_intensity_colors = [
|
17
|
+
'808080',
|
18
|
+
'FF0000',
|
19
|
+
'00FF00',
|
20
|
+
'FFFF00',
|
21
|
+
'0000FF',
|
22
|
+
'FF00FF',
|
23
|
+
'00FFFF',
|
24
|
+
'FFFFFF'
|
25
|
+
]
|
26
|
+
|
27
|
+
current_foreground_color = low_intensity_colors[0]
|
28
|
+
current_background_color = high_intensity_colors[7]
|
29
|
+
|
30
|
+
'<pre>' + self.gsub('&', '&').gsub('"', '"').gsub('<', '<').gsub('>', '>').gsub(/\\e\[(\d+)m/) do |ansi_code|
|
31
|
+
case $1.to_i
|
32
|
+
when 0 # reset to default values
|
33
|
+
end_tags = start_tags
|
34
|
+
start_tags = 0
|
35
|
+
'</span>' * end_tags
|
36
|
+
when 1 # bold
|
37
|
+
start_tags += 1
|
38
|
+
'<span style="font-weight: bold;">'
|
39
|
+
when 3 # italic
|
40
|
+
start_tags += 1
|
41
|
+
'<span style="font-style: italic;">'
|
42
|
+
when 4 # underlined
|
43
|
+
start_tags += 1
|
44
|
+
'<span style="text-decoration: underline;">'
|
45
|
+
when 7 # swap foreground and background colors
|
46
|
+
start_tags += 1
|
47
|
+
new_foreground_color = current_background_color
|
48
|
+
new_background_color = current_foreground_color
|
49
|
+
current_foreground_color = new_foreground_color
|
50
|
+
current_background_color = new_background_color
|
51
|
+
"<span style=\"color: ##{new_foreground_color}; background-color: ##{new_background_color};\">"
|
52
|
+
when 8 # hidden
|
53
|
+
start_tags += 1
|
54
|
+
'<span style="visibility: hidden;">'
|
55
|
+
when 22 # normal weight
|
56
|
+
start_tags += 1
|
57
|
+
'<span style="font-weight: normal;">'
|
58
|
+
when 24 # not underlined
|
59
|
+
start_tags += 1
|
60
|
+
'<span style="text-decoration: none;">'
|
61
|
+
when 28 # not hidden
|
62
|
+
start_tags += 1
|
63
|
+
'<span style="visibility: visible;">'
|
64
|
+
when 30..37 # low intensity foreground colors
|
65
|
+
start_tags += 1
|
66
|
+
"<span style=\"color: ##{current_foreground_color = low_intensity_colors[$1.to_i - 30]};\">"
|
67
|
+
when 39 # reset foreground color to default low intensity color
|
68
|
+
start_tags += 1
|
69
|
+
"<span style=\"color: ##{current_foreground_color = low_intensity_colors[0]};\">"
|
70
|
+
when 40..47 # low intensity background colors
|
71
|
+
start_tags += 1
|
72
|
+
"<span style=\"background-color: ##{current_background_color = low_intensity_colors[$1.to_i - 40]};\">"
|
73
|
+
when 49 # reset background color to default low intensity color
|
74
|
+
start_tags += 1
|
75
|
+
"<span style=\"background-color: ##{current_background_color = low_intensity_colors[7]};\">"
|
76
|
+
when 90..97 # high intensity foreground colors
|
77
|
+
start_tags += 1
|
78
|
+
"<span style=\"color: ##{current_foreground_color = high_intensity_colors[$1.to_i - 90]};\">"
|
79
|
+
when 99 # reset foreground color to default high intensity color
|
80
|
+
start_tags += 1
|
81
|
+
"<span style=\"color: ##{current_foreground_color = high_intensity_colors[0]};\">"
|
82
|
+
when 100..107 # high intensity background colors
|
83
|
+
start_tags += 1
|
84
|
+
"<span style=\"background-color: ##{current_background_color = high_intensity_colors[$1.to_i - 100]};\">"
|
85
|
+
when 109 # reset background color to default high intensity color
|
86
|
+
start_tags += 1
|
87
|
+
"<span style=\"background-color: ##{current_background_color = high_intensity_colors[7]};\">"
|
88
|
+
end
|
89
|
+
end + '</span>' * start_tags + '</pre>'
|
90
|
+
end
|
91
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe String do
|
4
|
+
describe '#ansi_to_html' do
|
5
|
+
it 'should wrap everything in <pre> tags' do
|
6
|
+
'hello world'.ansi_to_html.should == '<pre>hello world</pre>'
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'should escape HTML' do
|
10
|
+
'<>&"'.ansi_to_html.should == '<pre><>&"</pre>'
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'should set font weight to bold' do
|
14
|
+
'\e[1mbold text\e[0m'.ansi_to_html.should == '<pre><span style="font-weight: bold;">bold text</span></pre>'
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'should set font style to italic' do
|
18
|
+
'\e[3mitalic text\e[0m'.ansi_to_html.should == '<pre><span style="font-style: italic;">italic text</span></pre>'
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'should set text decoration to underline' do
|
22
|
+
'\e[4munderlined text\e[0m'.ansi_to_html.should == '<pre><span style="text-decoration: underline;">underlined text</span></pre>'
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'should swap foreground and background colors' do
|
26
|
+
'\e[32mgreen foreground color. \e[7mgreen background color.\e[0m'.ansi_to_html.should == '<pre><span style="color: #008000;">green foreground color. <span style="color: #FFFFFF; background-color: #008000;">green background color.</span></span></pre>'
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'should set visibility to hidden' do
|
30
|
+
'\e[8mhidden text\e[0m'.ansi_to_html.should == '<pre><span style="visibility: hidden;">hidden text</span></pre>'
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'should font weight to normal' do
|
34
|
+
'\e[22mnormal text\e[0m'.ansi_to_html.should == '<pre><span style="font-weight: normal;">normal text</span></pre>'
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'should set text decoration to none' do
|
38
|
+
'\e[24mnon-decorated text\e[0m'.ansi_to_html.should == '<pre><span style="text-decoration: none;">non-decorated text</span></pre>'
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'should not hide text' do
|
42
|
+
'\e[28mvisible text\e[0m'.ansi_to_html.should == '<pre><span style="visibility: visible;">visible text</span></pre>'
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'should set foreground color to a low intensity color' do
|
46
|
+
'\e[31mdark red text\e[0m'.ansi_to_html.should == '<pre><span style="color: #800000;">dark red text</span></pre>'
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'should set foreground color to the default low intensity color' do
|
50
|
+
'\e[39mblack text\e[0m'.ansi_to_html == '<pre><span style="color: #000000;">black text</span></pre>'
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'should set background color to a low intensity color' do
|
54
|
+
'\e[42mtext with green background\e[0m'.ansi_to_html.should == '<pre><span style="background-color: #008000;">text with green background</span></pre>'
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'should set background color to the default low intensity color' do
|
58
|
+
'\e[49mtext with white background\e[0m'.ansi_to_html.should == '<pre><span style="background-color: #C0C0C0;">text with white background</span></pre>'
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'should set foreground color to a high intensity color' do
|
62
|
+
'\e[93mvery bright yellow text\e[0m'.ansi_to_html.should == '<pre><span style="color: #FFFF00;">very bright yellow text</span></pre>'
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'should set foreground color to the default high intensity color' do
|
66
|
+
'\e[99mgray text\e[0m'.ansi_to_html.should == '<pre><span style="color: #808080;">gray text</span></pre>'
|
67
|
+
end
|
68
|
+
|
69
|
+
it 'should set background color to a high intensity color' do
|
70
|
+
'\e[104mbright blue background\e[0m'.ansi_to_html.should == '<pre><span style="background-color: #0000FF;">bright blue background</span></pre>'
|
71
|
+
end
|
72
|
+
|
73
|
+
it 'should set background color to the default high intensity color' do
|
74
|
+
'\e[109mtext with white background\e[0m'.ansi_to_html.should == '<pre><span style="background-color: #FFFFFF;">text with white background</span></pre>'
|
75
|
+
end
|
76
|
+
|
77
|
+
it 'should close unclosed tags' do
|
78
|
+
'\e[92m\e[1mgreen bold text'.ansi_to_html.should == '<pre><span style="color: #00FF00;"><span style="font-weight: bold;">green bold text</span></span></pre>'
|
79
|
+
end
|
80
|
+
|
81
|
+
it 'should remove unsupported codes' do
|
82
|
+
'\e[5mdo not make text blink. no, seriously'.ansi_to_html.should == '<pre>do not make text blink. no, seriously</pre>'
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ansitags
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- David Trasbo
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-10-01 00:00:00 +02:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rspec
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
description: Converts ANSI codes to HTML tags
|
26
|
+
email: thisisdato@gmail.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- LICENSE
|
33
|
+
- README.rdoc
|
34
|
+
files:
|
35
|
+
- .document
|
36
|
+
- .gitignore
|
37
|
+
- LICENSE
|
38
|
+
- README.rdoc
|
39
|
+
- Rakefile
|
40
|
+
- VERSION
|
41
|
+
- ansitags.gemspec
|
42
|
+
- lib/ansitags.rb
|
43
|
+
- spec/ansitags_spec.rb
|
44
|
+
- spec/spec_helper.rb
|
45
|
+
has_rdoc: true
|
46
|
+
homepage: http://github.com/thisisdato/ansitags
|
47
|
+
licenses: []
|
48
|
+
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options:
|
51
|
+
- --charset=UTF-8
|
52
|
+
require_paths:
|
53
|
+
- lib
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: "0"
|
59
|
+
version:
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: "0"
|
65
|
+
version:
|
66
|
+
requirements: []
|
67
|
+
|
68
|
+
rubyforge_project:
|
69
|
+
rubygems_version: 1.3.5
|
70
|
+
signing_key:
|
71
|
+
specification_version: 3
|
72
|
+
summary: Converts ANSI codes to HTML tags
|
73
|
+
test_files:
|
74
|
+
- spec/spec_helper.rb
|
75
|
+
- spec/ansitags_spec.rb
|