mirror of
https://github.com/awesome-print/awesome_print
synced 2023-03-27 23:22:34 -04:00
Added ap support within Rails templates (ex. <%= ap @obj =>)
This commit is contained in:
parent
a42d00ad33
commit
5ee3eaeb57
5 changed files with 93 additions and 10 deletions
|
@ -8,8 +8,8 @@ require File.dirname(__FILE__) + "/ap/core_ext/kernel"
|
|||
require File.dirname(__FILE__) + "/ap/awesome_print"
|
||||
|
||||
require File.dirname(__FILE__) + "/ap/core_ext/logger" if defined?(::Logger) or defined?(::ActiveSupport::BufferedLogger)
|
||||
require File.dirname(__FILE__) + "/ap/core_ext/action_view" if defined?(::ActionView)
|
||||
|
||||
require File.dirname(__FILE__) + "/ap/mixin/active_record" if defined?(::ActiveRecord)
|
||||
require File.dirname(__FILE__) + "/ap/mixin/active_support" if defined?(::ActiveSupport)
|
||||
require File.dirname(__FILE__) + "/ap/mixin/application_helper" if defined?(::ApplicationHelper)
|
||||
|
||||
|
|
30
lib/ap/core_ext/action_view.rb
Normal file
30
lib/ap/core_ext/action_view.rb
Normal file
|
@ -0,0 +1,30 @@
|
|||
module AwesomePrintActionView
|
||||
|
||||
def ap_debug(object, options = {})
|
||||
formatted = object.ai(options)
|
||||
|
||||
if options[:plain]
|
||||
%Q|<pre class="debug_dump">#{h(formatted).gsub(" ", " ")}</pre>|
|
||||
else
|
||||
hash = {} # Build ANSI => HTML color map.
|
||||
[ :gray, :red, :green, :yellow, :blue, :purple, :cyan, :white ].each_with_index do |color, i|
|
||||
hash["\033[1;#{30+i}m"] = color
|
||||
end
|
||||
[ :black, :darkred, :darkgreen, :sienna, :navy, :darkmagenta, :darkcyan, :slategray ].each_with_index do |color, i|
|
||||
hash["\033[0;#{30+i}m"] = color
|
||||
end
|
||||
|
||||
formatted = h(formatted).gsub(" ", " ")
|
||||
hash.each do |key, value|
|
||||
formatted.gsub!(key, %Q|<font color="#{value}">|)
|
||||
end
|
||||
formatted.gsub!("\033[0m", "</font>")
|
||||
%Q|<pre class="debug_dump">#{formatted}</pre>|
|
||||
end
|
||||
|
||||
end
|
||||
alias_method :ap, :ap_debug
|
||||
|
||||
end
|
||||
|
||||
ActionView::Base.send(:include, AwesomePrintActionView) if defined?(::ActionView)
|
|
@ -1,5 +0,0 @@
|
|||
module ApplicationHelper
|
||||
def ap_debug(obj)
|
||||
content_tag(:pre, obj.ai(:plain => true), :class => 'debug_dump')
|
||||
end
|
||||
end
|
35
spec/action_view_spec.rb
Normal file
35
spec/action_view_spec.rb
Normal file
|
@ -0,0 +1,35 @@
|
|||
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
||||
|
||||
require 'action_view'
|
||||
require 'ap/core_ext/action_view'
|
||||
|
||||
describe "AwesomePrint ActionView extensions" do
|
||||
before(:each) do
|
||||
@view = ActionView::Base.new
|
||||
end
|
||||
|
||||
it "should wrap ap output with <pre> tag" do
|
||||
obj = 42
|
||||
@view.ap(obj, :plain => true).should == '<pre class="debug_dump">42</pre>'
|
||||
end
|
||||
|
||||
it "should encode HTML entities" do
|
||||
obj = " &<hello>"
|
||||
@view.ap(obj, :plain => true).should == '<pre class="debug_dump">" &<hello>"</pre>'
|
||||
end
|
||||
|
||||
it "should convert primary ANSI colors to HTML" do
|
||||
obj = 42
|
||||
[ :gray, :red, :green, :yellow, :blue, :purple, :cyan, :white ].each do |color|
|
||||
@view.ap(obj, :color => { :fixnum => color }).should == %Q|<pre class="debug_dump"><font color="#{color}">42</font></pre>|
|
||||
end
|
||||
end
|
||||
|
||||
it "should convert mixed ANSI colors to HTML" do
|
||||
obj = 42
|
||||
[ :grayish, :redish, :greenish, :yellowish, :blueish, :purpleish, :cyanish, :whiteish, :black, :pale ].zip(
|
||||
[ :black, :darkred, :darkgreen, :sienna, :navy, :darkmagenta, :darkcyan, :slategray, :black, :slategray ]) do |ansi, html|
|
||||
@view.ap(obj, :color => { :fixnum => ansi }).should == %Q|<pre class="debug_dump"><font color="#{html}">42</font></pre>|
|
||||
end
|
||||
end
|
||||
end
|
|
@ -306,34 +306,57 @@ EOS
|
|||
end
|
||||
|
||||
it "plain multiline" do
|
||||
@struct.ai(:plain => true).should == <<-EOS.strip
|
||||
s1 = <<-EOS.strip
|
||||
{
|
||||
:address => "1313 Mockingbird Lane",
|
||||
:name => "Herman Munster"
|
||||
}
|
||||
EOS
|
||||
s2 = <<-EOS.strip
|
||||
{
|
||||
:name => "Herman Munster",
|
||||
:address => "1313 Mockingbird Lane"
|
||||
}
|
||||
EOS
|
||||
@struct.ai(:plain => true).should satisfy { |match| match == s1 || match == s2 }
|
||||
end
|
||||
|
||||
it "plain multiline indented" do
|
||||
@struct.ai(:plain => true, :indent => 1).should == <<-EOS.strip
|
||||
s1 = <<-EOS.strip
|
||||
{
|
||||
:address => "1313 Mockingbird Lane",
|
||||
:name => "Herman Munster"
|
||||
}
|
||||
EOS
|
||||
s2 = <<-EOS.strip
|
||||
{
|
||||
:name => "Herman Munster",
|
||||
:address => "1313 Mockingbird Lane"
|
||||
}
|
||||
EOS
|
||||
@struct.ai(:plain => true, :indent => 1).should satisfy { |match| match == s1 || match == s2 }
|
||||
end
|
||||
|
||||
it "plain single line" do
|
||||
@struct.ai(:plain => true, :multiline => false).should == "{ :address => \"1313 Mockingbird Lane\", :name => \"Herman Munster\" }"
|
||||
s1 = "{ :address => \"1313 Mockingbird Lane\", :name => \"Herman Munster\" }"
|
||||
s2 = "{ :name => \"Herman Munster\", :address => \"1313 Mockingbird Lane\" }"
|
||||
@struct.ai(:plain => true, :multiline => false).should satisfy { |match| match == s1 || match == s2 }
|
||||
end
|
||||
|
||||
it "colored multiline (default)" do
|
||||
@struct.ai.should == <<-EOS.strip
|
||||
s1 = <<-EOS.strip
|
||||
{
|
||||
:address\e[0;37m => \e[0m\e[0;33m\"1313 Mockingbird Lane\"\e[0m,
|
||||
:name\e[0;37m => \e[0m\e[0;33m\"Herman Munster\"\e[0m
|
||||
}
|
||||
EOS
|
||||
s2 = <<-EOS.strip
|
||||
{
|
||||
:name\e[0;37m => \e[0m\e[0;33m\"Herman Munster\"\e[0m,
|
||||
:address\e[0;37m => \e[0m\e[0;33m\"1313 Mockingbird Lane\"\e[0m
|
||||
}
|
||||
EOS
|
||||
@struct.ai.should satisfy { |match| match == s1 || match == s2 }
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue