Class: PrettyXML::PrettyPrint
- Inherits:
-
Object
- Object
- PrettyXML::PrettyPrint
- Defined in:
- lib/printers/pretty_print.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#handler ⇒ Object
readonly
Returns the value of attribute handler.
-
#printer ⇒ Object
readonly
Returns the value of attribute printer.
Instance Method Summary collapse
- #doctype_node(doc) ⇒ Object
-
#initialize(options) ⇒ PrettyPrint
constructor
A new instance of PrettyPrint.
- #pp(doc) ⇒ Object
- #verify_doc(doc) ⇒ Object
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() @handler = SaxPrinter.new() @printer = Nokogiri::XML::SAX::Parser.new(handler) end |
Instance Attribute Details
#handler ⇒ Object (readonly)
Returns the value of attribute handler.
3 4 5 |
# File 'lib/printers/pretty_print.rb', line 3 def handler @handler end |
#printer ⇒ Object (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 |