module Twitter # A module for including Tweet auto-linking in a class. The primary use of this is for helpers/views so they can auto-link # usernames, lists, hashtags and URLs. module Autolink include ActionView::Helpers::TagHelper #tag_options needed by auto_link # Default CSS class for auto-linked URLs DEFAULT_URL_CLASS = "tweet-url" # Default CSS class for auto-linked lists (along with the url class) DEFAULT_LIST_CLASS = "list-slug" # Default CSS class for auto-linked usernames (along with the url class) DEFAULT_USERNAME_CLASS = "username" # Default CSS class for auto-linked hashtags (along with the url class) DEFAULT_HASHTAG_CLASS = "hashtag" # Add tags around the usernames, lists, hashtags and URLs in the provided text. The # tags can be controlled with the following entries in the options # hash: # # :url_class:: class to add to all tags # :list_class:: class to add to list tags # :username_class:: class to add to username tags # :hashtag_class:: class to add to hashtag tags # :username_url_base:: the value for href attribute on username links. The @username (minus the @) will be appended at the end of this. # :list_url_base:: the value for href attribute on list links. The @username/list (minus the @) will be appended at the end of this. # :hashtag_url_base:: the value for href attribute on hashtag links. The #hashtag (minus the #) will be appended at the end of this. def auto_link(text, options = {}, &block) # :yields: hashtag_or_list_or_username options = options.dup auto_link_usernames_or_lists(auto_link_urls_custom(auto_link_hashtags(text), options, &block), &block) end # Add tags around the usernames and lists in the provided text. The # tags can be controlled with the following entries in the options # hash: # # :url_class:: class to add to all tags # :list_class:: class to add to list tags # :username_class:: class to add to username tags # :username_url_base:: the value for href attribute on username links. The @username (minus the @) will be appended at the end of this. # :list_url_base:: the value for href attribute on list links. The @username/list (minus the @) will be appended at the end of this. def auto_link_usernames_or_lists(text, options = {}) # :yields: list_or_username options = options.dup options[:url_class] ||= DEFAULT_URL_CLASS options[:list_class] ||= DEFAULT_LIST_CLASS options[:username_class] ||= DEFAULT_USERNAME_CLASS options[:username_url_base] ||= "http://twitter.com/" options[:list_url_base] ||= "http://twitter.com/" text.gsub(Twitter::Regex[:auto_link_usernames_or_lists]) do if $4 && !options[:suppress_lists] # the link is a list text = list = "#{$3}#{$4}" text = yield(list) if block_given? "#{$1}#{$2}#{text}" else # this is a screen name text = $3 text = yield(text) if block_given? "#{$1}#{$2}#{text}" end end end # Add tags around the hashtags in the provided text. The # tags can be controlled with the following entries in the options # hash: # # :url_class:: class to add to all tags # :hashtag_class:: class to add to hashtag tags # :hashtag_url_base:: the value for href attribute. The hashtag text (minus the #) will be appended at the end of this. # def auto_link_hashtags(text, options = {}) # :yields: hashtag_text options = options.dup options[:url_class] ||= DEFAULT_URL_CLASS options[:hashtag_class] ||= DEFAULT_HASHTAG_CLASS options[:hashtag_url_base] ||= "http://twitter.com/search?q=%23" text.gsub(Twitter::Regex[:auto_link_hashtags]) do before = $1 hash = $2 text = $3 text = yield(text) if block_given? "#{before}#{hash}#{text}" end end # Add tags around the URLs in the provided text. Any # elements in the href_options hash will be converted to HTML attributes # and place in the tag. def auto_link_urls_custom(text, href_options = {}) text.gsub(Twitter::Regex[:valid_url]) do all, before, url, protocol = $1, $2, $3, $4 options = tag_options(href_options.stringify_keys) || "" full_url = (protocol == "www." ? "http://#{url}" : url) "#{before}#{url}" end end end end