module Twitter::TwitterText::Autolink

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.

Constants

BOOLEAN_ATTRIBUTES
DEFAULT_CASHTAG_CLASS

Default CSS class for auto-linked cashtags

DEFAULT_CASHTAG_URL_BASE

Default URL base for auto-linked cashtags

DEFAULT_HASHTAG_CLASS

Default CSS class for auto-linked hashtags

DEFAULT_HASHTAG_URL_BASE

Default URL base for auto-linked hashtags

DEFAULT_INVISIBLE_TAG_ATTRS

Default attributes for invisible span tag

DEFAULT_LIST_CLASS

Default CSS class for auto-linked lists

DEFAULT_LIST_URL_BASE

Default URL base for auto-linked lists

DEFAULT_OPTIONS
DEFAULT_USERNAME_CLASS

Default CSS class for auto-linked usernames

DEFAULT_USERNAME_URL_BASE

Default URL base for auto-linked usernames

HTML_ENTITIES
OPTIONS_NOT_ATTRIBUTES

Options which should not be passed as HTML attributes

Public Instance Methods

html_escape(text) click to toggle source
# File lib/twitter-text/autolink.rb, line 207
def html_escape(text)
  text && text.to_s.gsub(/[&"'><]/) do |character|
    HTML_ENTITIES[character]
  end
end

Private Instance Methods

extract_html_attrs_from_options!(options) click to toggle source
# File lib/twitter-text/autolink.rb, line 226
def extract_html_attrs_from_options!(options)
  html_attrs = {}
  options.reject! do |key, value|
    unless OPTIONS_NOT_ATTRIBUTES.include?(key)
      html_attrs[key] = value
      true
    end
  end
  html_attrs
end
tag_attrs(attributes) click to toggle source
# File lib/twitter-text/autolink.rb, line 430
def tag_attrs(attributes)
  attributes.keys.sort_by{|k| k.to_s}.inject("") do |attrs, key|
    value = attributes[key]

    if BOOLEAN_ATTRIBUTES.include?(key)
      value = value ? key : nil
    end

    unless value.nil?
      value = case value
              when Array
                value.compact.join(" ")
              else
                value
              end
      attrs << %( #{html_escape(key)}="#{html_escape(value)}")
    end

    attrs
  end
end
url_entities_hash(url_entities) click to toggle source
# File lib/twitter-text/autolink.rb, line 237
def url_entities_hash(url_entities)
  (url_entities || {}).inject({}) do |entities, entity|
    # be careful not to alter arguments received
    _entity = HashHelper.symbolize_keys(entity)
    entities[_entity[:url]] = _entity
    entities
  end
end