Created
August 5, 2012 06:03
-
-
Save lpm11/3262141 to your computer and use it in GitHub Desktop.
Gmail のエクスポートしたフィルタを適当に sort して出力
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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>"); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
めんどくさい方はこちら: http://gmfilter-sort.herokuapp.com/