Module: Twitter::TwitterText::Autolink

Extended by:
Autolink, Deprecation
Included in:
Autolink
Defined in:
lib/twitter-text/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

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

Methods included from Deprecation

deprecate

Instance Method Details

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, options = {}, &block)
  auto_link_entities(text, Extractor.extract_entities_with_indices(text, :extract_url_without_protocol => false), options, &block)
end


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, options = {}, &block)
  return text if entities.empty?

  # NOTE deprecate these attributes not options keys in options hash, then use html_attrs
  options = DEFAULT_OPTIONS.merge(options)
  options[:html_attrs] = extract_html_attrs_from_options!(options)
  options[:html_attrs][:rel] ||= "nofollow" unless options[:suppress_no_follow]
  options[:html_attrs][:target] = "_blank" if options[:target_blank] == true

  Twitter::TwitterText::Rewriter.rewrite_entities(text.dup, entities) do |entity, chars|
    if entity[:url]
      link_to_url(entity, chars, options, &block)
    elsif entity[:hashtag]
      link_to_hashtag(entity, chars, options, &block)
    elsif entity[:screen_name]
      link_to_screen_name(entity, chars, options, &block)
    elsif entity[:cashtag]
      link_to_cashtag(entity, chars, options, &block)
    elsif entity[:emoji]
      entity[:emoji]
    end
  end
end


134
135
136
# File 'lib/twitter-text/autolink.rb', line 134

def auto_link_usernames_or_lists(text, options = {}, &block) # :yields: list_or_username
  auto_link_entities(text, Extractor.extract_mentions_or_lists_with_indices(text), options, &block)
end


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, options = {})
  # 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, options)
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