From 1e5c6c92f4edc09fe30c24e20941f255ea6f1c70 Mon Sep 17 00:00:00 2001 From: James Cox Date: Tue, 22 Jan 2019 16:43:22 -0500 Subject: [PATCH] Fix nokogiri errors --- .../ext/nokogiri_document_formatter.rb | 30 +++++++++++++++++++ .../ext/nokogiri_nodeset_formatter.rb | 2 +- 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 lib/awesome_print/formatters/ext/nokogiri_document_formatter.rb 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