mirror of
https://github.com/awesome-print/awesome_print
synced 2023-03-27 23:22:34 -04:00
Enabled peaceful coexistence with the colorize gem
This commit is contained in:
parent
5f6d2268b0
commit
39bf45e4d5
2 changed files with 49 additions and 4 deletions
|
@ -39,12 +39,19 @@ module AwesomePrint
|
||||||
|
|
||||||
# Pick the color and apply it to the given string as necessary.
|
# Pick the color and apply it to the given string as necessary.
|
||||||
#------------------------------------------------------------------------------
|
#------------------------------------------------------------------------------
|
||||||
def colorize(s, type)
|
def colorize(str, type)
|
||||||
s = CGI.escapeHTML(s) if @options[:html]
|
str = CGI.escapeHTML(str) if @options[:html]
|
||||||
if @options[:plain] || !@options[:color][type] || !@inspector.colorize?
|
if @options[:plain] || !@options[:color][type] || !@inspector.colorize?
|
||||||
s
|
str
|
||||||
|
#
|
||||||
|
# Check if the string color method is defined by awesome_print and accepts
|
||||||
|
# html parameter or it has been overriden by some gem such as colorize.
|
||||||
|
#
|
||||||
|
elsif str.method(@options[:color][type]).arity == -1 # Accepts html parameter.
|
||||||
|
str.send(@options[:color][type], @options[:html])
|
||||||
else
|
else
|
||||||
s.send(@options[:color][type], @options[:html])
|
str = %Q|<kbd style="color:#{@options[:color][type]}">#{str}</kbd>| if @options[:html]
|
||||||
|
str.send(@options[:color][type])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -101,5 +101,43 @@ EOS
|
||||||
}
|
}
|
||||||
EOS
|
EOS
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "Coexistence with the colorize gem" do
|
||||||
|
before do # Redefine String#red just like colorize gem does it.
|
||||||
|
@awesome_method = "".method(:red)
|
||||||
|
String.instance_eval do
|
||||||
|
define_method :red do # Method arity is 0.
|
||||||
|
"[red]#{self}[/red]"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
after do # Restore String#red method.
|
||||||
|
awesome_method = @awesome_method
|
||||||
|
String.instance_eval do
|
||||||
|
define_method :red, awesome_method
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
it "shoud not raise ArgumentError when formatting HTML" do
|
||||||
|
out = "hello".ai(:color => { :string => :red }, :html => true)
|
||||||
|
out.should == %Q|<pre>[red]<kbd style="color:red">"hello"</kbd>[/red]</pre>|
|
||||||
|
end
|
||||||
|
|
||||||
|
it "shoud not raise ArgumentError when formatting HTML (shade color)" do
|
||||||
|
out = "hello".ai(:color => { :string => :redish }, :html => true)
|
||||||
|
out.should == %Q|<pre><kbd style="color:darkred">"hello"</kbd></pre>|
|
||||||
|
end
|
||||||
|
|
||||||
|
it "shoud not raise ArgumentError when formatting non-HTML" do
|
||||||
|
out = "hello".ai(:color => { :string => :red }, :html => false)
|
||||||
|
out.should == %Q|[red]"hello"[/red]|
|
||||||
|
end
|
||||||
|
|
||||||
|
it "shoud not raise ArgumentError when formatting non-HTML (shade color)" do
|
||||||
|
out = "hello".ai(:color => { :string => :redish }, :html => false)
|
||||||
|
out.should == %Q|\e[0;31m"hello"\e[0m|
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue