class Nokogiri::XML::Node
def write_to(io, *options)
node.write_to(io, indent_text: '-', indent: 2)
To save indented with two dashes:
node.write_to(io, encoding: 'UTF-8', indent: 2)
To save with UTF-8 indented twice:
* +:save_with+ (Integer) a combination of SaveOptions constants
* +:indent+ (Integer) the number of +:indent_text+ to use (defaults to +2+)
* +:indent_text+ (String) the indentation text (defaults to
" "
)* +:encoding+ (String or Encoding) specify the encoding of the output (defaults to document encoding)
[Options]
- +options+ (Hash) See below
- +io+ (IO) An IO-like object to which the serialized content will be written.
[Parameters]
Serialize this node or document to +io+.
write_to(io, *options)
:call-seq:
##
def write_to(io, *options) options = options.first.is_a?(Hash) ? options.shift : {} encoding = options[:encoding] || options[0] || document.encoding if Nokogiri.jruby? save_options = options[:save_with] || options[1] indent_times = options[:indent] || 0 else save_options = options[:save_with] || options[1] || SaveOptions::FORMAT indent_times = options[:indent] || 2 end indent_text = options[:indent_text] || " " # Any string times 0 returns an empty string. Therefore, use the same # string instead of generating a new empty string for every node with # zero indentation. indentation = indent_times.zero? ? "" : (indent_text * indent_times) config = SaveOptions.new(save_options.to_i) yield config if block_given? encoding = encoding.is_a?(Encoding) ? encoding.name : encoding native_write_to(io, encoding, indentation, config.options) end