Module: AmazingPrint::Nokogiri

Included in:
Formatter
Defined in:
lib/amazing_print/ext/nokogiri.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



10
11
12
13
# File 'lib/amazing_print/ext/nokogiri.rb', line 10

def self.included(base)
  base.send :alias_method, :cast_without_nokogiri, :cast
  base.send :alias_method, :cast, :cast_with_nokogiri
end

Instance Method Details

#awesome_nokogiri_xml_node(object) ⇒ Object




27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/amazing_print/ext/nokogiri.rb', line 27

def awesome_nokogiri_xml_node(object)
  return '[]' if object.is_a?(::Nokogiri::XML::NodeSet) && object.empty?

  xml = object.to_xml(indent: 2)
  #
  # Colorize tag, id/class name, and contents.
  #
  xml.gsub!(%r{(<)(/?[A-Za-z1-9]+)}) { |_tag| "#{Regexp.last_match(1)}#{colorize(Regexp.last_match(2), :keyword)}" }
  xml.gsub!(/(id|class)="[^"]+"/i) { |id| colorize(id, :class) }
  xml.gsub!(/>([^<]+)</) do |contents|
    contents = colorize(Regexp.last_match(1), :trueclass) if contents && !contents.empty?
    ">#{contents}<"
  end
  xml
end

#cast_with_nokogiri(object, type) ⇒ Object

Add Nokogiri XML Node and NodeSet names to the dispatcher pipeline.




17
18
19
20
21
22
23
24
# File 'lib/amazing_print/ext/nokogiri.rb', line 17

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