public
Created

  • Download Gist
parseitems.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
#!/usr/bin/env ruby
 
require 'rubygems'
require 'rexml/document'
require 'trollop'
 
@options = Trollop::options do
version "parseitems.rb 0.1 (c) 2010 Sven Pfleiderer"
banner <<-EOS
This is a simple, script which takes an xml file with items and generates a wiki table with the given content
 
Usage:
 
parseitems.rb -i <filename>
EOS
opt :input, "Input file", :type => String
end
 
if (@options[:input].nil? or !File.exist?(@options[:input]))
Trollop::die "must specify an existant input file"
end
 
def parse_items(document)
items = Array.new
REXML::XPath.each(document, "//item[@id]") do |fe|
items << fe
end
items
end
 
def read_items
begin
item_file = File.read(@options[:input])
item_document = REXML::Document.new(item_file)
items = parse_items(item_document)
rescue NoMethodError
puts "File #{@options[:input]} could not be parsed!"
rescue
puts "File #{@options[:input]} not found!"
end
return items if items
end
 
# |[[url|id]]|owner|desc|usage|deadline|public|
read_items.each do |item|
att = item.attributes
puts "|[[http://xt3.fourtyone.org/~xt3/shack/small_#{att['id']}.JPG|#{att['id']}]]|#{att['owner'] or "n/a"}|#{att['desc'] or "n/a"}|#{att['usage'] or "n/a"}|#{att['deadline'] or "n/a"}|#{att['public'] or "n/a"}|"
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.