Skip to content

Instantly share code, notes, and snippets.

@ta1kt0me
Created January 15, 2019 12:57
Show Gist options
  • Save ta1kt0me/a9d71276486779d9841ccfe23a412baf to your computer and use it in GitHub Desktop.
Save ta1kt0me/a9d71276486779d9841ccfe23a412baf to your computer and use it in GitHub Desktop.
Genarate gmail-britta DSL from mailFilter.xml for gmail, not perfect
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