Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/env ruby
- #-*- coding: utf-8 -*-
- require("rexml/document");
- def compare_entry_by_property_value(e1,e2,property)
- p1 = REXML::XPath.first(e1,"apps:property[@name='#{property}']");
- p2 = REXML::XPath.first(e2,"apps:property[@name='#{property}']");
- return p1.attribute("value").value <=> p2.attribute("value").value if (p1 && p2);
- return -1 if (p1);
- return +1 if (p2);
- return 0;
- end
- def compare_entry_by_property_exists(e1,e2,property)
- p1 = REXML::XPath.first(e1,"apps:property[@name='#{property}']");
- p2 = REXML::XPath.first(e1,"apps:property[@name='#{property}']");
- return -1 if (p1 && !p2);
- return +1 if (!p1 && p2);
- return 0;
- end
- def compare_entry(e1,e2)
- c = compare_entry_by_property_value(e1,e2,"label");
- return c if (c!=0);
- c = compare_entry_by_property_exists(e1,e2,"shouldTrash");
- return -c if (c!=0);
- c = compare_entry_by_property_exists(e1,e2,"shouldArchive");
- return -c if (c!=0);
- c = compare_entry_by_property_exists(e1,e2,"shouldMarkAsRead");
- return -c if (c!=0);
- c = compare_entry_by_property_exists(e1,e2,"shouldStar");
- return c if (c!=0);
- c = compare_entry_by_property_value(e1,e2,"from");
- return c if (c!=0);
- return 0;
- end
- doc = REXML::Document.new(File.open(ARGV[0]));
- entries = REXML::XPath.match(doc,"/feed/entry");
- entries.sort! { |e1,e2| compare_entry(e1,e2); }
- puts("<?xml version='1.0' encoding='UTF-8'?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:apps='http://schemas.google.com/apps/2006'>");
- entries.each { |entry| puts(entry); }
- puts("</feed>");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement