class Arboretum::XML::IO::ArboretumIO

Public Class Methods

read(file_path, style: :clean, type: :auto) click to toggle source
# File lib/arboretum/xml.rb, line 13
def self.read(file_path, style: :clean, type: :auto)
  origin_file = caller_locations.first.absolute_path[0..(caller_locations.first.absolute_path.rindex(/\/.+\..+/))]
  sax_parser = ArboretumSax.new(style)
  type = file_path[(file_path.rindex(/\..+/))+1..-1].to_sym if type == :auto
  File.open(origin_file+file_path, 'r') do |f|
    case type
    when :xml, :xhtml
      Ox.sax_parse(sax_parser, f, :skip => :skip_off)
    when :html
      Ox.sax_parse(sax_parser, f, :skip => :skip_off, :smart => true)
    else
      puts "Warning: Invalid file type `#{type}` given for read. Using `:xml` instead..."
      Ox.sax_parse(sax_parser, f, :skip => :skip_off)
    end
  end
  sax_parser.tree
end
write(tree, file_path, style: :pretty, type: :auto) click to toggle source
# File lib/arboretum/xml.rb, line 31
def self.write(tree, file_path, style: :pretty, type: :auto)
  origin_file = caller_locations.first.absolute_path[0..(caller_locations.first.absolute_path.rindex(/\/.+\..+/))]
  type = file_path[(file_path.rindex(/\..+/))+1..-1].to_sym if type == :auto
  if ![:xml, :xhtml, :html].include?(type)
    puts "Warning: Invalid file type `#{type}` given for write. Using `:xml` instead..."
    type = :xml
  end
  File.open(origin_file+file_path, 'w') do |f|
    f.write(tree.dump_markup(style, type))
  end
end