mirror of
https://github.com/awesome-print/awesome_print
synced 2023-03-27 23:22:34 -04:00
Added Nokogiri plugin
This commit is contained in:
parent
f24462a533
commit
89b8fc92ef
3 changed files with 103 additions and 0 deletions
|
@ -1,3 +1,8 @@
|
|||
# Copyright (c) 2010-2011 Michael Dvorkin
|
||||
#
|
||||
# Awesome Print is freely distributable under the terms of MIT license.
|
||||
# See LICENSE file or http://www.opensource.org/licenses/mit-license.php
|
||||
#------------------------------------------------------------------------------
|
||||
module AwesomePrint
|
||||
module Mongoid
|
||||
|
||||
|
|
45
lib/awesome_print/ext/nokogiri.rb
Normal file
45
lib/awesome_print/ext/nokogiri.rb
Normal file
|
@ -0,0 +1,45 @@
|
|||
# Copyright (c) 2010-2011 Michael Dvorkin
|
||||
#
|
||||
# Awesome Print is freely distributable under the terms of MIT license.
|
||||
# See LICENSE file or http://www.opensource.org/licenses/mit-license.php
|
||||
#------------------------------------------------------------------------------
|
||||
module AwesomePrint
|
||||
module Nokogiri
|
||||
|
||||
def self.included(base)
|
||||
base.send :alias_method, :cast_without_nokogiri, :cast
|
||||
base.send :alias_method, :cast, :cast_with_nokogiri
|
||||
end
|
||||
|
||||
# Add Nokogiri XML Node and NodeSet names to the dispatcher pipeline.
|
||||
#------------------------------------------------------------------------------
|
||||
def cast_with_nokogiri(object, type)
|
||||
cast = cast_without_nokogiri(object, type)
|
||||
if (defined?(::Nokogiri::XML::Node) && object.is_a?(::Nokogiri::XML::Node)) ||
|
||||
(defined?(::Nokogiri::XML::NodeSet) && object.is_a?(::Nokogiri::XML::NodeSet))
|
||||
cast = :nokogiri_xml_node
|
||||
end
|
||||
cast
|
||||
end
|
||||
|
||||
#------------------------------------------------------------------------------
|
||||
def awesome_nokogiri_xml_node(object)
|
||||
if object.is_a?(::Nokogiri::XML::NodeSet) && object.empty?
|
||||
return "[]"
|
||||
end
|
||||
xml = object.to_xml(:indent => 2)
|
||||
#
|
||||
# Colorize tag, id/class name, and contents.
|
||||
#
|
||||
xml.gsub!(/(<)(\/?[A-Za-z1-9]+)/) { |tag| "#{$1}#{colorize($2, :keyword)}" }
|
||||
xml.gsub!(/(id|class)="[^"]+"/i) { |id| colorize(id, :class) }
|
||||
xml.gsub!(/>([^<]+)</) do |contents|
|
||||
contents = colorize($1, :trueclass) if contents && !contents.empty?
|
||||
">#{contents}<"
|
||||
end
|
||||
xml
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
AwesomePrint::Formatter.send(:include, AwesomePrint::Nokogiri)
|
53
spec/ext/nokogiri_spec.rb
Normal file
53
spec/ext/nokogiri_spec.rb
Normal file
|
@ -0,0 +1,53 @@
|
|||
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
||||
|
||||
begin
|
||||
require "nokogiri"
|
||||
require "awesome_print/ext/nokogiri"
|
||||
|
||||
describe "AwesomePrint/Nokogiri" do
|
||||
before do
|
||||
stub_dotfile!
|
||||
end
|
||||
|
||||
it "should colorize tags" do
|
||||
xml = Nokogiri::XML('<html><body><h1></h1></body></html>')
|
||||
xml.ai.should == <<-EOS
|
||||
<?xml version=\"1.0\"?>\e[1;32m
|
||||
\e[0m<\e[1;36mhtml\e[0m>\e[1;32m
|
||||
\e[0m<\e[1;36mbody\e[0m>\e[1;32m
|
||||
\e[0m<\e[1;36mh1\e[0m/>\e[1;32m
|
||||
\e[0m<\e[1;36m/body\e[0m>\e[1;32m
|
||||
\e[0m<\e[1;36m/html\e[0m>
|
||||
EOS
|
||||
end
|
||||
|
||||
it "should colorize contents" do
|
||||
xml = Nokogiri::XML('<html><body><h1>Hello</h1></body></html>')
|
||||
xml.ai.should == <<-EOS
|
||||
<?xml version=\"1.0\"?>\e[1;32m
|
||||
\e[0m<\e[1;36mhtml\e[0m>\e[1;32m
|
||||
\e[0m<\e[1;36mbody\e[0m>\e[1;32m
|
||||
\e[0m<\e[1;36mh1\e[0m>\e[1;32mHello\e[0m<\e[1;36m/h1\e[0m>\e[1;32m
|
||||
\e[0m<\e[1;36m/body\e[0m>\e[1;32m
|
||||
\e[0m<\e[1;36m/html\e[0m>
|
||||
EOS
|
||||
end
|
||||
|
||||
it "should colorize class and id" do
|
||||
xml = Nokogiri::XML('<html><body><h1><span id="hello" class="world"></span></h1></body></html>')
|
||||
xml.ai.should == <<-EOS
|
||||
<?xml version=\"1.0\"?>\e[1;32m
|
||||
\e[0m<\e[1;36mhtml\e[0m>\e[1;32m
|
||||
\e[0m<\e[1;36mbody\e[0m>\e[1;32m
|
||||
\e[0m<\e[1;36mh1\e[0m>\e[1;32m
|
||||
\e[0m<\e[1;36mspan\e[0m \e[1;33mid=\"hello\"\e[0m \e[1;33mclass=\"world\"\e[0m/>\e[1;32m
|
||||
\e[0m<\e[1;36m/h1\e[0m>\e[1;32m
|
||||
\e[0m<\e[1;36m/body\e[0m>\e[1;32m
|
||||
\e[0m<\e[1;36m/html\e[0m>
|
||||
EOS
|
||||
end
|
||||
end
|
||||
|
||||
rescue LoadError => error
|
||||
puts "Skipping Nokogiri specs: #{error}"
|
||||
end
|
Loading…
Reference in a new issue