twitter-text 1.4.17 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/rewriter.rb DELETED
@@ -1,65 +0,0 @@
1
- module Twitter
2
- # A module provides base methods to rewrite usernames, lists, hashtags and URLs.
3
- module Rewriter extend self
4
- def rewrite(text, options = {})
5
- [:hashtags, :urls, :usernames_or_lists].inject(text) do |key|
6
- send("rewrite_#{key}", text, &options[key]) if options[key]
7
- end
8
- end
9
-
10
- def rewrite_usernames_or_lists(text)
11
- new_text = ""
12
-
13
- # this -1 flag allows strings ending in ">" to work
14
- text.to_s.split(/[<>]/, -1).each_with_index do |chunk, index|
15
- if index != 0
16
- new_text << ((index % 2 == 0) ? ">" : "<")
17
- end
18
-
19
- if index % 4 != 0
20
- new_text << chunk
21
- else
22
- new_text << chunk.gsub(Twitter::Regex[:auto_link_usernames_or_lists]) do
23
- before, at, user, slash_listname, after = $1, $2, $3, $4, $'
24
- if slash_listname
25
- # the link is a list
26
- "#{before}#{yield(at, user, slash_listname)}"
27
- else
28
- if after =~ Twitter::Regex[:end_screen_name_match]
29
- # Followed by something that means we don't autolink
30
- "#{before}#{at}#{user}#{slash_listname}"
31
- else
32
- # this is a screen name
33
- "#{before}#{yield(at, user, nil)}#{slash_listname}"
34
- end
35
- end
36
- end
37
- end
38
- end
39
-
40
- new_text
41
- end
42
-
43
- def rewrite_hashtags(text)
44
- text.to_s.gsub(Twitter::Regex[:auto_link_hashtags]) do
45
- before, hash, hashtag, after = $1, $2, $3, $'
46
- if after =~ Twitter::Regex[:end_hashtag_match]
47
- "#{before}#{hash}#{hashtag}"
48
- else
49
- "#{before}#{yield(hash, hashtag)}"
50
- end
51
- end
52
- end
53
-
54
- def rewrite_urls(text)
55
- text.to_s.gsub(Twitter::Regex[:valid_url]) do
56
- all, before, url, protocol, domain, path, query_string = $1, $2, $3, $4, $5, $6, $7
57
- if protocol && !protocol.empty?
58
- "#{before}#{yield(url)}"
59
- else
60
- all
61
- end
62
- end
63
- end
64
- end
65
- end