autolinker 0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,17 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+
4
+ begin
5
+ Bundler.setup(:default, :development)
6
+ rescue Bundler::BundlerError => e
7
+ $stderr.puts e.message
8
+ $stderr.puts "Run `bundle install` to install missing gems"
9
+ exit e.status_code
10
+ end
11
+ require 'minitest/unit'
12
+ require 'minitest/autorun'
13
+
14
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
15
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
16
+ require 'autolinker'
17
+
@@ -0,0 +1,270 @@
1
+ require 'helper'
2
+
3
+ class TestAutolinker < MiniTest::Unit::TestCase
4
+
5
+ def setup
6
+ @text_helper = Autolinker::TextHelper.new
7
+ end
8
+
9
+ def test_auto_link_within_tags
10
+ link_raw = 'http://www.rubyonrails.org/images/rails.png'
11
+ link_result = %Q(<img src="#{link_raw}" />)
12
+ assert_equal link_result, @text_helper.auto_link(link_result)
13
+ end
14
+
15
+ def test_auto_link_with_brackets
16
+ link1_raw = 'http://en.wikipedia.org/wiki/Sprite_(computer_graphics)'
17
+ link1_result = generate_result(link1_raw)
18
+ assert_equal link1_result, @text_helper.auto_link(link1_raw)
19
+ assert_equal "(link: #{link1_result})", @text_helper.auto_link("(link: #{link1_raw})")
20
+
21
+ link2_raw = 'http://en.wikipedia.org/wiki/Sprite_[computer_graphics]'
22
+ link2_result = generate_result(link2_raw)
23
+ assert_equal link2_result, @text_helper.auto_link(link2_raw)
24
+ assert_equal "[link: #{link2_result}]", @text_helper.auto_link("[link: #{link2_raw}]")
25
+
26
+ link3_raw = 'http://en.wikipedia.org/wiki/Sprite_{computer_graphics}'
27
+ link3_result = generate_result(link3_raw)
28
+ assert_equal link3_result, @text_helper.auto_link(link3_raw)
29
+ assert_equal "{link: #{link3_result}}", @text_helper.auto_link("{link: #{link3_raw}}")
30
+ end
31
+
32
+ def test_auto_link_with_options_hash
33
+ assert_equal 'Welcome to my new blog at <a class="menu" href="http://www.myblog.com/" target="_blank">http://www.myblog.com/</a>. Please e-mail me at <a class="menu" href="mailto:me@email.com" target="_blank">me@email.com</a>.',
34
+ @text_helper.auto_link("Welcome to my new blog at http://www.myblog.com/. Please e-mail me at me@email.com.",
35
+ :link => :all, :html => { :class => "menu", :target => "_blank" })
36
+ end
37
+
38
+ def test_auto_link_with_multiple_trailing_punctuations
39
+ url = "http://youtube.com"
40
+ url_result = generate_result(url)
41
+ assert_equal url_result, @text_helper.auto_link(url)
42
+ assert_equal "(link: #{url_result}).", @text_helper.auto_link("(link: #{url}).")
43
+ end
44
+
45
+ def test_auto_link_with_block
46
+ url = "http://api.rubyonrails.com/Foo.html"
47
+ email = "fantabulous@shiznadel.ic"
48
+
49
+ assert_equal %(<p><a href="#{url}">#{url[0...7]}...</a><br /><a href="mailto:#{email}">#{email[0...7]}...</a><br /></p>), @text_helper.auto_link("<p>#{url}<br />#{email}<br /></p>") { |_url| truncate(_url, 10) }
50
+ end
51
+
52
+ def test_auto_link_with_block_with_html
53
+ pic = "http://example.com/pic.png"
54
+ url = "http://example.com/album?a&b=c"
55
+
56
+ assert_equal %(My pic: <a href="#{pic}"><img src="#{pic}" width="160px"></a> -- full album here #{generate_result(url)}), @text_helper.auto_link("My pic: #{pic} -- full album here #{url}") { |link|
57
+ if link =~ /\.(jpg|gif|png|bmp|tif)$/i
58
+ %(<img src="#{link}" width="160px">)
59
+ else
60
+ link
61
+ end
62
+ }
63
+ end
64
+
65
+ def test_auto_link_should_sanitize_input_when_sanitize_option_is_not_false
66
+ link_raw = %{http://www.rubyonrails.com?id=1&num=2}
67
+ malicious_script = '<script>alert("malicious!")</script>'
68
+ assert_equal %{<a href="http://www.rubyonrails.com?id=1&num=2">http://www.rubyonrails.com?id=1&num=2</a>}, @text_helper.auto_link("#{link_raw}#{malicious_script}")
69
+ end
70
+
71
+ def test_auto_link_should_sanitize_input_with_sanitize_options
72
+ link_raw = %{http://www.rubyonrails.com?id=1&num=2}
73
+ malicious_script = '<script>alert("malicious!")</script>'
74
+ text_with_attributes = %{<a href="http://ruby-lang-org" target="_blank" data-malicious="inject">Ruby</a>}
75
+
76
+ text_result = %{<a class="big" href="http://www.rubyonrails.com?id=1&num=2">http://www.rubyonrails.com?id=1&num=2</a><a href="http://ruby-lang-org" target="_blank">Ruby</a>}
77
+ assert_equal text_result, @text_helper.auto_link("#{link_raw}#{malicious_script}#{text_with_attributes}",
78
+ :sanitize_options => { :attributes => ["target", "href"] },
79
+ :html => { :class => 'big' })
80
+ end
81
+
82
+ def test_auto_link_should_not_sanitize_input_when_sanitize_option_is_false
83
+ link_raw = %{http://www.rubyonrails.com?id=1&num=2}
84
+ malicious_script = '<script>alert("malicious!")</script>'
85
+
86
+ assert_equal %{<a href="http://www.rubyonrails.com?id=1&num=2">http://www.rubyonrails.com?id=1&num=2</a><script>alert("malicious!")</script>}, @text_helper.auto_link("#{link_raw}#{malicious_script}", :sanitize => false)
87
+ end
88
+
89
+ def test_auto_link_other_protocols
90
+ ftp_raw = 'ftp://example.com/file.txt'
91
+ assert_equal %(Download #{generate_result(ftp_raw)}), @text_helper.auto_link("Download #{ftp_raw}")
92
+
93
+ file_scheme = 'file:///home/username/RomeoAndJuliet.pdf'
94
+ assert_equal generate_result(file_scheme), @text_helper.auto_link(file_scheme)
95
+ end
96
+
97
+ def test_auto_link_already_linked
98
+ linked1 = generate_result('Ruby On Rails', 'http://www.rubyonrails.com')
99
+ linked2 = %('<a href="http://www.example.com">www.example.com</a>')
100
+ linked3 = %('<a href="http://www.example.com" rel="nofollow">www.example.com</a>')
101
+ linked4 = %('<a href="http://www.example.com"><b>www.example.com</b></a>')
102
+ linked5 = %('<a href="#close">close</a> <a href="http://www.example.com"><b>www.example.com</b></a>')
103
+ linked6 = %('<a href="#close">close</a> <a href="http://www.example.com" target="_blank" data-ruby="ror"><b>www.example.com</b></a>')
104
+ assert_equal linked1, @text_helper.auto_link(linked1)
105
+ assert_equal linked2, @text_helper.auto_link(linked2)
106
+ assert_equal linked3, @text_helper.auto_link(linked3, :sanitize => false)
107
+ assert_equal linked4, @text_helper.auto_link(linked4)
108
+ assert_equal linked5, @text_helper.auto_link(linked5)
109
+ assert_equal linked6, @text_helper.auto_link(linked6, :sanitize_options => { :attributes => ["href", "target", "data-ruby"] })
110
+
111
+ linked_email = %Q(<a href="mailto:david@loudthinking.com">Mail me</a>)
112
+ assert_equal linked_email, @text_helper.auto_link(linked_email)
113
+ end
114
+
115
+
116
+ def test_auto_link_at_eol
117
+ url1 = "http://api.rubyonrails.com/Foo.html"
118
+ url2 = "http://www.ruby-doc.org/core/Bar.html"
119
+
120
+ assert_equal %(<p><a href="#{url1}">#{url1}</a><br /><a href="#{url2}">#{url2}</a><br /></p>), @text_helper.auto_link("<p>#{url1}<br />#{url2}<br /></p>")
121
+ end
122
+
123
+ def test_auto_link
124
+ email_raw = 'david@loudthinking.com'
125
+ email_result = %{<a href="mailto:#{email_raw}">#{email_raw}</a>}
126
+ link_raw = 'http://www.rubyonrails.com'
127
+ link_result = generate_result(link_raw)
128
+ link_result_with_options = %{<a href="#{link_raw}" target="_blank">#{link_raw}</a>}
129
+
130
+ assert_equal '', @text_helper.auto_link(nil)
131
+ assert_equal '', @text_helper.auto_link('')
132
+ assert_equal "#{link_result} #{link_result} #{link_result}", @text_helper.auto_link("#{link_raw} #{link_raw} #{link_raw}")
133
+
134
+ assert_equal %(hello #{email_result}), @text_helper.auto_link("hello #{email_raw}", :email_addresses)
135
+ assert_equal %(Go to #{link_result}), @text_helper.auto_link("Go to #{link_raw}", :urls)
136
+ assert_equal %(Go to #{link_raw}), @text_helper.auto_link("Go to #{link_raw}", :email_addresses)
137
+ assert_equal %(Go to #{link_result} and say hello to #{email_result}), @text_helper.auto_link("Go to #{link_raw} and say hello to #{email_raw}")
138
+ assert_equal %(<p>Link #{link_result}</p>), @text_helper.auto_link("<p>Link #{link_raw}</p>")
139
+ assert_equal %(<p>#{link_result} Link</p>), @text_helper.auto_link("<p>#{link_raw} Link</p>")
140
+ assert_equal %(<p>Link #{link_result_with_options}</p>), @text_helper.auto_link("<p>Link #{link_raw}</p>", :all, { :target => "_blank" })
141
+ assert_equal %(Go to #{link_result}.), @text_helper.auto_link(%(Go to #{link_raw}.))
142
+ assert_equal %(<p>Go to #{link_result}, then say hello to #{email_result}.</p>), @text_helper.auto_link(%(<p>Go to #{link_raw}, then say hello to #{email_raw}.</p>))
143
+ assert_equal %(#{link_result} #{link_result}), @text_helper.auto_link(%(#{link_result} #{link_raw}))
144
+
145
+ email2_raw = '+david@loudthinking.com'
146
+ email2_result = %{<a href="mailto:#{email2_raw}">#{email2_raw}</a>}
147
+ assert_equal email2_result, @text_helper.auto_link(email2_raw)
148
+ assert_equal email2_result, @text_helper.auto_link(email2_raw, :all)
149
+ assert_equal email2_result, @text_helper.auto_link(email2_raw, :email_addresses)
150
+
151
+ link2_raw = 'www.rubyonrails.com'
152
+ link2_result = generate_result(link2_raw, "http://#{link2_raw}")
153
+ assert_equal %(Go to #{link2_result}), @text_helper.auto_link("Go to #{link2_raw}", :urls)
154
+ assert_equal %(Go to #{link2_raw}), @text_helper.auto_link("Go to #{link2_raw}", :email_addresses)
155
+ assert_equal %(<p>Link #{link2_result}</p>), @text_helper.auto_link("<p>Link #{link2_raw}</p>")
156
+ assert_equal %(<p>#{link2_result} Link</p>), @text_helper.auto_link("<p>#{link2_raw} Link</p>")
157
+ assert_equal %(Go to #{link2_result}.), @text_helper.auto_link(%(Go to #{link2_raw}.))
158
+ assert_equal %(<p>Say hello to #{email_result}, then go to #{link2_result}.</p>), @text_helper.auto_link(%(<p>Say hello to #{email_raw}, then go to #{link2_raw}.</p>))
159
+
160
+ link3_raw = 'http://manuals.ruby-on-rails.com/read/chapter.need_a-period/103#page281'
161
+ link3_result = generate_result(link3_raw)
162
+ assert_equal %(Go to #{link3_result}), @text_helper.auto_link("Go to #{link3_raw}", :urls)
163
+ assert_equal %(Go to #{link3_raw}), @text_helper.auto_link("Go to #{link3_raw}", :email_addresses)
164
+ assert_equal %(<p>Link #{link3_result}</p>), @text_helper.auto_link("<p>Link #{link3_raw}</p>")
165
+ assert_equal %(<p>#{link3_result} Link</p>), @text_helper.auto_link("<p>#{link3_raw} Link</p>")
166
+ assert_equal %(Go to #{link3_result}.), @text_helper.auto_link(%(Go to #{link3_raw}.))
167
+ assert_equal %(<p>Go to #{link3_result}. Seriously, #{link3_result}? I think I'll say hello to #{email_result}. Instead.</p>),
168
+ @text_helper.auto_link(%(<p>Go to #{link3_raw}. Seriously, #{link3_raw}? I think I'll say hello to #{email_raw}. Instead.</p>))
169
+
170
+ link4_raw = 'http://foo.example.com/controller/action?parm=value&p2=v2#anchor123'
171
+ link4_result = generate_result(link4_raw)
172
+ assert_equal %(<p>Link #{link4_result}</p>), @text_helper.auto_link("<p>Link #{link4_raw}</p>")
173
+ assert_equal %(<p>#{link4_result} Link</p>), @text_helper.auto_link("<p>#{link4_raw} Link</p>")
174
+
175
+ link5_raw = 'http://foo.example.com:3000/controller/action'
176
+ link5_result = generate_result(link5_raw)
177
+ assert_equal %(<p>#{link5_result} Link</p>), @text_helper.auto_link("<p>#{link5_raw} Link</p>")
178
+
179
+ link6_raw = 'http://foo.example.com:3000/controller/action+pack'
180
+ link6_result = generate_result(link6_raw)
181
+ assert_equal %(<p>#{link6_result} Link</p>), @text_helper.auto_link("<p>#{link6_raw} Link</p>")
182
+
183
+ link7_raw = 'http://foo.example.com/controller/action?parm=value&p2=v2#anchor-123'
184
+ link7_result = generate_result(link7_raw)
185
+ assert_equal %(<p>#{link7_result} Link</p>), @text_helper.auto_link("<p>#{link7_raw} Link</p>")
186
+
187
+ link8_raw = 'http://foo.example.com:3000/controller/action.html'
188
+ link8_result = generate_result(link8_raw)
189
+ assert_equal %(Go to #{link8_result}), @text_helper.auto_link("Go to #{link8_raw}", :urls)
190
+ assert_equal %(Go to #{link8_raw}), @text_helper.auto_link("Go to #{link8_raw}", :email_addresses)
191
+ assert_equal %(<p>Link #{link8_result}</p>), @text_helper.auto_link("<p>Link #{link8_raw}</p>")
192
+ assert_equal %(<p>#{link8_result} Link</p>), @text_helper.auto_link("<p>#{link8_raw} Link</p>")
193
+ assert_equal %(Go to #{link8_result}.), @text_helper.auto_link(%(Go to #{link8_raw}.))
194
+ assert_equal %(<p>Go to #{link8_result}. Seriously, #{link8_result}? I think I'll say hello to #{email_result}. Instead.</p>),
195
+ @text_helper.auto_link(%(<p>Go to #{link8_raw}. Seriously, #{link8_raw}? I think I'll say hello to #{email_raw}. Instead.</p>))
196
+
197
+ link9_raw = 'http://business.timesonline.co.uk/article/0,,9065-2473189,00.html'
198
+ link9_result = generate_result(link9_raw)
199
+ assert_equal %(Go to #{link9_result}), @text_helper.auto_link("Go to #{link9_raw}", :urls)
200
+ assert_equal %(Go to #{link9_raw}), @text_helper.auto_link("Go to #{link9_raw}", :email_addresses)
201
+ assert_equal %(<p>Link #{link9_result}</p>), @text_helper.auto_link("<p>Link #{link9_raw}</p>")
202
+ assert_equal %(<p>#{link9_result} Link</p>), @text_helper.auto_link("<p>#{link9_raw} Link</p>")
203
+ assert_equal %(Go to #{link9_result}.), @text_helper.auto_link(%(Go to #{link9_raw}.))
204
+ assert_equal %(<p>Go to #{link9_result}. Seriously, #{link9_result}? I think I'll say hello to #{email_result}. Instead.</p>),
205
+ @text_helper.auto_link(%(<p>Go to #{link9_raw}. Seriously, #{link9_raw}? I think I'll say hello to #{email_raw}. Instead.</p>))
206
+
207
+ link10_raw = 'http://www.mail-archive.com/ruby-talk@ruby-lang.org/'
208
+ link10_result = generate_result(link10_raw)
209
+ assert_equal %(<p>#{link10_result} Link</p>), @text_helper.auto_link("<p>#{link10_raw} Link</p>")
210
+
211
+ link11_raw = 'http://asakusa.rubyist.net/'
212
+ link11_result = generate_result(link11_raw)
213
+ assert_equal %(浅草.rbの公式サイトはこちら#{link11_result}), @text_helper.auto_link("浅草.rbの公式サイトはこちら#{link11_raw}")
214
+
215
+ link12_raw = 'http://tools.ietf.org/html/rfc3986'
216
+ link12_result = generate_result(link12_raw)
217
+ assert_equal %(<p>#{link12_result} text-after-nonbreaking-space</p>), @text_helper.auto_link("<p>#{link12_raw} text-after-nonbreaking-space</p>")
218
+ end
219
+
220
+ def test_auto_link_parsing
221
+ urls = %w(
222
+ http://www.rubyonrails.com
223
+ http://www.rubyonrails.com:80
224
+ http://www.rubyonrails.com/~minam
225
+ https://www.rubyonrails.com/~minam
226
+ http://www.rubyonrails.com/~minam/url%20with%20spaces
227
+ http://www.rubyonrails.com/foo.cgi?something=here
228
+ http://www.rubyonrails.com/foo.cgi?something=here&and=here
229
+ http://www.rubyonrails.com/contact;new
230
+ http://www.rubyonrails.com/contact;new%20with%20spaces
231
+ http://www.rubyonrails.com/contact;new?with=query&string=params
232
+ http://www.rubyonrails.com/~minam/contact;new?with=query&string=params
233
+ http://en.wikipedia.org/wiki/Wikipedia:Today%27s_featured_picture_%28animation%29/January_20%2C_2007
234
+ http://www.mail-archive.com/rails@lists.rubyonrails.org/
235
+ http://www.amazon.com/Testing-Equal-Sign-In-Path/ref=pd_bbs_sr_1?ie=UTF8&s=books&qid=1198861734&sr=8-1
236
+ http://en.wikipedia.org/wiki/Texas_hold'em
237
+ https://www.google.com/doku.php?id=gps:resource:scs:start
238
+ http://connect.oraclecorp.com/search?search[q]=green+france&search[type]=Group
239
+ http://of.openfoundry.org/projects/492/download#4th.Release.3
240
+ http://maps.google.co.uk/maps?f=q&q=the+london+eye&ie=UTF8&ll=51.503373,-0.11939&spn=0.007052,0.012767&z=16&iwloc=A
241
+ http://около.кола/колокола
242
+ )
243
+
244
+ urls.each do |url|
245
+ assert_equal generate_result(url), @text_helper.auto_link(url)
246
+ end
247
+ end
248
+
249
+ private
250
+ def generate_result(link_text, href = nil, escape = false)
251
+ href ||= link_text
252
+ if escape
253
+ %{<a href="#{CGI::escapeHTML href}">#{CGI::escapeHTML link_text}</a>}
254
+ else
255
+ %{<a href="#{href}">#{link_text}</a>}
256
+ end
257
+ end
258
+
259
+ def truncate(text, length, options = {})
260
+ options[:omission] ||= "..."
261
+
262
+ length_with_room_for_omission = length - options[:omission].length
263
+
264
+ stop = options[:separator] ?
265
+ (text.rindex(options[:separator], length_with_room_for_omission) || length_with_room_for_omission) : length_with_room_for_omission
266
+
267
+ (text.length > length ? text[0...stop] + options[:omission] : text).to_s
268
+ end
269
+
270
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: autolinker
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - Helmut Juskewycz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-10-08 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: |2
14
+ An autolinking library that acts as a standalone replacement for Rails `auto_link`
15
+ email: helmut@juskewycz
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - README.md
21
+ - Rakefile
22
+ - lib/autolinker.rb
23
+ - lib/autolinker/text_helper.rb
24
+ - lib/autolinker/html/node.rb
25
+ - lib/autolinker/html/sanitizer.rb
26
+ - lib/autolinker/html/tokenizer.rb
27
+ - autolinker.gemspec
28
+ - test/helper.rb
29
+ - test/test_autolinker.rb
30
+ homepage: http://github.com/hjuskewycz/autolinker
31
+ licenses:
32
+ - MIT
33
+ metadata: {}
34
+ post_install_message:
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirements: []
49
+ rubyforge_project:
50
+ rubygems_version: 2.0.3
51
+ signing_key:
52
+ specification_version: 4
53
+ summary: Mostly autolinking
54
+ test_files:
55
+ - test/test_autolinker.rb