lib/autolink.rb in twitter-text-1.4.15 vs lib/autolink.rb in twitter-text-1.4.16
- old
+ new
@@ -18,11 +18,11 @@
HTML_ATTR_NO_FOLLOW = " rel=\"nofollow\""
# Options which should not be passed as HTML attributes
OPTIONS_NOT_ATTRIBUTES = [:url_class, :list_class, :username_class, :hashtag_class,
:username_url_base, :list_url_base, :hashtag_url_base,
:username_url_block, :list_url_block, :hashtag_url_block, :link_url_block,
- :suppress_lists, :suppress_no_follow, :url_entities]
+ :username_include_symbol, :suppress_lists, :suppress_no_follow, :url_entities]
HTML_ENTITIES = {
'&' => '&',
'>' => '>',
'<' => '<',
@@ -45,10 +45,11 @@
# <tt>:username_class</tt>:: class to add to username <tt><a></tt> tags
# <tt>:hashtag_class</tt>:: class to add to hashtag <tt><a></tt> tags
# <tt>:username_url_base</tt>:: the value for <tt>href</tt> attribute on username links. The <tt>@username</tt> (minus the <tt>@</tt>) will be appended at the end of this.
# <tt>:list_url_base</tt>:: the value for <tt>href</tt> attribute on list links. The <tt>@username/list</tt> (minus the <tt>@</tt>) will be appended at the end of this.
# <tt>:hashtag_url_base</tt>:: the value for <tt>href</tt> attribute on hashtag links. The <tt>#hashtag</tt> (minus the <tt>#</tt>) will be appended at the end of this.
+ # <tt>:username_include_symbol</tt>:: place the <tt>@</tt> symbol within username and list links
# <tt>:suppress_lists</tt>:: disable auto-linking to lists
# <tt>:suppress_no_follow</tt>:: Do not add <tt>rel="nofollow"</tt> to auto-linked items
# <tt>:target</tt>:: add <tt>target="window_name"</tt> to auto-linked items
def auto_link(text, options = {})
auto_link_usernames_or_lists(
@@ -64,43 +65,47 @@
#
# <tt>:url_class</tt>:: class to add to all <tt><a></tt> tags
# <tt>:list_class</tt>:: class to add to list <tt><a></tt> tags
# <tt>:username_class</tt>:: class to add to username <tt><a></tt> tags
# <tt>:username_url_base</tt>:: the value for <tt>href</tt> attribute on username links. The <tt>@username</tt> (minus the <tt>@</tt>) will be appended at the end of this.
+ # <tt>:username_include_symbol</tt>:: place the <tt>@</tt> symbol within username and list links
# <tt>:list_url_base</tt>:: the value for <tt>href</tt> attribute on list links. The <tt>@username/list</tt> (minus the <tt>@</tt>) will be appended at the end of this.
# <tt>:suppress_lists</tt>:: disable auto-linking to lists
# <tt>:suppress_no_follow</tt>:: Do not add <tt>rel="nofollow"</tt> to auto-linked items
# <tt>:target</tt>:: add <tt>target="window_name"</tt> to auto-linked items
def auto_link_usernames_or_lists(text, options = {}) # :yields: list_or_username
options = options.dup
options[:url_class] ||= DEFAULT_URL_CLASS
options[:list_class] ||= DEFAULT_LIST_CLASS
options[:username_class] ||= DEFAULT_USERNAME_CLASS
- options[:username_url_base] ||= "http://twitter.com/"
- options[:list_url_base] ||= "http://twitter.com/"
+ options[:username_url_base] ||= "https://twitter.com/"
+ options[:list_url_base] ||= "https://twitter.com/"
options[:target] ||= DEFAULT_TARGET
extra_html = HTML_ATTR_NO_FOLLOW unless options[:suppress_no_follow]
Twitter::Rewriter.rewrite_usernames_or_lists(text) do |at, username, slash_listname|
+ at_before_user = options[:username_include_symbol] ? at : ''
+ at = options[:username_include_symbol] ? '' : at
+
name = "#{username}#{slash_listname}"
chunk = block_given? ? yield(name) : name
if slash_listname && !options[:suppress_lists]
href = if options[:list_url_block]
options[:list_url_block].call(name.downcase)
else
- "#{html_escape(options[:list_url_base])}#{html_escape(name.downcase)}"
+ "#{html_escape(options[:list_url_base] + name.downcase)}"
end
- %(#{at}<a class="#{options[:url_class]} #{options[:list_class]}" #{target_tag(options)}href="#{href}"#{extra_html}>#{html_escape(chunk)}</a>)
+ %(#{at}<a class="#{options[:url_class]} #{options[:list_class]}" #{target_tag(options)}href="#{href}"#{extra_html}>#{html_escape(at_before_user + chunk)}</a>)
else
href = if options[:username_url_block]
options[:username_url_block].call(chunk)
else
- "#{html_escape(options[:username_url_base])}#{html_escape(chunk)}"
+ "#{html_escape(options[:username_url_base] + chunk)}"
end
- %(#{at}<a class="#{options[:url_class]} #{options[:username_class]}" #{target_tag(options)}href="#{href}"#{extra_html}>#{html_escape(chunk)}</a>)
+ %(#{at}<a class="#{options[:url_class]} #{options[:username_class]}" #{target_tag(options)}href="#{href}"#{extra_html}>#{html_escape(at_before_user + chunk)}</a>)
end
end
end
# Add <tt><a></a></tt> tags around the hashtags in the provided <tt>text</tt>. The
@@ -114,11 +119,11 @@
# <tt>:target</tt>:: add <tt>target="window_name"</tt> to auto-linked items
def auto_link_hashtags(text, options = {}) # :yields: hashtag_text
options = options.dup
options[:url_class] ||= DEFAULT_URL_CLASS
options[:hashtag_class] ||= DEFAULT_HASHTAG_CLASS
- options[:hashtag_url_base] ||= "http://twitter.com/search?q=%23"
+ options[:hashtag_url_base] ||= "https://twitter.com/#!/search?q=%23"
options[:target] ||= DEFAULT_TARGET
extra_html = HTML_ATTR_NO_FOLLOW unless options[:suppress_no_follow]
Twitter::Rewriter.rewrite_hashtags(text) do |hash, hashtag|
hashtag = yield(hashtag) if block_given?
@@ -177,13 +182,13 @@
private
BOOLEAN_ATTRIBUTES = Set.new([:disabled, :readonly, :multiple, :checked]).freeze
def html_attrs_for_options(options)
- html_attrs options.reject{|k, v| OPTIONS_NOT_ATTRIBUTES.include?(k)}
+ autolink_html_attrs options.reject{|k, v| OPTIONS_NOT_ATTRIBUTES.include?(k)}
end
- def html_attrs(options)
+ def autolink_html_attrs(options)
options.inject("") do |attrs, (key, value)|
if BOOLEAN_ATTRIBUTES.include?(key)
value = value ? key : nil
end
if !value.nil?