rb/lib/twitter-text/extractor.rb
# Extracts a list of all URLs included in the Tweet <tt>text</tt> along
# with the indices. If the <tt>text</tt> is <tt>nil</tt> or contains no
# URLs an empty array will be returned.
#
# If a block is given then it will be called for each URL.
Extracts a list of all URLs included in the Tweet text along
with the indices. If the text is nil or contains no
URLs an empty array will be returned.

If a block is given then it will be called for each URL.
Extracts a list of all URLs included in the Tweet text along with the indices . If the text is nil or contains no URLs an empty array will be returned . If a block is given then it will be called for each URL .
def extract_urls_with_indices(text, options = {:extract_url_without_protocol => true}) 
        return [] unless text && (options[:extract_url_without_protocol] ? text.index(".") : text.index(":"))
        urls = []
        text.to_s.scan(Twitter::TwitterText::Regex[:valid_url]) do |all, before, url, protocol, domain, port, path, query|
          valid_url_match_data = $~
                                  start_position = valid_url_match_data.char_begin(3)
          end_position = valid_url_match_data.char_end(3)
          if !protocol
            next if !options[:extract_url_without_protocol] || before =~ Twitter::TwitterText::Regex[:invalid_url_without_protocol_preceding_chars]
            last_url = nil
            domain.scan(Twitter::TwitterText::Regex[:valid_ascii_domain]) do |ascii_domain|
              next unless is_valid_domain(url.length, ascii_domain, protocol)
              last_url = {
                :url => ascii_domain,
                :indices => [start_position + $~.char_begin(0),
                             start_position + $~.char_end(0)]
              }
              urls << last_url
            end
            next unless last_url
            if path
              last_url[:url] = url.sub(domain, last_url[:url])
              last_url[:indices][1] = end_position
            end
          else
            if url =~ Twitter::TwitterText::Regex[:valid_tco_url]
              next if $1 && $1.length > MAX_TCO_SLUG_LENGTH
              url = $&
                    end_position = start_position + url.codepoint_length
            end
            next unless is_valid_domain(url.length, domain, protocol)
            urls << {
              :url => url,
              :indices => [start_position, end_position]
            }
          end
        end
        urls.each{|url| yield url[:url], url[:indices].first, url[:indices].last} if block_given?
        urls
      end
def extract_urls_with_indices ( text , options = { :extract_url_without_protocol => true } ) return [ ] unless text && ( options [ :extract_url_without_protocol ] ? text . index ( "." ) : text . index ( ":" ) ) urls = [ ] text . to_s . scan ( Twitter :: TwitterText :: Regex [ :valid_url ] ) do | all , before , url , protocol , domain , port , path , query | valid_url_match_data = $~ start_position = valid_url_match_data . char_begin ( 3 ) end_position = valid_url_match_data . char_end ( 3 ) if ! protocol next if ! options [ :extract_url_without_protocol ] || before =~ Twitter :: TwitterText :: Regex [ :invalid_url_without_protocol_preceding_chars ] last_url = nil domain . scan ( Twitter :: TwitterText :: Regex [ :valid_ascii_domain ] ) do | ascii_domain | next unless is_valid_domain ( url . length , ascii_domain , protocol ) last_url = { :url => ascii_domain , :indices => [ start_position + $~ . char_begin ( 0 ) , start_position + $~ . char_end ( 0 ) ] } urls << last_url end next unless last_url if path last_url [ :url ] = url . sub ( domain , last_url [ :url ] ) last_url [ :indices ] [ 1 ] = end_position end else if url =~ Twitter :: TwitterText :: Regex [ :valid_tco_url ] next if $1 && $1 . length > MAX_TCO_SLUG_LENGTH url = $& end_position = start_position + url . codepoint_length end next unless is_valid_domain ( url . length , domain , protocol ) urls << { :url => url , :indices => [ start_position , end_position ] } end end urls . each { | url | yield url [ :url ] , url [ :indices ] . first , url [ :indices ] . last } if block_given? urls end
Extracts a list of all URLs included in the Tweet <tt>text</tt> along
with the indices.
Extracts a list of all URLs included in the Tweet <tt > text< / tt > along with the indices .
# :yields: url, start, end # If protocol is missing and domain contains non-ASCII characters, # extract ASCII-only domains. # no ASCII-only domain found. Skip the entire URL # last_url only contains domain. Need to add path and query if they exist. # last_url was not added. Add it to urls here. # In the case of t.co URLs, don't allow additional path characters
text
<not_specific>
options
<not_specific>