lib/autolink.rb
# Add <tt><a></a></tt> tags around the usernames and lists in the provided
#   <tt>text</tt>. The <tt><a></tt> tags can be controlled with the
#   following entries in the <tt>options</tt> hash:
#
# <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>: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
Add  tags around the usernames and lists in the provided
text. The  tags can be controlled with the
following entries in the options hash.


Add tags around the usernames and lists in the provided text . The tags can be controlled with the following entries in the options hash .
def auto_link_usernames(text, options = {}) 
      options = options.dup
      options[:url_class] ||= DEFAULT_URL_CLASS
      options[:username_class] ||= DEFAULT_USERNAME_CLASS
      options[:username_url_base] ||= "http://twitter.com/"
      options[:target] ||= DEFAULT_TARGET
      extra_html = HTML_ATTR_NO_FOLLOW unless options[:suppress_no_follow]
      new_text = ""
      text.split(/[<>]/, -1).each_with_index do |chunk, index|
        if index != 0
          new_text << ((index % 2 == 0) ? ">" : "<")
        end
        if index % 4 != 0
          new_text << chunk
        else
          new_text << chunk.gsub(Twitter::Regex[:auto_link_usernames]) do
            before, at, user, after = $1, $2, $3, $'
            user_model, title = block_given? ? yield(user) : [true, user]
            if after !~ Twitter::Regex[:end_screen_name_match] && user_model
              chunk = user
              "#{before}#{at}<a title=\"#{html_escape(title)}\" class=\"#{options[:url_class]} #{options[:username_class]}\" #{target_tag(options)}href=\"#{html_escape(options[:username_url_base])}#{html_escape(chunk)}\"#{extra_html}>#{html_escape(chunk)}</a>"
            else
              "#{before}#{at}#{user}"
            end
          end
        end
      end
      new_text
    end
def auto_link_usernames ( text , options = { } ) options = options . dup options [ :url_class ] ||= DEFAULT_URL_CLASS options [ :username_class ] ||= DEFAULT_USERNAME_CLASS options [ :username_url_base ] ||= "http://twitter.com/" options [ :target ] ||= DEFAULT_TARGET extra_html = HTML_ATTR_NO_FOLLOW unless options [ :suppress_no_follow ] new_text = "" text . split ( / [<>] / , - 1 ) . each_with_index do | chunk , index | if index != 0 new_text << ( ( index % 2 == 0 ) ? ">" : "<" ) end if index % 4 != 0 new_text << chunk else new_text << chunk . gsub ( Twitter :: Regex [ :auto_link_usernames ] ) do before , at , user , after = $1 , $2 , $3 , $' user_model , title = block_given? ? yield ( user ) : [ true , user ] if after !~ Twitter :: Regex [ :end_screen_name_match ] && user_model chunk = user "#{before}#{at}<a title=\"#{html_escape(title)}\" class=\"#{options[:url_class]} #{options[:username_class]}\" #{target_tag(options)}href=\"#{html_escape(options[:username_url_base])}#{html_escape(chunk)}\"#{extra_html}>#{html_escape(chunk)}</a>" else "#{before}#{at}#{user}" end end end end new_text end
Add <tt><a></a></tt> tags around the usernames and lists in the provided
<tt>text</tt>.
Add <tt > <a > < / a > < / tt > tags around the usernames and lists in the provided <tt > text< / tt > .
# :yields: list_or_username # #options[:list_url_base] ||= "http://twitter.com/" # this -1 flag allows strings ending in ">" to work #如果不加block参数，就假设screen_name是存在的，auto_link之！ #如果加block参数，block需返回两个元素的数组，前一个为user对象，后一个为link想要设置的title # this is a screen name # Followed by something that means we don't autolink # block return false
text
options