Created
February 17, 2011 01:24
-
-
Save fracai/830730 to your computer and use it in GitHub Desktop.
An email obfuscation filter for nanoc. It should find and hide all links in the form of: <a href="mailto:"></a>
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
module Nanoc3::Filters | |
class ObfuscateMail < Nanoc3::Filter | |
identifier :obfuscate_mail | |
require 'digest/sha1' | |
def run(content, params={}) | |
link_count=0 | |
content.split("\n").each do |line| | |
line.gsub!(/<a href="mailto:[^>]+>[^<]+<\/a>/) { |match| | |
link_count+=1 | |
hashed = Digest::SHA1.hexdigest(match+link_count.to_s) | |
'<span id="'+hashed+'">'+ | |
simple_obfuscate(match)+ | |
'</span>'+ | |
'<script type="text/javascript">'+ | |
'document.getElementById("'+hashed+'").innerHTML='+js_de_rot13(match.tr("A-Ma-mN-Zn-z","N-Zn-zA-Ma-m"))+';'+ | |
'</script>' | |
} | |
end.join("\n") | |
end | |
def js_de_rot13(content) | |
"'"+content+"'"+'.replace(/[a-zA-Z]/g,function(c){return String.fromCharCode((c<="Z"?90:122)>=(c=c.charCodeAt(0)+13)?c:c-26);})' | |
end | |
def simple_obfuscate(content) | |
c = content.sub(/mailto:[^"]+/) { |m| m.gsub(/@/," AT ").gsub(/\./," DOT ") } | |
c.sub(/>[^<]+</) { |m| m.gsub(/@/," AT ").gsub(/\./," DOT ") } | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment