require 'nokogiri'
doc = File.open(ARGV[0]) { |f| Nokogiri::XML(f) }

def maybequote(s)
    if s.match(/["', ;\]]/) then return '"'+s+'"' else return s end
end

def process(node)
    if node.text? then
        print node.text
    else 
        print "\\" + node.name
        if node.keys.count > 0 then
            print "["
            print node.map { |k,v| "#{k}=#{maybequote(v)}" }.join(", ")
            print "]"
        end
        if node.children.count > 0 then
            print "{"
            node.children.map {|n| process(n)}
            print "}"
        end
    end
end

puts "\\begin{document}"
doc.root.children.map {|n| process(n)}
puts "\\end{document}"
