Class: Canon::PrettyPrinter::Xml
- Inherits:
-
Object
- Object
- Canon::PrettyPrinter::Xml
- Defined in:
- lib/canon/pretty_printer/xml.rb
Overview
Pretty printer for XML with consistent indentation
Instance Method Summary collapse
-
#format(xml_string) ⇒ Object
Pretty print XML with consistent indentation.
-
#initialize(indent: 2, indent_type: "space") ⇒ Xml
constructor
A new instance of Xml.
Constructor Details
#initialize(indent: 2, indent_type: "space") ⇒ Xml
Returns a new instance of Xml.
9 10 11 12 |
# File 'lib/canon/pretty_printer/xml.rb', line 9 def initialize(indent: 2, indent_type: "space") @indent = indent.to_i @indent_type = indent_type end |
Instance Method Details
#format(xml_string) ⇒ Object
Pretty print XML with consistent indentation
15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/canon/pretty_printer/xml.rb', line 15 def format(xml_string) doc = Nokogiri::XML(xml_string, &:noblanks) # Use Nokogiri's built-in pretty printing if @indent_type == "tab" # For tabs, use indent_text parameter doc.to_xml(indent: 1, indent_text: "\t", encoding: "UTF-8") else # For spaces, use indent parameter doc.to_xml(indent: @indent, encoding: "UTF-8") end end |