Class: Arboretum::XML::IO::ArboretumIO

Inherits:
Object
  • Object
show all
Defined in:
lib/arboretum/xml.rb

Class Method Summary collapse

Class Method Details

.read(file_path, style: :clean, type: :auto) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 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) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# 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