From a861908fb7616a739237b3e9b5a66a460bc12a67 Mon Sep 17 00:00:00 2001 From: Mike Dvorkin Date: Sat, 7 May 2011 11:50:04 -0700 Subject: [PATCH] Added :html => true option to dump HTML colors rather that ANSI --- README.md | 28 ++++++++++++++++++---------- lib/ap/awesome_print.rb | 36 ++++++++++++++++++++---------------- lib/ap/mixin/action_view.rb | 25 ++----------------------- spec/action_view_spec.rb | 29 +++++++---------------------- spec/awesome_print_spec.rb | 36 ++++++++++++++++++++++++++++-------- 5 files changed, 75 insertions(+), 79 deletions(-) diff --git a/README.md b/README.md index c2ba95d..f67ca7f 100644 --- a/README.md +++ b/README.md @@ -20,10 +20,11 @@ objects and usage within Rails templates are supported via included mixins. Default options: - :multiline => true, # Display in multipe lines. - :plain => false, # Use colors. - :indent => 4, # Indent using 4 spaces. - :index => true, # Display array indices. + :multiline => true, # Display in multiple lines. + :plain => false, # Use colors. + :indent => 4, # Indent using 4 spaces. + :index => true, # Display array indices. + :html => false, # Use ANSI color codes rather than HTML. :sorted_hash_keys => false, # Do not sort hash keys. :color => { :array => :white, @@ -131,7 +132,7 @@ Supported color names: $ cat > 6.rb require "ap" - ap(42 == ap(42)) + ap 42 == ap(42) ^D $ ruby 6.rb 42 @@ -217,25 +218,30 @@ lines into your ~/.irbrc file: end ### Logger Convenience Method ### -awesome_print adds an ap method to the Logger and ActiveSupport::BufferedLogger classes, -allowing you to call: +awesome_print adds the 'ap' method to the Logger and ActiveSupport::BufferedLogger classes +letting you call: logger.ap object -By default, this logs at the :debug level. You can override that globally with +By default, this logs at the :debug level. You can override that globally with: :log_level => :info -in the custom defaults (see below), or you can override on a per call basis with +in the custom defaults (see below). You can also override on a per call basis with: logger.ap object, :warn ### ActionView Convenience Method ### -awesome_print adds an ap method to the ActionView::Base class making it available +awesome_print adds the 'ap' method to the ActionView::Base class making it available within Rails templates. For example: <%= ap @accounts.first %> +With other web frameworks (ex: in Sinatra templates) you can explicitly request HTML +formatting: + + <%= ap @accounts.first, :html => true %> + ### Setting Custom Defaults ### You can set your own default options by creating ``.aprc`` file in your home directory. Within that file assign your defaults to ``AwesomePrint.defaults``. @@ -278,7 +284,9 @@ For example: ### License ### Copyright (c) 2010-2011 Michael Dvorkin + twitter.com/mid + %w(mike dvorkin.net) * "@" || %w(mike fatfreecrm.com) * "@" Released under the MIT license. See LICENSE file for details. diff --git a/lib/ap/awesome_print.rb b/lib/ap/awesome_print.rb index 153d74b..69e81d6 100755 --- a/lib/ap/awesome_print.rb +++ b/lib/ap/awesome_print.rb @@ -3,19 +3,22 @@ # Awesome Print is freely distributable under the terms of MIT license. # See LICENSE file or http://www.opensource.org/licenses/mit-license.php #------------------------------------------------------------------------------ +require "cgi" require "shellwords" class AwesomePrint AP = :__awesome_print__ unless defined?(AwesomePrint::AP) CORE = [ :array, :hash, :class, :file, :dir, :bigdecimal, :rational, :struct, :method, :unboundmethod ] unless defined?(AwesomePrint::CORE) + @@force_colors = false def initialize(options = {}) @options = { - :multiline => true, - :plain => false, - :indent => 4, - :index => true, - :sorted_hash_keys => false, + :multiline => true, # Display in multiple lines. + :plain => false, # Use colors. + :indent => 4, # Indent using 4 spaces. + :index => true, # Display array indices. + :html => false, # Use ANSI color codes rather than HTML. + :sorted_hash_keys => false, # Do not sort hash keys. :color => { :array => :white, :bigdecimal => :blue, @@ -104,7 +107,7 @@ class AwesomePrint end end - # Format a Struct. If @options[:indent] if negative left align hash keys. + # Format a Struct. If @options[:indent] is negative left align hash keys. #------------------------------------------------------------------------------ def awesome_struct(s) h = {} @@ -242,10 +245,11 @@ class AwesomePrint # Pick the color and apply it to the given string as necessary. #------------------------------------------------------------------------------ def colorize(s, type) - if @options[:plain] || @options[:color][type].nil? || !colorize? - s + s = CGI.escapeHTML(s) if @options[:html] + if @options[:plain] || !@options[:color][type] || !colorize? + @options[:html] ? "
#{s}
" : s else - s.send(@options[:color][type]) + s.send(@options[:color][type], @options[:html]) end end @@ -326,6 +330,13 @@ class AwesomePrint @@force_colors || (STDOUT.tty? && ((ENV['TERM'] && ENV['TERM'] != 'dumb') || ENV['ANSICON'])) end + # Class accessor to force colorized output (ex. forked subprocess where TERM + # might be dumb). + #------------------------------------------------------------------------------ + def self.force_colors!(value = true) + @@force_colors = value + end + # Class accessors for custom defaults. #------------------------------------------------------------------------------ def self.defaults @@ -336,11 +347,4 @@ class AwesomePrint @@defaults = args end - # Class accessor to force colorized output (ex. forked subprocess where TERM - # might be dumb). - #------------------------------------------------------------------------------ - def self.force_colors!(value = true) - @@force_colors = value - end - end diff --git a/lib/ap/mixin/action_view.rb b/lib/ap/mixin/action_view.rb index 1a34c6e..97551fa 100644 --- a/lib/ap/mixin/action_view.rb +++ b/lib/ap/mixin/action_view.rb @@ -5,30 +5,9 @@ #------------------------------------------------------------------------------ module AwesomePrintActionView - def self.included(base) - unless base.const_defined?(:AP_ANSI_TO_HTML) - 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, :brown, :navy, :darkmagenta, :darkcyan, :slategray ].each_with_index do |color, i| - hash["\033[0;#{30+i}m"] = color - end - base.const_set(:AP_ANSI_TO_HTML, hash.freeze) - end - end - + # Use HTML colors and add default "debug_dump" class to the resulting HTML. def ap_debug(object, options = {}) - formatted = h(object.ai(options)) - - unless options[:plain] - self.class::AP_ANSI_TO_HTML.each do |key, value| - formatted.gsub!(key, %Q||) - end - formatted.gsub!("\033[0m", "") - end - - content_tag(:pre, formatted, :class => "debug_dump") + object.ai(options.merge(:html => true)).sub(/^])/, '
 tag" do
-      obj = 42
-      @view.ap(obj, :plain => true).should == '
42
' + it "uses HTML and adds 'debug_dump' class to plain
 tag" do
+      markup = rand
+      @view.ap(markup, :plain => true).should == %Q|
#{markup}
| end - it "should encode HTML entities" do - obj = " &" - @view.ap(obj, :plain => true).should == '
"  &<hello>"
' - 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|
42
| - 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, :brown, :navy, :darkmagenta, :darkcyan, :slategray, :black, :slategray ]) do |ansi, html| - @view.ap(obj, :color => { :fixnum => ansi }).should == %Q|
42
| - end + it "uses HTML and adds 'debug_dump' class to colorized
 tag" do
+      markup = ' &'
+      @view.ap(markup).should == '
" &<hello>"
' end end diff --git a/spec/awesome_print_spec.rb b/spec/awesome_print_spec.rb index 21e1ba1..593a51e 100644 --- a/spec/awesome_print_spec.rb +++ b/spec/awesome_print_spec.rb @@ -7,14 +7,6 @@ describe "AwesomePrint" do stub_dotfile! end - describe "ap method" do - it "return value" do - object = rand - self.stub!(:puts) - (ap object).should eql(object) - end - end - describe "Array" do before(:each) do @arr = [ 1, :two, "three", [ nil, [ true, false] ] ] @@ -469,6 +461,34 @@ EOS grepped = hash.keys.grep(/^(\d+)\//) { $1 } grepped.ai(:plain => true, :multiline => false).should == '[ "1", "2" ]' end + + it "returns value passed as a parameter" do + object = rand + self.stub!(:puts) + (ap object).should == object + end + end + + describe "HTML output" do + it "wraps ap output with plain
 tag" do
+      markup = rand
+      markup.ai(:html => true, :plain => true).should == "
#{markup}
" + end + + it "wraps ap output with colorized
 tag" do
+      markup = rand
+      markup.ai(:html => true).should == %Q|
#{markup}
| + end + + it "encodes HTML entities (plain)" do + markup = ' &' + markup.ai(:html => true, :plain => true).should == '
" &<hello>"
' + end + + it "encodes HTML entities (color)" do + markup = ' &' + markup.ai(:html => true).should == '
" &<hello>"
' + end end #------------------------------------------------------------------------------