Module: Twitter::Autolink
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
- DEFAULT_LIST_CLASS =
Default CSS class for auto-linked lists
"tweet-url list-slug".freeze
- DEFAULT_USERNAME_CLASS =
Default CSS class for auto-linked usernames
"tweet-url username".freeze
- DEFAULT_HASHTAG_CLASS =
Default CSS class for auto-linked hashtags
"tweet-url hashtag".freeze
- DEFAULT_CASHTAG_CLASS =
Default CSS class for auto-linked cashtags
"tweet-url cashtag".freeze
- DEFAULT_USERNAME_URL_BASE =
Default URL base for auto-linked usernames
"https://twitter.com/".freeze
- DEFAULT_LIST_URL_BASE =
Default URL base for auto-linked lists
"https://twitter.com/".freeze
- DEFAULT_HASHTAG_URL_BASE =
Default URL base for auto-linked hashtags
"https://twitter.com/#!/search?q=%23".freeze
- DEFAULT_CASHTAG_URL_BASE =
Default URL base for auto-linked cashtags
"https://twitter.com/#!/search?q=%24".freeze
- DEFAULT_INVISIBLE_TAG_ATTRS =
Default attributes for invisible span tag
"style='position:absolute;left:-9999px;'".freeze
- DEFAULT_OPTIONS =
{ :list_class => DEFAULT_LIST_CLASS, :username_class => DEFAULT_USERNAME_CLASS, :hashtag_class => DEFAULT_HASHTAG_CLASS, :cashtag_class => DEFAULT_CASHTAG_CLASS, :username_url_base => DEFAULT_USERNAME_URL_BASE, :list_url_base => DEFAULT_LIST_URL_BASE, :hashtag_url_base => DEFAULT_HASHTAG_URL_BASE, :cashtag_url_base => DEFAULT_CASHTAG_URL_BASE, :invisible_tag_attrs => DEFAULT_INVISIBLE_TAG_ATTRS }.freeze
Instance Method Summary collapse
-
#auto_link(text, options = {}, &block) ⇒ Object
Add
<a></a>
tags around the usernames, lists, hashtags and URLs in the providedtext
. -
#auto_link_cashtags(text, options = {}, &block) ⇒ Object
Add
<a></a>
tags around the cashtags in the providedtext
. - #auto_link_entities(text, entities, options = {}, &block) ⇒ Object
-
#auto_link_hashtags(text, options = {}, &block) ⇒ Object
Add
<a></a>
tags around the hashtags in the providedtext
. -
#auto_link_urls(text, options = {}, &block) ⇒ Object
(also: #auto_link_urls_custom)
Add
<a></a>
tags around the URLs in the providedtext
. -
#auto_link_usernames_or_lists(text, options = {}, &block) ⇒ Object
Add
<a></a>
tags around the usernames and lists in the providedtext
. - #auto_link_with_json(text, json, options = {}) ⇒ Object
- #html_escape(text) ⇒ Object
Methods included from Deprecation
Instance Method Details
#auto_link(text, options = {}, &block) ⇒ 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: Also any elements in the options
hash will be converted to HTML attributes and place in the <a>
tag.
:url_class
-
class to add to url
<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 :cashtag_class
-
class to add to cashtag
<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. :cashtag_url_base
-
the value for
href
attribute on cashtag links. The$cashtag
(minus the$
) will be appended at the end of this. :invisible_tag_attrs
-
HTML attribute to add to invisible span tags
:username_include_symbol
-
place the
@
symbol within username and list links :suppress_lists
-
disable auto-linking to lists
:suppress_no_follow
-
do not add
rel="nofollow"
to auto-linked items :symbol_tag
-
tag to apply around symbol (@, #, $) in username / hashtag / cashtag links
:text_with_symbol_tag
-
tag to apply around text part in username / hashtag / cashtag links
:url_target
-
the value for
target
attribute on URL links. :target_blank
-
adds
target="_blank"
to all auto_linked items username / hashtag / cashtag links / urls :link_attribute_block
-
function to modify the attributes of a link based on the entity. called with |entity, attributes| params, and should modify the attributes hash.
:link_text_block
-
function to modify the text of a link based on the entity. called with |entity, text| params, and should return a modified text.
107 108 109 |
# File 'lib/twitter-text/autolink.rb', line 107 def auto_link(text, = {}, &block) auto_link_entities(text, Extractor.extract_entities_with_indices(text, :extract_url_without_protocol => false), , &block) end |
#auto_link_cashtags(text, options = {}, &block) ⇒ Object
Add <a></a>
tags around the cashtags in the provided text
. The <a>
tags can be controlled with the following entries in the options
hash. Also any elements in the options
hash will be converted to HTML attributes and place in the <a>
tag.
:cashtag_class
-
class to add to cashtag
<a>
tags :cashtag_url_base
-
the value for
href
attribute. The cashtag text (minus the$
) will be appended at the end of this. :suppress_no_follow
-
do not add
rel="nofollow"
to auto-linked items :symbol_tag
-
tag to apply around symbol (@, #, $) in username / hashtag / cashtag links
:text_with_symbol_tag
-
tag to apply around text part in username / hashtag / cashtag links
:link_attribute_block
-
function to modify the attributes of a link based on the entity. called with |entity, attributes| params, and should modify the attributes hash.
:link_text_block
-
function to modify the text of a link based on the entity. called with |entity, text| params, and should return a modified text.
159 160 161 |
# File 'lib/twitter-text/autolink.rb', line 159 def (text, = {}, &block) # :yields: cashtag_text auto_link_entities(text, Extractor.(text), , &block) end |
#auto_link_entities(text, entities, options = {}, &block) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/twitter-text/autolink.rb', line 61 def auto_link_entities(text, entities, = {}, &block) return text if entities.empty? # NOTE deprecate these attributes not options keys in options hash, then use html_attrs = DEFAULT_OPTIONS.merge() [:html_attrs] = () [:html_attrs][:rel] ||= "nofollow" unless [:suppress_no_follow] [:html_attrs][:target] = "_blank" if [:target_blank] == true Twitter::Rewriter.rewrite_entities(text.dup, entities) do |entity, chars| if entity[:url] link_to_url(entity, chars, , &block) elsif entity[:hashtag] link_to_hashtag(entity, chars, , &block) elsif entity[:screen_name] link_to_screen_name(entity, chars, , &block) elsif entity[:cashtag] link_to_cashtag(entity, chars, , &block) end end end |
#auto_link_hashtags(text, options = {}, &block) ⇒ 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. Also any elements in the options
hash will be converted to HTML attributes and place in the <a>
tag.
: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 :symbol_tag
-
tag to apply around symbol (@, #, $) in username / hashtag / cashtag links
:text_with_symbol_tag
-
tag to apply around text part in username / hashtag / cashtag links
:link_attribute_block
-
function to modify the attributes of a link based on the entity. called with |entity, attributes| params, and should modify the attributes hash.
:link_text_block
-
function to modify the text of a link based on the entity. called with |entity, text| params, and should return a modified text.
143 144 145 |
# File 'lib/twitter-text/autolink.rb', line 143 def (text, = {}, &block) # :yields: hashtag_text auto_link_entities(text, Extractor.(text), , &block) end |
#auto_link_urls(text, options = {}, &block) ⇒ Object Also known as: auto_link_urls_custom
Add <a></a>
tags around the URLs in the provided text
. The <a>
tags can be controlled with the following entries in the options
hash. Also any elements in the options
hash will be converted to HTML attributes and place in the <a>
tag.
:url_class
-
class to add to url
<a>
tags :invisible_tag_attrs
-
HTML attribute to add to invisible span tags
:suppress_no_follow
-
do not add
rel="nofollow"
to auto-linked items :symbol_tag
-
tag to apply around symbol (@, #, $) in username / hashtag / cashtag links
:text_with_symbol_tag
-
tag to apply around text part in username / hashtag / cashtag links
:url_target
-
the value for
target
attribute on URL links. :link_attribute_block
-
function to modify the attributes of a link based on the entity. called with |entity, attributes| params, and should modify the attributes hash.
:link_text_block
-
function to modify the text of a link based on the entity. called with |entity, text| params, and should return a modified text.
176 177 178 |
# File 'lib/twitter-text/autolink.rb', line 176 def auto_link_urls(text, = {}, &block) auto_link_entities(text, Extractor.extract_urls_with_indices(text, :extract_url_without_protocol => false), , &block) end |
#auto_link_usernames_or_lists(text, options = {}, &block) ⇒ 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. Also any elements in the options
hash will be converted to HTML attributes and place in the <a>
tag.
: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. :username_include_symbol
-
place the
@
symbol within username and list links :suppress_lists
-
disable auto-linking to lists
:suppress_no_follow
-
do not add
rel="nofollow"
to auto-linked items :symbol_tag
-
tag to apply around symbol (@, #, $) in username / hashtag / cashtag links
:text_with_symbol_tag
-
tag to apply around text part in username / hashtag / cashtag links
:link_attribute_block
-
function to modify the attributes of a link based on the entity. called with |entity, attributes| params, and should modify the attributes hash.
:link_text_block
-
function to modify the text of a link based on the entity. called with |entity, text| params, and should return a modified text.
127 128 129 |
# File 'lib/twitter-text/autolink.rb', line 127 def auto_link_usernames_or_lists(text, = {}, &block) # :yields: list_or_username auto_link_entities(text, Extractor.extract_mentions_or_lists_with_indices(text), , &block) end |
#auto_link_with_json(text, json, options = {}) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/twitter-text/autolink.rb', line 45 def auto_link_with_json(text, json, = {}) # concatenate entities entities = json.values().flatten() # map JSON entity to twitter-text entity # be careful not to alter arguments received entities.map! do |entity| entity = HashHelper.symbolize_keys(entity) # hashtag entity[:hashtag] = entity[:text] if entity[:text] entity end auto_link_entities(text, entities, ) end |
#html_escape(text) ⇒ Object
202 203 204 205 206 |
# File 'lib/twitter-text/autolink.rb', line 202 def html_escape(text) text && text.to_s.gsub(/[&"'><]/) do |character| HTML_ENTITIES[character] end end |