zendesk-rinku 1.7.0

Sign up to get free protection for your applications and to get access to all the features.
data/ext/rinku/rinku.h ADDED
@@ -0,0 +1,7 @@
1
+ #ifndef UPSKIRT_RINKU_H
2
+ #define UPSKIRT_RINKU_H
3
+
4
+ extern const VALUE
5
+ allow_short_domains(void);
6
+
7
+ #endif /* UPSKIRT_RINKU_H */
@@ -0,0 +1,29 @@
1
+ require 'rinku'
2
+
3
+ module RailsRinku
4
+ def rinku_auto_link(text, *args, &block)
5
+ return '' if text.blank?
6
+
7
+ options = args.size == 2 ? {} : args.extract_options!
8
+ unless args.empty?
9
+ options[:link] = args[0] || :all
10
+ options[:html] = args[1] || {}
11
+ options[:skip] = args[2]
12
+ end
13
+ options.reverse_merge!(:link => :all, :html => {})
14
+ text = h(text) unless text.html_safe?
15
+
16
+ Rinku.auto_link(
17
+ text,
18
+ options[:link],
19
+ tag_options(options[:html]),
20
+ options[:skip],
21
+ &block
22
+ ).html_safe
23
+ end
24
+ end
25
+
26
+ module ActionView::Helpers::TextHelper
27
+ include RailsRinku
28
+ alias_method :auto_link, :rinku_auto_link
29
+ end
data/lib/rinku.rb ADDED
@@ -0,0 +1,7 @@
1
+ module Rinku
2
+ VERSION = "1.7.0"
3
+ attr_accessor :skip_tags
4
+ extend self
5
+ end
6
+
7
+ require 'rinku.so'
data/rinku.gemspec ADDED
@@ -0,0 +1,38 @@
1
+ # encoding: UTF-8
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'zendesk-rinku'
5
+ s.version = '1.7.0'
6
+ s.summary = "Mostly autolinking"
7
+ s.description = <<-EOF
8
+ A fast and very smart autolinking library that
9
+ acts as a drop-in replacement for Rails `auto_link`
10
+
11
+ This is just a fork of vmg's lib with a small patch we need.
12
+ EOF
13
+ s.email = 'shajith@zendesk.com'
14
+ s.homepage = 'http://github.com/shajith/rinku'
15
+ s.authors = ["Shajith Chacko", "Vicent Marti"]
16
+ # = MANIFEST =
17
+ s.files = %w[
18
+ COPYING
19
+ README.markdown
20
+ Rakefile
21
+ ext/rinku/rinku.c
22
+ ext/rinku/rinku.h
23
+ ext/rinku/autolink.c
24
+ ext/rinku/autolink.h
25
+ ext/rinku/buffer.c
26
+ ext/rinku/buffer.h
27
+ ext/rinku/extconf.rb
28
+ lib/rinku.rb
29
+ lib/rails_rinku.rb
30
+ rinku.gemspec
31
+ test/autolink_test.rb
32
+ ]
33
+ # = MANIFEST =
34
+ s.test_files = ["test/autolink_test.rb"]
35
+ s.extra_rdoc_files = ["COPYING"]
36
+ s.extensions = ["ext/rinku/extconf.rb"]
37
+ s.require_paths = ["lib"]
38
+ end
@@ -0,0 +1,295 @@
1
+ # encoding: utf-8
2
+ rootdir = File.dirname(File.dirname(__FILE__))
3
+ $LOAD_PATH.unshift "#{rootdir}/lib"
4
+
5
+ require 'test/unit'
6
+ require 'cgi'
7
+ require 'uri'
8
+ require 'rinku'
9
+
10
+ class RedcarpetAutolinkTest < Test::Unit::TestCase
11
+
12
+ SAFE_CHARS = "{}[]~'"
13
+
14
+ def assert_linked(expected, url)
15
+ assert_equal expected, Rinku.auto_link(url)
16
+ end
17
+
18
+ def test_escapes_quotes
19
+ assert_linked %(<a href="http://website.com/&quot;onmouseover=document.body.style.backgroundColor=&quot;pink&quot;;//">http://website.com/"onmouseover=document.body.style.backgroundColor="pink";//</a>),
20
+ %(http://website.com/"onmouseover=document.body.style.backgroundColor="pink";//)
21
+ end
22
+
23
+ def test_global_skip_tags
24
+ assert_equal Rinku.skip_tags, nil
25
+ Rinku.skip_tags = ['pre']
26
+ assert_equal Rinku.skip_tags, ['pre']
27
+
28
+ Rinku.skip_tags = ['pa']
29
+ url = 'This is just a <pa>http://www.pokemon.com</pa> test'
30
+ assert_equal Rinku.auto_link(url), url
31
+
32
+ Rinku.skip_tags = nil
33
+ assert_not_equal Rinku.auto_link(url), url
34
+ end
35
+
36
+ def test_auto_link_with_single_trailing_punctuation_and_space
37
+ url = "http://www.youtube.com"
38
+ url_result = generate_result(url)
39
+ assert_equal url_result, Rinku.auto_link(url)
40
+ assert_equal "link: #{url_result}. foo?", Rinku.auto_link("link: #{url}. foo?")
41
+ end
42
+
43
+ def test_does_not_segfault
44
+ assert_linked "< this is just a test", "< this is just a test"
45
+ end
46
+
47
+ def test_skips_tags
48
+ html = <<-html
49
+ This is just a test. http://www.pokemon.com
50
+ <div>
51
+ More test
52
+ http://www.amd.com
53
+ </div>
54
+ <pre>
55
+ CODE www.less.es
56
+ </pre>
57
+ html
58
+
59
+ result = <<-result
60
+ This is just a test. <a href="http://www.pokemon.com">http://www.pokemon.com</a>
61
+ <div>
62
+ More test
63
+ http://www.amd.com
64
+ </div>
65
+ <pre>
66
+ CODE <a href="http://www.less.es">www.less.es</a>
67
+ </pre>
68
+ result
69
+ assert_equal result, Rinku.auto_link(html, :all, nil, ["div", "a"])
70
+ end
71
+
72
+ def test_auto_link_with_brackets
73
+ link1_raw = 'http://en.wikipedia.org/wiki/Sprite_(computer_graphics)'
74
+ link1_result = generate_result(link1_raw)
75
+ assert_equal link1_result, Rinku.auto_link(link1_raw)
76
+ assert_equal "(link: #{link1_result})", Rinku.auto_link("(link: #{link1_raw})")
77
+
78
+ link2_raw = 'http://en.wikipedia.org/wiki/Sprite_[computer_graphics]'
79
+ link2_result = generate_result(link2_raw)
80
+ assert_equal link2_result, Rinku.auto_link(link2_raw)
81
+ assert_equal "[link: #{link2_result}]", Rinku.auto_link("[link: #{link2_raw}]")
82
+
83
+ link3_raw = 'http://en.wikipedia.org/wiki/Sprite_{computer_graphics}'
84
+ link3_result = generate_result(link3_raw)
85
+ assert_equal link3_result, Rinku.auto_link(link3_raw)
86
+ assert_equal "{link: #{link3_result}}", Rinku.auto_link("{link: #{link3_raw}}")
87
+ end
88
+
89
+ def test_auto_link_with_multiple_trailing_punctuations
90
+ url = "http://youtube.com"
91
+ url_result = generate_result(url)
92
+ assert_equal url_result, Rinku.auto_link(url)
93
+ assert_equal "(link: #{url_result}).", Rinku.auto_link("(link: #{url}).")
94
+ end
95
+
96
+ def test_auto_link_with_block
97
+ url = "http://api.rubyonrails.com/Foo.html"
98
+ email = "fantabulous@shiznadel.ic"
99
+
100
+ assert_equal %(<p><a href="#{url}">#{url[0...7]}...</a><br /><a href="mailto:#{email}">#{email[0...7]}...</a><br /></p>), Rinku.auto_link("<p>#{url}<br />#{email}<br /></p>") { |_url| _url[0...7] + '...'}
101
+ end
102
+
103
+ def test_auto_link_with_block_with_html
104
+ pic = "http://example.com/pic.png"
105
+ url = "http://example.com/album?a&b=c"
106
+
107
+ expect = %(My pic: <a href="#{pic}"><img src="#{pic}" width="160px"></a> -- full album here #{generate_result(url)})
108
+ text = "My pic: #{pic} -- full album here #{CGI.escapeHTML url}"
109
+
110
+ assert_equal expect, Rinku.auto_link(text) { |link|
111
+ if link =~ /\.(jpg|gif|png|bmp|tif)$/i
112
+ %(<img src="#{link}" width="160px">)
113
+ else
114
+ link
115
+ end
116
+ }
117
+ end
118
+
119
+ def test_auto_link_already_linked
120
+ linked1 = generate_result('Ruby On Rails', 'http://www.rubyonrails.com')
121
+ linked2 = %('<a href="http://www.example.com">www.example.com</a>')
122
+ linked3 = %('<a href="http://www.example.com" rel="nofollow">www.example.com</a>')
123
+ linked4 = %('<a href="http://www.example.com"><b>www.example.com</b></a>')
124
+ linked5 = %('<a href="#close">close</a> <a href="http://www.example.com"><b>www.example.com</b></a>')
125
+ assert_equal linked1, Rinku.auto_link(linked1)
126
+ assert_equal linked2, Rinku.auto_link(linked2)
127
+ assert_equal linked3, Rinku.auto_link(linked3)
128
+ assert_equal linked4, Rinku.auto_link(linked4)
129
+ assert_equal linked5, Rinku.auto_link(linked5)
130
+
131
+ linked_email = %Q(<a href="mailto:david@loudthinking.com">Mail me</a>)
132
+ assert_equal linked_email, Rinku.auto_link(linked_email)
133
+ end
134
+
135
+
136
+ def test_auto_link_at_eol
137
+ url1 = "http://api.rubyonrails.com/Foo.html"
138
+ url2 = "http://www.ruby-doc.org/core/Bar.html"
139
+
140
+ assert_equal %(<p><a href="#{url1}">#{url1}</a><br /><a href="#{url2}">#{url2}</a><br /></p>), Rinku.auto_link("<p>#{url1}<br />#{url2}<br /></p>")
141
+ end
142
+
143
+ def test_block
144
+ link = Rinku.auto_link("Find ur favorite pokeman @ http://www.pokemon.com") do |url|
145
+ assert_equal url, "http://www.pokemon.com"
146
+ "POKEMAN WEBSITE"
147
+ end
148
+
149
+ assert_equal link, "Find ur favorite pokeman @ <a href=\"http://www.pokemon.com\">POKEMAN WEBSITE</a>"
150
+ end
151
+
152
+ def test_autolink_works
153
+ url = "http://example.com/"
154
+ assert_linked "<a href=\"#{url}\">#{url}</a>", url
155
+ end
156
+
157
+ def test_autolink_options_for_short_domains
158
+ url = "http://google"
159
+ linked_url = "<a href=\"#{url}\">#{url}</a>"
160
+ flags = Rinku::AUTOLINK_SHORT_DOMAINS
161
+
162
+ # Specifying use short_domains in the args
163
+ url = "http://google"
164
+ linked_url = "<a href=\"#{url}\">#{url}</a>"
165
+ assert_equal Rinku.auto_link(url, nil, nil, nil, flags), linked_url
166
+
167
+ # Specifying no short_domains in the args
168
+ url = "http://google"
169
+ linked_url = "<a href=\"#{url}\">#{url}</a>"
170
+ assert_equal Rinku.auto_link(url, nil, nil, nil, 0), url
171
+ end
172
+
173
+ def test_not_autolink_www
174
+ assert_linked "Awww... man", "Awww... man"
175
+ end
176
+
177
+ def test_does_not_terminate_on_dash
178
+ url = "http://example.com/Notification_Center-GitHub-20101108-140050.jpg"
179
+ assert_linked "<a href=\"#{url}\">#{url}</a>", url
180
+ end
181
+
182
+ def test_does_not_include_trailing_gt
183
+ url = "http://example.com"
184
+ assert_linked "&lt;<a href=\"#{url}\">#{url}</a>&gt;", "&lt;#{url}&gt;"
185
+ end
186
+
187
+ def test_links_with_anchors
188
+ url = "https://github.com/github/hubot/blob/master/scripts/cream.js#L20-20"
189
+ assert_linked "<a href=\"#{url}\">#{url}</a>", url
190
+ end
191
+
192
+ def test_links_like_rails
193
+ urls = %w(http://www.rubyonrails.com
194
+ http://www.rubyonrails.com:80
195
+ http://www.rubyonrails.com/~minam
196
+ https://www.rubyonrails.com/~minam
197
+ http://www.rubyonrails.com/~minam/url%20with%20spaces
198
+ http://www.rubyonrails.com/foo.cgi?something=here
199
+ http://www.rubyonrails.com/foo.cgi?something=here&and=here
200
+ http://www.rubyonrails.com/contact;new
201
+ http://www.rubyonrails.com/contact;new%20with%20spaces
202
+ http://www.rubyonrails.com/contact;new?with=query&string=params
203
+ http://www.rubyonrails.com/~minam/contact;new?with=query&string=params
204
+ http://en.wikipedia.org/wiki/Wikipedia:Today%27s_featured_picture_%28animation%29/January_20%2C_2007
205
+ http://www.mail-archive.com/rails@lists.rubyonrails.org/
206
+ http://www.amazon.com/Testing-Equal-Sign-In-Path/ref=pd_bbs_sr_1?ie=UTF8&s=books&qid=1198861734&sr=8-1
207
+ http://en.wikipedia.org/wiki/Sprite_(computer_graphics)
208
+ http://en.wikipedia.org/wiki/Texas_hold%27em
209
+ https://www.google.com/doku.php?id=gps:resource:scs:start
210
+ )
211
+
212
+ urls.each do |url|
213
+ assert_linked %(<a href="#{CGI.escapeHTML url}">#{CGI.escapeHTML url}</a>), CGI.escapeHTML(url)
214
+ end
215
+ end
216
+
217
+ def test_links_like_autolink_rails
218
+ email_raw = 'david@loudthinking.com'
219
+ email_result = %{<a href="mailto:#{email_raw}">#{email_raw}</a>}
220
+ email2_raw = '+david@loudthinking.com'
221
+ email2_result = %{<a href="mailto:#{email2_raw}">#{email2_raw}</a>}
222
+ link_raw = 'http://www.rubyonrails.com'
223
+ link_result = %{<a href="#{link_raw}">#{link_raw}</a>}
224
+ link_result_with_options = %{<a href="#{link_raw}" target="_blank">#{link_raw}</a>}
225
+ link2_raw = 'www.rubyonrails.com'
226
+ link2_result = %{<a href="http://#{link2_raw}">#{link2_raw}</a>}
227
+ link3_raw = 'http://manuals.ruby-on-rails.com/read/chapter.need_a-period/103#page281'
228
+ link3_result = %{<a href="#{link3_raw}">#{link3_raw}</a>}
229
+ link4_raw = CGI.escapeHTML 'http://foo.example.com/controller/action?parm=value&p2=v2#anchor123'
230
+ link4_result = %{<a href="#{link4_raw}">#{link4_raw}</a>}
231
+ link5_raw = 'http://foo.example.com:3000/controller/action'
232
+ link5_result = %{<a href="#{link5_raw}">#{link5_raw}</a>}
233
+ link6_raw = 'http://foo.example.com:3000/controller/action+pack'
234
+ link6_result = %{<a href="#{link6_raw}">#{link6_raw}</a>}
235
+ link7_raw = CGI.escapeHTML 'http://foo.example.com/controller/action?parm=value&p2=v2#anchor-123'
236
+ link7_result = %{<a href="#{link7_raw}">#{link7_raw}</a>}
237
+ link8_raw = 'http://foo.example.com:3000/controller/action.html'
238
+ link8_result = %{<a href="#{link8_raw}">#{link8_raw}</a>}
239
+ link9_raw = 'http://business.timesonline.co.uk/article/0,,9065-2473189,00.html'
240
+ link9_result = %{<a href="#{link9_raw}">#{link9_raw}</a>}
241
+ link10_raw = 'http://www.mail-archive.com/ruby-talk@ruby-lang.org/'
242
+ link10_result = %{<a href="#{link10_raw}">#{link10_raw}</a>}
243
+
244
+ assert_linked %(Go to #{link_result} and say hello to #{email_result}), "Go to #{link_raw} and say hello to #{email_raw}"
245
+ assert_linked %(<p>Link #{link_result}</p>), "<p>Link #{link_raw}</p>"
246
+ assert_linked %(<p>#{link_result} Link</p>), "<p>#{link_raw} Link</p>"
247
+ assert_linked %(Go to #{link_result}.), %(Go to #{link_raw}.)
248
+ assert_linked %(<p>Go to #{link_result}, then say hello to #{email_result}.</p>), %(<p>Go to #{link_raw}, then say hello to #{email_raw}.</p>)
249
+ assert_linked %(<p>Link #{link2_result}</p>), "<p>Link #{link2_raw}</p>"
250
+ assert_linked %(<p>#{link2_result} Link</p>), "<p>#{link2_raw} Link</p>"
251
+ assert_linked %(Go to #{link2_result}.), %(Go to #{link2_raw}.)
252
+ assert_linked %(<p>Say hello to #{email_result}, then go to #{link2_result},</p>), %(<p>Say hello to #{email_raw}, then go to #{link2_raw},</p>)
253
+ assert_linked %(<p>Link #{link3_result}</p>), "<p>Link #{link3_raw}</p>"
254
+ assert_linked %(<p>#{link3_result} Link</p>), "<p>#{link3_raw} Link</p>"
255
+ assert_linked %(Go to #{link3_result}.), %(Go to #{link3_raw}.)
256
+ assert_linked %(<p>Go to #{link3_result}. seriously, #{link3_result}? i think I'll say hello to #{email_result}. instead.</p>), %(<p>Go to #{link3_raw}. seriously, #{link3_raw}? i think I'll say hello to #{email_raw}. instead.</p>)
257
+ assert_linked %(<p>Link #{link4_result}</p>), "<p>Link #{link4_raw}</p>"
258
+ assert_linked %(<p>#{link4_result} Link</p>), "<p>#{link4_raw} Link</p>"
259
+ assert_linked %(<p>#{link5_result} Link</p>), "<p>#{link5_raw} Link</p>"
260
+ assert_linked %(<p>#{link6_result} Link</p>), "<p>#{link6_raw} Link</p>"
261
+ assert_linked %(<p>#{link7_result} Link</p>), "<p>#{link7_raw} Link</p>"
262
+ assert_linked %(<p>Link #{link8_result}</p>), "<p>Link #{link8_raw}</p>"
263
+ assert_linked %(<p>#{link8_result} Link</p>), "<p>#{link8_raw} Link</p>"
264
+ assert_linked %(Go to #{link8_result}.), %(Go to #{link8_raw}.)
265
+ assert_linked %(<p>Go to #{link8_result}. seriously, #{link8_result}? i think I'll say hello to #{email_result}. instead.</p>), %(<p>Go to #{link8_raw}. seriously, #{link8_raw}? i think I'll say hello to #{email_raw}. instead.</p>)
266
+ assert_linked %(<p>Link #{link9_result}</p>), "<p>Link #{link9_raw}</p>"
267
+ assert_linked %(<p>#{link9_result} Link</p>), "<p>#{link9_raw} Link</p>"
268
+ assert_linked %(Go to #{link9_result}.), %(Go to #{link9_raw}.)
269
+ assert_linked %(<p>Go to #{link9_result}. seriously, #{link9_result}? i think I'll say hello to #{email_result}. instead.</p>), %(<p>Go to #{link9_raw}. seriously, #{link9_raw}? i think I'll say hello to #{email_raw}. instead.</p>)
270
+ assert_linked %(<p>#{link10_result} Link</p>), "<p>#{link10_raw} Link</p>"
271
+ assert_linked email2_result, email2_raw
272
+ assert_linked "#{link_result} #{link_result} #{link_result}", "#{link_raw} #{link_raw} #{link_raw}"
273
+ assert_linked '<a href="http://www.rubyonrails.com">Ruby On Rails</a>', '<a href="http://www.rubyonrails.com">Ruby On Rails</a>'
274
+ end
275
+
276
+ if "".respond_to?(:force_encoding)
277
+ def test_copies_source_encoding
278
+ str = "http://www.bash.org"
279
+
280
+ ret = Rinku.auto_link str
281
+ assert_equal str.encoding, ret.encoding
282
+
283
+ str.encode! 'binary'
284
+
285
+ ret = Rinku.auto_link str
286
+ assert_equal str.encoding, ret.encoding
287
+ end
288
+ end
289
+
290
+ def generate_result(link_text, href = nil)
291
+ href ||= link_text
292
+ %{<a href="#{CGI.escapeHTML href}">#{CGI.escapeHTML link_text}</a>}
293
+ end
294
+
295
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zendesk-rinku
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.7.0
5
+ platform: ruby
6
+ authors:
7
+ - Shajith Chacko
8
+ - Vicent Marti
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-03-31 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: ! " A fast and very smart autolinking library that\n acts as a
15
+ drop-in replacement for Rails `auto_link`\n\n This is just a fork of vmg's lib
16
+ with a small patch we need.\n"
17
+ email: shajith@zendesk.com
18
+ executables: []
19
+ extensions:
20
+ - ext/rinku/extconf.rb
21
+ extra_rdoc_files:
22
+ - COPYING
23
+ files:
24
+ - COPYING
25
+ - README.markdown
26
+ - Rakefile
27
+ - ext/rinku/autolink.c
28
+ - ext/rinku/autolink.h
29
+ - ext/rinku/buffer.c
30
+ - ext/rinku/buffer.h
31
+ - ext/rinku/extconf.rb
32
+ - ext/rinku/rinku.c
33
+ - ext/rinku/rinku.h
34
+ - lib/rails_rinku.rb
35
+ - lib/rinku.rb
36
+ - rinku.gemspec
37
+ - test/autolink_test.rb
38
+ homepage: http://github.com/shajith/rinku
39
+ licenses: []
40
+ metadata: {}
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ! '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubyforge_project:
57
+ rubygems_version: 2.2.2
58
+ signing_key:
59
+ specification_version: 4
60
+ summary: Mostly autolinking
61
+ test_files:
62
+ - test/autolink_test.rb