class Middleman::ProtectEmailsExtension::Middleware

Public Class Methods

new(app, options = {}) click to toggle source
# File lib/middleman-protect-emails/extension.rb, line 14
def initialize(app, options = {})
  @rack_app = app
  @middleman_app = options[:middleman_app]
end

Public Instance Methods

call(env) click to toggle source
# File lib/middleman-protect-emails/extension.rb, line 19
def call(env)
  status, headers, response = @rack_app.call(env)

  # Get path
  path = ::Middleman::Util.full_path(env['PATH_INFO'], @middleman_app)

  # Match only HTML documents
  if path =~ /(^\/$)|(\.(htm|html)$)/
    body = ::Middleman::Util.extract_response_text(response)
    if body
      status, headers, response = Rack::Response.new(rewrite_response(body), status, headers).finish
    end
  end

  [status, headers, response]
end

Private Instance Methods

rewrite_response(body) click to toggle source
# File lib/middleman-protect-emails/extension.rb, line 38
def rewrite_response(body)
  # Keeps track of email replaces
  replaced_email = false

  # Replaces mailto links with ROT13 equivalent
  # TODO: Don't replace plaintext mailto links
  invalid_character = '\s"\'>'
  email_username = "[^@#{invalid_character}]+"
  email_domain = "[^?#{invalid_character}]+"
  email_param = "[^&#{invalid_character}]+"
  new_content = body.gsub /mailto:(#{email_username}@#{email_domain}(\?#{email_param}(\&#{email_param})*)?)/i do
    replaced_email = true
    email = $1.tr 'A-Za-z','N-ZA-Mn-za-m'
    "#email-protection-#{email}"
  end

  # Don't do anything else if there are no emails on the page
  return body unless replaced_email

  # Reads decoding script
  file = File.join(File.dirname(__FILE__), 'rot13_script.html')
  script_content = File.read file

  # Appends decoding script at end of body or end of page
  if new_content =~ /<\/body>/i
    new_content.gsub(/(<\/body>)/i) do
      script_content + $1
    end
  else
    new_content + script_content
  end
end