Module: Twitter::Autolink

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

Default CSS class for auto-linked usernames

"tweet-url username"
DEFAULT_HASHTAG_CLASS =

Default CSS class for auto-linked hashtags

"tweet-url hashtag"
DEFAULT_CASHTAG_CLASS =

Default CSS class for auto-linked cashtags

"tweet-url cashtag"
DEFAULT_USERNAME_URL_BASE =

Default URL base for auto-linked usernames

"/"
DEFAULT_LIST_URL_BASE =

Default URL base for auto-linked lists

"/"
DEFAULT_HASHTAG_URL_BASE =

Default URL base for auto-linked hashtags

"/#!/search?q=%23"
DEFAULT_CASHTAG_URL_BASE =

Default URL base for auto-linked cashtags

"/#!/search?q=%24"
DEFAULT_INVISIBLE_TAG_ATTRS =

Default attributes for invisible span tag

"style='position:absolute;left:-9999px;'"
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 <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.

: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.



103
104
105
# File 'lib/twitter-text-relative/autolink.rb', line 103

def auto_link(text, options = {}, &block)
  auto_link_entities(text, Extractor.extract_entities_with_indices(text, :extract_url_without_protocol => false), options, &block)
end

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.



155
156
157
# File 'lib/twitter-text-relative/autolink.rb', line 155

def auto_link_cashtags(text, options = {}, &block)  # :yields: cashtag_text
  auto_link_entities(text, Extractor.extract_cashtags_with_indices(text), options, &block)
end


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/twitter-text-relative/autolink.rb', line 59

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]

  Twitter::Rewriter.rewrite_entities(text, 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)
    end
  end
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. 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.



139
140
141
# File 'lib/twitter-text-relative/autolink.rb', line 139

def auto_link_hashtags(text, options = {}, &block)  # :yields: hashtag_text
  auto_link_entities(text, Extractor.extract_hashtags_with_indices(text), options, &block)
end

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.



172
173
174
# File 'lib/twitter-text-relative/autolink.rb', line 172

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



123
124
125
# File 'lib/twitter-text-relative/autolink.rb', line 123

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


45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/twitter-text-relative/autolink.rb', line 45

def auto_link_with_json(text, json, options = {})
  # concatenate entities
  entities = json.values().flatten()

  # map JSON entity to twitter-text entity
  entities.each do |entity|
    HashHelper.symbolize_keys!(entity)
    # hashtag
    entity[:hashtag] = entity[:text] if entity[:text]
  end

  auto_link_entities(text, entities, options)
end

#html_escape(text) ⇒ Object



198
199
200
201
202
# File 'lib/twitter-text-relative/autolink.rb', line 198

def html_escape(text)
  text && text.to_s.gsub(/[&"'><]/) do |character|
    HTML_ENTITIES[character]
  end
end