diff --git a/lib/awesome_print/formatters/ext/nokogiri_document_formatter.rb b/lib/awesome_print/formatters/ext/nokogiri_document_formatter.rb new file mode 100644 index 0000000..379a88c --- /dev/null +++ b/lib/awesome_print/formatters/ext/nokogiri_document_formatter.rb @@ -0,0 +1,30 @@ +require_relative '../base_formatter' + +module AwesomePrint + module Formatters + class NokogiriDocumentFormatter < BaseFormatter + + formatter_for :nokogiri_xml_document + + def self.formattable?(object) + defined?(::Nokogiri) && object.is_a?(::Nokogiri::XML::Document) + end + + def format(object) + + 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!(/>([^<]+)#{contents}<" + end + xml + end + + end + end +end diff --git a/lib/awesome_print/formatters/ext/nokogiri_nodeset_formatter.rb b/lib/awesome_print/formatters/ext/nokogiri_nodeset_formatter.rb index 6dc3966..d0b6f7a 100644 --- a/lib/awesome_print/formatters/ext/nokogiri_nodeset_formatter.rb +++ b/lib/awesome_print/formatters/ext/nokogiri_nodeset_formatter.rb @@ -2,7 +2,7 @@ require_relative '../base_formatter' module AwesomePrint module Formatters - class NokogiriNodeFormatter < BaseFormatter + class NokogiriNodesetFormatter < BaseFormatter formatter_for :nokogiri_xml_nodeset