Class: Autolinker::TextHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/autolinker/text_helper.rb

Instance Method Summary collapse

Instance Method Details

Turns all URLs and e-mail addresses into clickable links. The :link option will limit what should be linked. You can add HTML attributes to the links using :html. Possible values for :link are :all (default), :email_addresses, and :urls. If a block is given, each URL and e-mail address is yielded and the result is used as the link text. By default the text given is sanitized, you can override this behaviour setting the :sanitize option to false, or you can add options to the sanitization of the text using the :sanitize_options option hash.

Examples

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>"

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>.
      Please e-mail me at <a href=\"mailto:[email protected]\">[email protected]</a>."

You can still use auto_link with the old API that accepts the link as its optional second parameter and the html_options hash as its optional third parameter:

post_body = "Welcome to my new blog at http://www.myblog.com/. Please e-mail me at [email protected]."
auto_link(post_body, :urls)
# => "Welcome to my new blog at <a href=\"http://www.myblog.com/\">http://www.myblog.com</a>.
      Please e-mail me at [email protected]."

auto_link(post_body, :all, :target => "_blank")
# => "Welcome to my new blog at <a href=\"http://www.myblog.com/\" target=\"_blank\">http://www.myblog.com</a>.
      Please e-mail me at <a href=\"mailto:[email protected]\">[email protected]</a>."


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/autolinker/text_helper.rb', line 43

def auto_link(text, *args, &block) #link = :all, html = {}, &block)
  return '' if text.nil? || text.empty?

  options = args.size == 2 ? {} : extract_options!(args) # this is necessary because the old auto_link API has a Hash as its last parameter

  unless args.empty?
    options[:link] = args[0] || :all
    options[:html] = args[1] || {}
  end
  options = { :link => :all, :html => {} }.merge(options)

  sanitize_options = options[:sanitize_options] || {}
  sanitize = (options[:sanitize] != false)
  text = conditional_sanitize(text, sanitize, sanitize_options).to_str

  case options[:link].to_sym
  when :all then
    auto_link_email_addresses(auto_link_urls(text, options[:html], options, &block), options[:html], &block)
  when :email_addresses then
    auto_link_email_addresses(text, options[:html], &block)
  when :urls then
    auto_link_urls(text, options[:html], options, &block)
  end
end