Created
January 15, 2019 12:57
-
-
Save ta1kt0me/a9d71276486779d9841ccfe23a412baf to your computer and use it in GitHub Desktop.
Genarate gmail-britta DSL from mailFilter.xml for gmail, not perfect
This file contains 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
require 'nokogiri' | |
require 'byebug' | |
f = File.open("./mailFilters.xml") | |
doc = Nokogiri::XML(f) | |
body = [] | |
doc.xpath('//xmlns:entry').each do |entry| | |
body << "filter {\n" | |
properties = entry.xpath('apps:property').map { |property| | |
{ | |
value: property["value"], | |
name: property["name"] | |
} | |
}.reject { |property| %w(sizeUnit sizeOperator).include? property[:name] } | |
properties.each do |property| | |
code = case property[:name] | |
when "shouldMarkAsRead" | |
%Q( mark_read\n) | |
when "shouldAlwaysMarkAsImportant" | |
%Q( mark_important\n) | |
when "shouldNeverMarkAsImportant" | |
%Q( mark_unimportant\n) | |
when "shouldArchive" | |
%Q( archive\n) | |
when "shouldTrash" | |
%Q( delete_it\n) | |
when "hasTheWord" | |
%Q( has "#{property[:value]}"\n) | |
when "doesNotHaveTheWord" | |
%Q( has_not "#{property[:value]}"\n) | |
when "forwardTo" | |
%Q( forward_to "#{property[:value]}"\n) | |
when "from" | |
values = property[:value] | |
if values.index(" OR ") | |
values.split(" OR ").join(" ") | |
%Q( from or: %w(#{values.split(" OR ").join(" ")})\n) | |
else | |
%Q( from "#{values}"\n) | |
end | |
else | |
%Q( #{property[:name]} "#{property[:value]}"\n) | |
end | |
body << code | |
end | |
body << "}\n\n" | |
end | |
puts body.join |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment