Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

implement Term::Ansicolor.to_html #8

Open
wants to merge 3 commits into from

1 participant

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 21, 2011
  1. @hotchpotch

    implement Term::Ansicolor.to_html,

    hotchpotch authored
    ANSI-sequences convert to html string.
  2. @hotchpotch
Commits on Dec 20, 2012
  1. @hotchpotch

    Remove not exist files

    hotchpotch authored
This page is out of date. Refresh to see the latest.
View
6 Rakefile
@@ -49,6 +49,12 @@ if defined? Gem
pkg.need_tar = true
pkg.package_files += PKG_FILES
end
+
+ desc "Generate gemspec file"
+ task :gemspec do
+ File.open('term-ansicolor.gemspec', 'w') {|f| f.puts spec.to_ruby }
+ puts "Generated: term-ansicolor.gemspec"
+ end
end
desc m = "Writing version information for #{PKG_VERSION}"
View
23 lib/term/ansicolor.rb
@@ -3,6 +3,10 @@
module Term
# The ANSIColor module can be used for namespacing and mixed into your own
# classes.
+
+ # autoloading Term::ANSIColor::HTML
+ autoload :HTML, 'term/ansicolor/html'
+
module ANSIColor
# :stopdoc:
ATTRIBUTES = [
@@ -75,7 +79,7 @@ def #{c}(string = nil)
# Regular expression that is used to scan for ANSI-sequences while
# uncoloring strings.
- COLORED_REGEXP = /\e\[(?:[34][0-7]|[0-9])?m/
+ COLORED_REGEXP = /\e\[([34][0-7]|[0-9])?m/
# Returns an uncolored version of the string, that is all
# ANSI-sequences are stripped from the string.
@@ -91,6 +95,23 @@ def uncolored(string = nil) # :yields:
end
end
+ # Return html string
+ # options[:no_style] = true #=> no render style attribute
+ def to_html(string = nil, options = {})
+ target = if block_given?
+ options = string if string.kind_of?(Hash)
+ yield
+ elsif string && !string.kind_of?(Hash)
+ string
+ elsif respond_to?(:to_str)
+ options = string if string.kind_of?(Hash)
+ to_str
+ else
+ ''
+ end
+ HTML.convert(target, options)
+ end
+
module_function
# Returns an array of all Term::ANSIColor attributes as symbols.
View
72 lib/term/ansicolor/html.rb
@@ -0,0 +1,72 @@
+
+module Term
+ module ANSIColor
+ # HTML convert module
+ module HTML
+ ATTRIBUTE_VALUES = ATTRIBUTES.transpose.last
+
+ # html escape chars/values
+ ESC = {
+ '&' => '&',
+ '"' => '"',
+ '<' => '&lt;',
+ '>' => '&gt;'
+ }
+ ESC_KEYS = ESC.keys.join('')
+
+ # escape sequences convert to html
+ def self.convert(string, options = {})
+ render_style_attributes = !options[:no_style]
+
+ escape_html(string).gsub(COLORED_REGEXP) {|m|
+ span_tag($1.to_i, render_style_attributes)
+ }
+ end
+
+ # generate span tag from escape sequences number
+ def self.span_tag(num, render_style_attributes)
+ if num == 0
+ "</span>"
+ else
+ "<span #{klass(num)}#{render_style_attributes ? style(num) : ''}>"
+ end
+ end
+
+ # generate class attribute from escape sequences number
+ def self.klass(num)
+ name = ATTRIBUTE_NAMES[ATTRIBUTE_VALUES.index(num)].to_s.gsub('_', '-')
+ %Q|class="ansi-color ansi-#{name}"|
+ end
+
+ # generate style attribute from escape sequences number
+ def self.style(num)
+ name = ATTRIBUTE_NAMES[ATTRIBUTE_VALUES.index(num)].to_s
+ val = nil
+ case name
+ when "bold"
+ val = "font-weight: bold"
+ when "underline"
+ val = "text-decoration: underline"
+ when "blink"
+ val = "text-decoration: blink"
+ else
+ if num >= 10
+ if m = name.match(/^on_(.*)$/)
+ val = "background-color: #{m[1]}"
+ else
+ val = "color: #{name}"
+ end
+ end
+ end
+ val ? %Q| style="#{val}"| : ""
+ end
+
+ # escape html chars
+ def self.escape_html(string)
+ string.gsub(/[#{ESC_KEYS}]/n) {|c| ESC[c] }
+ end
+
+ extend self
+ end
+ end
+end
View
31 term-ansicolor.gemspec
@@ -0,0 +1,31 @@
+# -*- encoding: utf-8 -*-
+
+Gem::Specification.new do |s|
+ s.name = %q{term-ansicolor}
+ s.version = "1.0.5"
+
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
+ s.authors = ["Florian Frank"]
+ s.date = %q{2011-07-21}
+ s.description = %q{}
+ s.email = %q{flori@ping.de}
+ s.executables = ["cdiff", "decolor"]
+ s.extra_rdoc_files = ["README"]
+ s.files = ["CHANGES", "COPYING", "README", "Rakefile", "VERSION", "bin", "bin/cdiff", "bin/decolor", "examples", "examples/example.rb", "install.rb", "lib", "lib/term", "lib/term/ansicolor", "lib/term/ansicolor.rb", "lib/term/ansicolor/html.rb", "lib/term/ansicolor/version.rb", "tests", "tests/ansicolor_test.rb"]
+ s.homepage = %q{http://flori.github.com/term-ansicolor}
+ s.rdoc_options = ["--main", "README", "--title", "Term::ANSIColor"]
+ s.require_paths = ["lib"]
+ s.rubyforge_project = %q{term-ansicolor}
+ s.rubygems_version = %q{1.6.2}
+ s.summary = %q{Ruby library that colors strings using ANSI escape sequences}
+ s.test_files = ["tests/ansicolor_test.rb"]
+
+ if s.respond_to? :specification_version then
+ s.specification_version = 3
+
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
+ else
+ end
+ else
+ end
+end
View
28 tests/ansicolor_test.rb
@@ -48,6 +48,34 @@ def test_uncolored
assert_equal string, uncolored { string }
end
+ def test_html_red
+ red_html = '<span class="ansi-color ansi-red" style="color: red">red</span>'
+ assert_equal red_html, @string_red.to_html
+ assert_equal red_html, Term::ANSIColor.to_html(@string_red)
+ assert_equal red_html, Term::ANSIColor.to_html { @string_red }
+ end
+
+ def test_html_red_on_green
+ red_on_green_html = '<span class="ansi-color ansi-on-green" style="background-color: green"><span class="ansi-color ansi-red" style="color: red">red</span></span>'
+ assert_equal red_on_green_html, @string_red_on_green.to_html
+ assert_equal red_on_green_html, Term::ANSIColor.to_html(@string_red_on_green)
+ assert_equal red_on_green_html, Term::ANSIColor.to_html { @string_red_on_green }
+ end
+
+ def test_html_with_options
+ options = {:no_style => true}
+ red_html = '<span class="ansi-color ansi-red">red</span>'
+ assert_equal red_html, @string_red.to_html(options)
+ assert_equal red_html, Term::ANSIColor.to_html(@string_red, options)
+ assert_equal red_html, Term::ANSIColor.to_html(options) { @string_red }
+ end
+
+ def test_html_styles
+ assert_equal ' style="font-weight: bold"', Term::ANSIColor::HTML.style(1)
+ assert_equal ' style="text-decoration: underline"', Term::ANSIColor::HTML.style(4)
+ assert_equal ' style="text-decoration: blink"', Term::ANSIColor::HTML.style(5)
+ end
+
def test_attributes
foo = 'foo'
for (a, _) in Term::ANSIColor.attributes
Something went wrong with that request. Please try again.