Module | Twitter::Autolink |
In: |
lib/autolink.rb
|
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.
DEFAULT_URL_CLASS | = | "tweet-url" | Default CSS class for auto-linked URLs | |
DEFAULT_LIST_CLASS | = | "list-slug" | Default CSS class for auto-linked lists (along with the url class) | |
DEFAULT_USERNAME_CLASS | = | "username" | Default CSS class for auto-linked usernames (along with the url class) | |
DEFAULT_HASHTAG_CLASS | = | "hashtag" | Default CSS class for auto-linked hashtags (along with the url class) | |
HTML_ATTR_NO_FOLLOW | = | " rel=\"nofollow\"" | HTML attribute for robot nofollow behavior (default) |
Add <a></a> tags around the usernames, lists, hashtags and URLs in the provided text. The <a> tags can be controlled with the following entries in the options hash:
:url_class: | class to add to all <a> tags |
:list_class: | class to add to list <a> tags |
:username_class: | class to add to username <a> tags |
:hashtag_class: | class to add to hashtag <a> 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. |
:suppress_lists: | disable auto-linking to lists |
:suppress_no_follow: | Do not add rel="nofollow" to auto-linked items |
# File lib/autolink.rb, line 34 def auto_link(text, options = {}) auto_link_usernames_or_lists( auto_link_urls_custom( auto_link_hashtags(text, options), options), options) end
Add <a></a> tags around the hashtags in the provided text. The <a> tags can be controlled with the following entries in the options hash:
:url_class: | class to add to all <a> tags |
:hashtag_class: | class to add to hashtag <a> tags |
:hashtag_url_base: | the value for href attribute. The hashtag text (minus the #) will be appended at the end of this. |
:suppress_no_follow: | Do not add rel="nofollow" to auto-linked items |
# File lib/autolink.rb, line 85 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" extra_html = HTML_ATTR_NO_FOLLOW unless options[:suppress_no_follow] text.gsub(Twitter::Regex[:auto_link_hashtags]) do before = $1 hash = $2 text = $3 text = yield(text) if block_given? "#{before}<a href=\"#{options[:hashtag_url_base]}#{text}\" title=\"##{text}\" class=\"#{options[:url_class]} #{options[:hashtag_class]}\"#{extra_html}>#{hash}#{text}</a>" end end
Add <a></a> 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 <a> tag. Unless href_options contains :suppress_no_follow the rel="nofollow" attribute will be added.
# File lib/autolink.rb, line 105 def auto_link_urls_custom(text, href_options = {}) options = href_options.dup options[:rel] = "nofollow" unless options.delete(:suppress_no_follow) text.gsub(Twitter::Regex[:valid_url]) do all, before, url, protocol = $1, $2, $3, $4 html_attrs = tag_options(options.stringify_keys) || "" full_url = (protocol =~ WWW_REGEX ? "http://#{url}" : url) "#{before}<a href=\"#{full_url}\"#{html_attrs}>#{url}</a>" end end
Add <a></a> tags around the usernames and lists in the provided text. The <a> tags can be controlled with the following entries in the options hash:
:url_class: | class to add to all <a> tags |
:list_class: | class to add to list <a> tags |
:username_class: | class to add to username <a> 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. |
:suppress_lists: | disable auto-linking to lists |
:suppress_no_follow: | Do not add rel="nofollow" to auto-linked items |
# File lib/autolink.rb, line 53 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/" extra_html = HTML_ATTR_NO_FOLLOW unless options[:suppress_no_follow] 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}<a class=\"#{options[:url_class]} #{options[:list_class]}\" href=\"#{options[:list_url_base]}#{list.downcase}\"#{extra_html}>#{text}</a>" else # this is a screen name text = $3 text = yield(text) if block_given? "#{$1}#{$2}<a class=\"#{options[:url_class]} #{options[:username_class]}\" href=\"#{options[:username_url_base]}#{text}\"#{extra_html}>#{text}</a>" end end end