gr_autolink

DESCRIPTION:

This is an adaptation of the extraction of the ‘auto_link` method from rails that is the rails_autolink gem. The `auto_link` method was removed from Rails in version Rails 3.1. This gem is meant to bridge the gap for people migrating…and behaves a little differently from the parent gem we forked from:

  • performs html-escaping of characters such as ‘&’ in strings that are getting linkified if the incoming string is not html_safe?

  • retains html-safety of incoming string (if input string is unsafe, will return unsafe and vice versa)

  • fixes at least one bug: (<img src=“some.u.rl”> => <img src=“<a href=”some.u.rl“>some.u.rl</a>”>) though can’t imagine this is intended behavior, also have trouble believing that this was an open bug in rails…

FEATURES:

By default gr_autolink returns retains html-safeness of input strings.

This is original gem’s behavior and this gem does NOT include this behavior:

  • This behaviour can be overriden setting the :sanitize option to false

(thus making it insecure if you don’t have the content under control).

SYNOPSIS:

require 'rails_autolink'

auto_link("Go to http://www.rubyonrails.org and say hello to [email protected]")
# => "Go to <a href=\"http://www.rubyonrails.org\">http://www.rubyonrails.org</a> and
#     say hello to <a href=\"mailto:[email protected]\">[email protected]</a>"

auto_link("Visit http://www.loudthinking.com/ or e-mail [email protected]", :link => :urls)
# => "Visit <a href=\"http://www.loudthinking.com/\">http://www.loudthinking.com/</a>
#     or e-mail [email protected]"

auto_link("Visit http://www.loudthinking.com/ or e-mail [email protected]", :link => :email_addresses)
# => "Visit http://www.loudthinking.com/ or e-mail <a href=\"mailto:[email protected]\">[email protected]</a>"

auto_link("Go to http://www.rubyonrails.org <script>Malicious code!</script>")
# => "Go to <a href=\"http://www.rubyonrails.org\">http://www.rubyonrails.org</a> "

auto_link("Go to http://www.rubyonrails.org <script>alert('Script!')</script>", :sanitize => false)
# => "Go to <a href=\"http://www.rubyonrails.org\">http://www.rubyonrails.org</a> <script>alert('Script!')</script>"

post_body = "Welcome to my new blog at http://www.myblog.com/.  Please e-mail me at [email protected]."
auto_link(post_body, :html => { :target => '_blank' }) do |text|
  truncate(text, :length => 15)
end
# => "Welcome to my new blog at <a href=\"http://www.myblog.com/\" target=\"_blank\">http://www.m...</a>.

REQUIREMENTS:

  • rails ~> 3.1

INSTALL:

  • gem install gr_autolink

LICENSE:

(The MIT License)

Copyright © 2012 Goodreads, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.