Module: Twitter::TwitterText::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
- #auto_link_cashtags(text, options = {}, &block) ⇒ Object
- #auto_link_entities(text, entities, options = {}, &block) ⇒ Object
- #auto_link_hashtags(text, options = {}, &block) ⇒ Object
- #auto_link_urls(text, options = {}, &block) ⇒ Object (also: #auto_link_urls_custom)
- #auto_link_usernames_or_lists(text, options = {}, &block) ⇒ Object
- #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 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: Also any elements in the options hash will be converted to HTML attributes and place in the tag.
:url_class:: class to add to url 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 :cashtag_class:: class to add to cashtag 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.
114 115 116 |
# File 'lib/twitter-text/autolink.rb', line 114 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 tags around the cashtags in the provided text. The 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 tag.
:cashtag_class:: class to add to cashtag 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.
166 167 168 |
# File 'lib/twitter-text/autolink.rb', line 166 def (text, = {}, &block) # :yields: cashtag_text auto_link_entities(text, Extractor.(text), , &block) end |
#auto_link_entities(text, entities, options = {}, &block) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/twitter-text/autolink.rb', line 66 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::TwitterText::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) elsif entity[:emoji] entity[:emoji] end end end |
#auto_link_hashtags(text, options = {}, &block) ⇒ Object
Add tags around the hashtags in the provided text. The 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 tag.
: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. :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.
150 151 152 |
# File 'lib/twitter-text/autolink.rb', line 150 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 tags around the URLs in the provided text. The 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 tag.
:url_class:: class to add to url 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.
183 184 185 |
# File 'lib/twitter-text/autolink.rb', line 183 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 tags around the usernames and lists in the provided text. The 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 tag.
: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. :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.
134 135 136 |
# File 'lib/twitter-text/autolink.rb', line 134 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
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/twitter-text/autolink.rb', line 50 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
209 210 211 212 213 |
# File 'lib/twitter-text/autolink.rb', line 209 def html_escape(text) text && text.to_s.gsub(/[&"'><]/) do |character| HTML_ENTITIES[character] end end |