Class: PrettyXML::PrettyPrint

Inherits:
Object
  • Object
show all
Defined in:
lib/printers/pretty_print.rb

Direct Known Subclasses

XHTML

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ PrettyPrint

Returns a new instance of PrettyPrint.



5
6
7
8
# File 'lib/printers/pretty_print.rb', line 5

def initialize(options)
  @handler = SaxPrinter.new(options)
  @printer = Nokogiri::XML::SAX::Parser.new(handler)
end

Instance Attribute Details

#handlerObject (readonly)

Returns the value of attribute handler.



3
4
5
# File 'lib/printers/pretty_print.rb', line 3

def handler
  @handler
end

#printerObject (readonly)

Returns the value of attribute printer.



3
4
5
# File 'lib/printers/pretty_print.rb', line 3

def printer
  @printer
end

Instance Method Details

#doctype_node(doc) ⇒ Object



26
27
28
# File 'lib/printers/pretty_print.rb', line 26

def doctype_node(doc)
  doc[/<!DOCTYPE[^>]*>/]
end

#pp(doc) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/printers/pretty_print.rb', line 10

def pp(doc)
  d = verify_doc(doc)
  dn = doctype_node(doc)
  instrs = []
  pretty = ''
  handler.pretty = pretty
  handler.instructions = instrs
  printer.parse(d)
  instrs << dn if dn
  instrs.empty? ? pretty : "#{instrs.join("\n")}\n#{pretty}"
end

#verify_doc(doc) ⇒ Object



22
23
24
# File 'lib/printers/pretty_print.rb', line 22

def verify_doc(doc)
  doc.is_a?(Nokogiri::XML::Document) ? doc.to_xml : doc
end