Skip to content

Instantly share code, notes, and snippets.

@fracai
Created February 17, 2011 01:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fracai/830730 to your computer and use it in GitHub Desktop.
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>
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