Module: Twitter::Autolink
- Includes:
- ActionView::Helpers::TagHelper
- Defined in:
- lib/autolink.rb
Overview
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.
Constant Summary collapse
- WWW_REGEX =
:nodoc:
/www\./i
- DEFAULT_URL_CLASS =
Default CSS class for auto-linked URLs
"tweet-url"
- DEFAULT_LIST_CLASS =
Default CSS class for auto-linked lists (along with the url class)
"list-slug"
- DEFAULT_USERNAME_CLASS =
Default CSS class for auto-linked usernames (along with the url class)
"username"
- DEFAULT_HASHTAG_CLASS =
Default CSS class for auto-linked hashtags (along with the url class)
"hashtag"
- HTML_ATTR_NO_FOLLOW =
HTML attribute for robot nofollow behavior (default)
" rel=\"nofollow\""
Instance Method Summary collapse
-
#auto_link(text, options = {}) ⇒ Object
Add
<a></a>
tags around the usernames, lists, hashtags and URLs in the providedtext
. -
#auto_link_hashtags(text, options = {}) ⇒ Object
Add
<a></a>
tags around the hashtags in the providedtext
. -
#auto_link_urls_custom(text, href_options = {}) ⇒ Object
Add
<a></a>
tags around the URLs in the providedtext
. -
#auto_link_usernames_or_lists(text, options = {}) ⇒ Object
Add
<a></a>
tags around the usernames and lists in the providedtext
.
Instance Method Details
#auto_link(text, options = {}) ⇒ Object
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
34 35 36 37 38 39 40 |
# File 'lib/autolink.rb', line 34 def auto_link(text, = {}) auto_link_usernames_or_lists( auto_link_urls_custom( (text, ), ), ) end |
#auto_link_hashtags(text, options = {}) ⇒ Object
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
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/autolink.rb', line 85 def (text, = {}) # :yields: hashtag_text = .dup [:url_class] ||= DEFAULT_URL_CLASS [:hashtag_class] ||= DEFAULT_HASHTAG_CLASS [:hashtag_url_base] ||= "http://twitter.com/search?q=%23" extra_html = HTML_ATTR_NO_FOLLOW unless [: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=\"#{[:hashtag_url_base]}#{text}\" title=\"##{text}\" class=\"#{[:url_class]} #{[:hashtag_class]}\"#{extra_html}>#{hash}#{text}</a>" end end |
#auto_link_urls_custom(text, href_options = {}) ⇒ Object
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.
105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/autolink.rb', line 105 def auto_link_urls_custom(text, = {}) = .dup [:rel] = "nofollow" unless .delete(:suppress_no_follow) text.gsub(Twitter::Regex[:valid_url]) do all, before, url, protocol = $1, $2, $3, $4 html_attrs = (.stringify_keys) || "" full_url = (protocol =~ WWW_REGEX ? "http://#{url}" : url) "#{before}<a href=\"#{full_url}\"#{html_attrs}>#{url}</a>" end end |
#auto_link_usernames_or_lists(text, options = {}) ⇒ Object
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
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/autolink.rb', line 53 def auto_link_usernames_or_lists(text, = {}) # :yields: list_or_username = .dup [:url_class] ||= DEFAULT_URL_CLASS [:list_class] ||= DEFAULT_LIST_CLASS [:username_class] ||= DEFAULT_USERNAME_CLASS [:username_url_base] ||= "http://twitter.com/" [:list_url_base] ||= "http://twitter.com/" extra_html = HTML_ATTR_NO_FOLLOW unless [:suppress_no_follow] text.gsub(Twitter::Regex[:auto_link_usernames_or_lists]) do if $4 && ![:suppress_lists] # the link is a list text = list = "#{$3}#{$4}" text = yield(list) if block_given? "#{$1}#{$2}<a class=\"#{[:url_class]} #{[:list_class]}\" href=\"#{[: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=\"#{[:url_class]} #{[:username_class]}\" href=\"#{[:username_url_base]}#{text}\"#{extra_html}>#{text}</a>" end end end |