rack-linkify 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,2 +1,10 @@
1
+ 0.0.2 (July 18, 2010)
2
+ * BUGFIX: No longer messes with existing hrefs.
3
+ * BUGFIX: No longer linkifies URLs in form elements.
4
+ * BUGFIX: Better URL matching in general.
5
+ * Added automated tests.
6
+
7
+ 0.0.1 (December 3, 2009)
8
+
1
9
  0.0.0 (December 3, 2009)
2
10
  * Initial release.
data/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License
2
2
 
3
- Copyright (c) 2009 Wyatt M. Greene
3
+ Copyright (c) 2009, 2010 Wyatt M. Greene
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
@@ -2,10 +2,9 @@
2
2
 
3
3
  == Description
4
4
 
5
- Dead links are magically
6
- transformed into live links! Rack::Linkify is very generous in what it
7
- considers a link; it will turn http://www.google.com, www.google.com, and
8
- just plain google.com into a link.
5
+ Dead links are magically transformed into live links! Rack::Linkify is
6
+ very generous in what it considers a link; it will turn http://www.google.com,
7
+ www.google.com, and just plain google.com into a link.
9
8
 
10
9
  Rack::Linkify can also turn text that looks like @this into a twitter link.
11
10
 
data/Rakefile CHANGED
@@ -1,12 +1,22 @@
1
1
  require 'rake'
2
+ require 'rake/testtask'
2
3
  require 'rubygems'
3
4
 
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test Rack::Linkify'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.pattern = 'test/**/*_test.rb'
12
+ t.verbose = true
13
+ end
14
+
4
15
  begin
5
16
  require 'jeweler'
6
17
  Jeweler::Tasks.new do |s|
7
18
  s.name = "rack-linkify"
8
- s.version = "0.0.1"
9
- s.add_dependency 'rack-plastic', '>= 0.0.3'
19
+ s.version = "0.0.2"
10
20
  s.author = "Wyatt Greene"
11
21
  s.email = "techiferous@gmail.com"
12
22
  s.summary = "Rack middleware that adds anchor tags to URLs in text."
@@ -14,6 +24,8 @@ begin
14
24
  Any URLs that occur in the text of the web page are automatically surrounded
15
25
  by an anchor tag.
16
26
  }
27
+ s.add_dependency('rack-plastic', '>= 0.1.1')
28
+ s.add_development_dependency('redgreen')
17
29
  s.require_path = "lib"
18
30
  s.files = []
19
31
  s.files << "README"
@@ -28,5 +40,5 @@ begin
28
40
  end
29
41
  Jeweler::GemcutterTasks.new
30
42
  rescue LoadError
31
- puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
43
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
32
44
  end
@@ -16,18 +16,17 @@ module Rack
16
16
 
17
17
  def find_candidate_links(doc)
18
18
  doc.at_css("body").traverse do |node|
19
- if node.text?
19
+ if node.text? && node.parent.name != 'textarea' && node.parent.name != 'option'
20
20
  update_text(node, mark_links(node.content))
21
21
  end
22
22
  end
23
23
  end
24
24
 
25
25
  def linkify(html)
26
+ html.gsub!(/beginninganchor1(?!http)/, 'beginninganchor1http://')
26
27
  html.gsub!('beginninganchor1', '<a href="')
27
28
  html.gsub!('beginninganchor2', '">')
28
29
  html.gsub!('endinganchor', '</a>')
29
- # if an href URL doesn't start with http://, let's add it:
30
- html.gsub!(/href="((?!http)\S+)/, 'href="http://\1')
31
30
  html
32
31
  end
33
32
 
@@ -48,12 +47,13 @@ module Rack
48
47
  common_gtlds = "com|net|org|edu|gov|info|mil|name|mobi|biz"
49
48
 
50
49
  new_text.gsub!(/\b
51
- (\S+\.(#{common_gtlds}|[a-z]{2})\S*) # match words that contain common
52
- # top-level domains or country codes
53
- #
54
- (\.|\?|!|:|,\))* # if the URL ends in punctuation,
55
- # assume the punction is grammatical
56
- # and is not part of the URL
50
+ (\S+\.(#{common_gtlds}|[a-z]{2}(?![a-z]))\S*) # match words that contain common
51
+ # top-level domains or country codes
52
+ #
53
+ (\.|\?|!|:|,|\))* # if the URL ends in punctuation,
54
+ # assume the punction is grammatical
55
+ # and is not part of the URL
56
+ #
57
57
  \b/x,
58
58
  # We mark the text with phrases like "beginninganchor1". That's because it's
59
59
  # much easier to replace these strings later with anchor tags rather than work within
@@ -62,7 +62,7 @@ module Rack
62
62
 
63
63
  # text that looks like @foo can become a twitter link
64
64
  if options[:twitter]
65
- new_text.gsub!(/(^|\s)(@(\w+))(\.|\?|!|:|,\))*\b/,
65
+ new_text.gsub!(/(^|\s|\()(@(\w+))(\.|\?|!|:|,|\))*\b/,
66
66
  '\1beginninganchor1http://twitter.com/\3beginninganchor2\2endinganchor')
67
67
  end
68
68
 
@@ -19,6 +19,17 @@ class App
19
19
  <body>
20
20
  <div id="container">
21
21
  <h1>Testing Rack::Linkify</h1>
22
+ <h2>How To Test</h2>
23
+ <p>
24
+ This gem comes with the expected automated unit tests that
25
+ you can run by simply typing 'rake test'.
26
+ </p>
27
+ <p>
28
+ This page serves as an integration test of sorts where you can
29
+ actually <em>see</em> the results of Rack::Linkify in your
30
+ browser.
31
+ </p>
32
+ <h2>Tests</h2>
22
33
  <p>
23
34
  This is a test of links in free-flowing text. <br />
24
35
  Test a typical URL http://www.google.com <br />
@@ -0,0 +1,19 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+ require 'redgreen'
4
+ require 'rack-linkify'
5
+ require 'plastic_test_helper'
6
+
7
+ module Test
8
+ module Unit
9
+ class TestCase
10
+
11
+ include PlasticTestHelper
12
+
13
+ def linkify_this_html(html)
14
+ process_html(html, Rack::Linkify, :twitter => true)
15
+ end
16
+
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,77 @@
1
+ require 'test/test_helper'
2
+
3
+ class TwitterLinksTest < Test::Unit::TestCase
4
+
5
+ def test_twitter_links
6
+ before_html = %Q{
7
+ <html>
8
+ <head><title>Testing Rack::Linkify</title></head>
9
+ <body>
10
+ <div id="container">
11
+ <p>
12
+ This test should linkify links like @foo and @bar if twitter is set to true.
13
+ </p>
14
+ The following should be linkified:
15
+ <ul>
16
+ <li>@foo</li>
17
+ <li>@bar</li>
18
+ </ul>
19
+ </div>
20
+ </body>
21
+ </html>
22
+ }
23
+ target_html = %Q{
24
+ <html>
25
+ <head><title>Testing Rack::Linkify</title></head>
26
+ <body>
27
+ <div id="container">
28
+ <p>
29
+ This test should linkify links like <a href="http://twitter.com/foo">@foo</a> and <a href="http://twitter.com/bar">@bar</a> if twitter is set to true.
30
+ </p>
31
+ The following should be linkified:
32
+ <ul>
33
+ <li><a href="http://twitter.com/foo">@foo</a></li>
34
+ <li><a href="http://twitter.com/bar">@bar</a></li>
35
+ </ul>
36
+ </div>
37
+ </body>
38
+ </html>
39
+ }
40
+ after_html = linkify_this_html(before_html)
41
+ assert_html_equal target_html, after_html
42
+ end
43
+
44
+
45
+ def test_twitter_links_with_punctuation
46
+ before_html = %Q{
47
+ <html>
48
+ <head><title>Testing Rack::Linkify</title></head>
49
+ <body>
50
+ <div id="container">
51
+ <p>
52
+ This test should linkify links like @foo, @bar!, @baz?, @qux:,
53
+ @quux?! and (@corge).
54
+ </p>
55
+ </div>
56
+ </body>
57
+ </html>
58
+ }
59
+ target_html = %Q{
60
+ <html>
61
+ <head><title>Testing Rack::Linkify</title></head>
62
+ <body>
63
+ <div id="container">
64
+ <p>
65
+ This test should linkify links like <a href="http://twitter.com/foo">@foo</a>, <a href="http://twitter.com/bar">@bar</a>!, <a href="http://twitter.com/baz">@baz</a>?, <a href="http://twitter.com/qux">@qux</a>:,
66
+ <a href="http://twitter.com/quux">@quux</a>?! and (<a href="http://twitter.com/corge">@corge</a>).
67
+ </p>
68
+ </div>
69
+ </body>
70
+ </html>
71
+ }
72
+ after_html = linkify_this_html(before_html)
73
+ assert_html_equal target_html, after_html
74
+ end
75
+
76
+
77
+ end
@@ -0,0 +1,720 @@
1
+ require 'test/test_helper'
2
+
3
+ class UrlsTest < Test::Unit::TestCase
4
+
5
+ def test_basic_document
6
+ before_html = %Q{
7
+ <!DOCTYPE html
8
+ PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
9
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
10
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
11
+ <head>
12
+ <title>Testing Rack::Linkify</title>
13
+ </head>
14
+ <body>
15
+ Hi, Mom!
16
+ </body>
17
+ </html>
18
+ }
19
+ after_html = linkify_this_html(before_html)
20
+ assert_html_equal before_html, after_html
21
+ end
22
+
23
+ def test_complex_document
24
+ before_html = %Q{
25
+ <html>
26
+ <head>
27
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
28
+ <script src="/javascripts/jquery-1.4.2.min.js?1278100057" type="text/javascript"></script>
29
+ <script src="/javascripts/rails.js?1278100057" type="text/javascript"></script>
30
+ <script src="/javascripts/application.js?1279244644" type="text/javascript"></script>
31
+ <link href="/stylesheets/reset.css?1278100057" media="screen" rel="stylesheet" type="text/css">
32
+ <link href="/stylesheets/application.css?1279312782" media="screen" rel="stylesheet" type="text/css">
33
+ <link href="/stylesheets/print.css?1279244644" media="print" rel="stylesheet" type="text/css">
34
+ <meta name="csrf-param" content="authenticity_token">
35
+ <meta name="csrf-token" content="f69Ldq/NF6OPL30KMTl9jeppLzu4TKYHEC2ZjWbbLro=">
36
+ <title>Geography :: Geographic Curiosities</title>
37
+ </head>
38
+ <body id="main">
39
+ <div id="container">
40
+ <header>
41
+ <h1>Geography Articles</h1>
42
+ <nav>
43
+ <ul>
44
+ <li><a href="articles.html">Index of all articles</a></li>
45
+ <li><a href="curiosities.html">Geographic curiosities</a></li>
46
+ <li><a href="book.html">Buy the book</a></li>
47
+ </ul>
48
+ </nav>
49
+ </header>
50
+ <div>
51
+ <article>
52
+ <header>
53
+ <h1>Geographic Curiosities</h1>
54
+ </header>
55
+ <section>
56
+ <h1>Comparitive Locations</h1>
57
+ <p class="explanation">
58
+ The relationships of the following cities may surprise you:
59
+ </p>
60
+ <ul>
61
+ <li>Reno, Nevada is farther west than Los Angeles, California.</li>
62
+ <li>Detroit, Michigan is farther east than Tallahassee, Florida.</li>
63
+ <li>Tijuana, Mexico is farther north than Hilton Head, South Carolina.</li>
64
+ </ul>
65
+ </section>
66
+ <section>
67
+ <h1>North American Countries</h1>
68
+ <p class="explanation">
69
+ Did you know about the following North American countries?
70
+ </p>
71
+ <ul>
72
+ <li>Dominica</li>
73
+ <li>Saint Kitts and Nevis</li>
74
+ <li>Saint Vincent and the Grenadines</li>
75
+ </ul>
76
+ </section>
77
+ <footer>
78
+ <p>Posted <time pubdate datetime="2010-11-04T15:04-08:50">Thursday</time>.</p>
79
+ </footer>
80
+ </article>
81
+ <h2>Sign Up!</h2>
82
+ <form action="/users/signup" class="signup_new_user" id="foo" method="post">
83
+ <div style="margin:0;padding:0;display:inline">
84
+ <input name="_method" type="hidden" value="put">
85
+ <input name="authenticity_token" type="hidden" value="f69Ldq/NF6OPL30KMTl9jeppLzu4TKYHEC2ZjWbbLro=">
86
+ </div>
87
+ <div id="title_field" class="field">
88
+ <label class="aligned_label" for="user[name]">Name</label>
89
+ <input id="user_name" name="user[name]" size="80" tabindex="1" type="text">
90
+ </div>
91
+ <div class="field">
92
+ <label class="aligned_label" for="subscription_id">Subscription</label>
93
+ <select id="subscription_id" name="subscription_id" tabindex="2">
94
+ <option value="1" selected>1 month</option>
95
+ <option value="2">6 months</option>
96
+ <option value="3">1 year</option>
97
+ </select>
98
+ </div>
99
+ <div class="clearfix">&nbsp;</div>
100
+ <div class="actions">
101
+ <input id="user_submit" name="commit" tabindex="3" type="submit" value="Sign Up">
102
+ </div>
103
+ </form>
104
+ </div>
105
+ <footer>
106
+ <p>Copyright &copy; 2010 </p>
107
+ <p>
108
+ <a href="about.html">About</a> -
109
+ <a href="policy.html">Privacy Policy</a> -
110
+ <a href="contact.html">Contact Us</a>
111
+ </p>
112
+ </footer>
113
+ </div>
114
+ </body>
115
+ </html>
116
+ }
117
+ after_html = linkify_this_html(before_html)
118
+ assert_html_equal before_html, after_html
119
+ end
120
+
121
+ def test_links_with_http_and_com_domain
122
+ before_html = %Q{
123
+ <html>
124
+ <head><title>Testing Rack::Linkify</title></head>
125
+ <body>
126
+ <div id="container">
127
+ <p>
128
+ This test should linkify links like http://www.google.com and
129
+ http://www.example.com
130
+ </p>
131
+ The following should be linkified:
132
+ <ul>
133
+ <li>http://www.google.com</li>
134
+ <li>http://www.example.com</li>
135
+ </ul>
136
+ </div>
137
+ </body>
138
+ </html>
139
+ }
140
+ target_html = %Q{
141
+ <html>
142
+ <head><title>Testing Rack::Linkify</title></head>
143
+ <body>
144
+ <div id="container">
145
+ <p>
146
+ This test should linkify links like <a href="http://www.google.com">http://www.google.com</a> and
147
+ <a href="http://www.example.com">http://www.example.com</a>
148
+ </p>
149
+ The following should be linkified:
150
+ <ul>
151
+ <li><a href="http://www.google.com">http://www.google.com</a></li>
152
+ <li><a href="http://www.example.com">http://www.example.com</a></li>
153
+ </ul>
154
+ </div>
155
+ </body>
156
+ </html>
157
+ }
158
+ after_html = linkify_this_html(before_html)
159
+ assert_html_equal target_html, after_html
160
+ end
161
+
162
+
163
+ def test_links_with_http_and_various_tlds
164
+ before_html = %Q{
165
+ <html>
166
+ <head><title>Testing Rack::Linkify</title></head>
167
+ <body>
168
+ <div id="container">
169
+ <p>
170
+ This test should linkify links like http://www.google.net and
171
+ http://www.example.org
172
+ </p>
173
+ The following should be linkified:
174
+ <ul>
175
+ <li>http://www.google.gov</li>
176
+ <li>http://www.example.de</li>
177
+ <li>http://www.example.me</li>
178
+ <li>http://www.example.us</li>
179
+ <li>http://www.example.edu</li>
180
+ <li>http://www.example.info</li>
181
+ </ul>
182
+ </div>
183
+ </body>
184
+ </html>
185
+ }
186
+ target_html = %Q{
187
+ <html>
188
+ <head><title>Testing Rack::Linkify</title></head>
189
+ <body>
190
+ <div id="container">
191
+ <p>
192
+ This test should linkify links like <a href="http://www.google.net">http://www.google.net</a> and
193
+ <a href="http://www.example.org">http://www.example.org</a>
194
+ </p>
195
+ The following should be linkified:
196
+ <ul>
197
+ <li><a href="http://www.google.gov">http://www.google.gov</a></li>
198
+ <li><a href="http://www.example.de">http://www.example.de</a></li>
199
+ <li><a href="http://www.example.me">http://www.example.me</a></li>
200
+ <li><a href="http://www.example.us">http://www.example.us</a></li>
201
+ <li><a href="http://www.example.edu">http://www.example.edu</a></li>
202
+ <li><a href="http://www.example.info">http://www.example.info</a></li>
203
+ </ul>
204
+ </div>
205
+ </body>
206
+ </html>
207
+ }
208
+ after_html = linkify_this_html(before_html)
209
+ assert_html_equal target_html, after_html
210
+ end
211
+
212
+
213
+ def test_links_with_http_and_paths
214
+ # Note: Linkify does not elegantly handle URLs ending with /. These URLs are
215
+ # still linkified, but the / ends up outside of the anchor tag.
216
+ before_html = %Q{
217
+ <html>
218
+ <head><title>Testing Rack::Linkify</title></head>
219
+ <body>
220
+ <div id="container">
221
+ <p>
222
+ This test should linkify links like http://www.google.com/foo/bar and
223
+ http://www.google.com/ and http://www.google.com/foo and http://www.google.com/foo/
224
+ </p>
225
+ The following should be linkified:
226
+ <ul>
227
+ <li>http://www.google.com/</li>
228
+ <li>http://www.google.com/foo</li>
229
+ </ul>
230
+ </div>
231
+ </body>
232
+ </html>
233
+ }
234
+ target_html = %Q{
235
+ <html>
236
+ <head><title>Testing Rack::Linkify</title></head>
237
+ <body>
238
+ <div id="container">
239
+ <p>
240
+ This test should linkify links like <a href="http://www.google.com/foo/bar">http://www.google.com/foo/bar</a> and
241
+ <a href="http://www.google.com">http://www.google.com</a>/ and <a href="http://www.google.com/foo">http://www.google.com/foo</a> and <a href="http://www.google.com/foo">http://www.google.com/foo</a>/
242
+ </p>
243
+ The following should be linkified:
244
+ <ul>
245
+ <li><a href="http://www.google.com">http://www.google.com</a>/</li>
246
+ <li><a href="http://www.google.com/foo">http://www.google.com/foo</a></li>
247
+ </ul>
248
+ </div>
249
+ </body>
250
+ </html>
251
+ }
252
+ after_html = linkify_this_html(before_html)
253
+ assert_html_equal target_html, after_html
254
+ end
255
+
256
+
257
+ def test_links_with_https
258
+ before_html = %Q{
259
+ <html>
260
+ <head><title>Testing Rack::Linkify</title></head>
261
+ <body>
262
+ <div id="container">
263
+ <p>
264
+ This test should linkify links like https://www.google.com and
265
+ https://www.example.com
266
+ </p>
267
+ The following should be linkified:
268
+ <ul>
269
+ <li>https://www.google.com</li>
270
+ <li>https://www.example.com</li>
271
+ </ul>
272
+ </div>
273
+ </body>
274
+ </html>
275
+ }
276
+ target_html = %Q{
277
+ <html>
278
+ <head><title>Testing Rack::Linkify</title></head>
279
+ <body>
280
+ <div id="container">
281
+ <p>
282
+ This test should linkify links like <a href="https://www.google.com">https://www.google.com</a> and
283
+ <a href="https://www.example.com">https://www.example.com</a>
284
+ </p>
285
+ The following should be linkified:
286
+ <ul>
287
+ <li><a href="https://www.google.com">https://www.google.com</a></li>
288
+ <li><a href="https://www.example.com">https://www.example.com</a></li>
289
+ </ul>
290
+ </div>
291
+ </body>
292
+ </html>
293
+ }
294
+ after_html = linkify_this_html(before_html)
295
+ assert_html_equal target_html, after_html
296
+ end
297
+
298
+
299
+ def test_links_without_http
300
+ before_html = %Q{
301
+ <html>
302
+ <head><title>Testing Rack::Linkify</title></head>
303
+ <body>
304
+ <div id="container">
305
+ <p>
306
+ This test should linkify links like www.google.com and
307
+ www.example.gov
308
+ </p>
309
+ The following should be linkified:
310
+ <ul>
311
+ <li>www.google.com</li>
312
+ <li>www.example.gov</li>
313
+ </ul>
314
+ </div>
315
+ </body>
316
+ </html>
317
+ }
318
+ target_html = %Q{
319
+ <html>
320
+ <head><title>Testing Rack::Linkify</title></head>
321
+ <body>
322
+ <div id="container">
323
+ <p>
324
+ This test should linkify links like <a href="http://www.google.com">www.google.com</a> and
325
+ <a href="http://www.example.gov">www.example.gov</a>
326
+ </p>
327
+ The following should be linkified:
328
+ <ul>
329
+ <li><a href="http://www.google.com">www.google.com</a></li>
330
+ <li><a href="http://www.example.gov">www.example.gov</a></li>
331
+ </ul>
332
+ </div>
333
+ </body>
334
+ </html>
335
+ }
336
+ after_html = linkify_this_html(before_html)
337
+ assert_html_equal target_html, after_html
338
+ end
339
+
340
+
341
+ def test_links_without_subdomains
342
+ before_html = %Q{
343
+ <html>
344
+ <head><title>Testing Rack::Linkify</title></head>
345
+ <body>
346
+ <div id="container">
347
+ <p>
348
+ This test should linkify links like google.com and
349
+ example.gov
350
+ </p>
351
+ The following should be linkified:
352
+ <ul>
353
+ <li>google.com</li>
354
+ <li>example.gov</li>
355
+ </ul>
356
+ </div>
357
+ </body>
358
+ </html>
359
+ }
360
+ target_html = %Q{
361
+ <html>
362
+ <head><title>Testing Rack::Linkify</title></head>
363
+ <body>
364
+ <div id="container">
365
+ <p>
366
+ This test should linkify links like <a href="http://google.com">google.com</a> and
367
+ <a href="http://example.gov">example.gov</a>
368
+ </p>
369
+ The following should be linkified:
370
+ <ul>
371
+ <li><a href="http://google.com">google.com</a></li>
372
+ <li><a href="http://example.gov">example.gov</a></li>
373
+ </ul>
374
+ </div>
375
+ </body>
376
+ </html>
377
+ }
378
+ after_html = linkify_this_html(before_html)
379
+ assert_html_equal target_html, after_html
380
+ end
381
+
382
+
383
+ def test_links_with_varied_subdomains
384
+ before_html = %Q{
385
+ <html>
386
+ <head><title>Testing Rack::Linkify</title></head>
387
+ <body>
388
+ <div id="container">
389
+ <p>
390
+ This test should linkify links like sub.google.com and
391
+ foo.bar.baz.example.gov
392
+ </p>
393
+ The following should be linkified:
394
+ <ul>
395
+ <li>sub.google.com</li>
396
+ <li>foo.bar.baz.example.gov</li>
397
+ </ul>
398
+ </div>
399
+ </body>
400
+ </html>
401
+ }
402
+ target_html = %Q{
403
+ <html>
404
+ <head><title>Testing Rack::Linkify</title></head>
405
+ <body>
406
+ <div id="container">
407
+ <p>
408
+ This test should linkify links like <a href="http://sub.google.com">sub.google.com</a> and
409
+ <a href="http://foo.bar.baz.example.gov">foo.bar.baz.example.gov</a>
410
+ </p>
411
+ The following should be linkified:
412
+ <ul>
413
+ <li><a href="http://sub.google.com">sub.google.com</a></li>
414
+ <li><a href="http://foo.bar.baz.example.gov">foo.bar.baz.example.gov</a></li>
415
+ </ul>
416
+ </div>
417
+ </body>
418
+ </html>
419
+ }
420
+ after_html = linkify_this_html(before_html)
421
+ assert_html_equal target_html, after_html
422
+ end
423
+
424
+
425
+ def test_links_followed_by_commas
426
+ before_html = %Q{
427
+ <html>
428
+ <head><title>Testing Rack::Linkify</title></head>
429
+ <body>
430
+ <div id="container">
431
+ <p>
432
+ This test should linkify links like sub.google.com,
433
+ http://www.google.com, http://example.gov/foo/bar, and
434
+ https://some.domain.example.com, even though they are
435
+ followed by punctuation.
436
+ </p>
437
+ </div>
438
+ </body>
439
+ </html>
440
+ }
441
+ target_html = %Q{
442
+ <html>
443
+ <head><title>Testing Rack::Linkify</title></head>
444
+ <body>
445
+ <div id="container">
446
+ <p>
447
+ This test should linkify links like <a href="http://sub.google.com">sub.google.com</a>,
448
+ <a href="http://www.google.com">http://www.google.com</a>, <a href="http://example.gov/foo/bar">http://example.gov/foo/bar</a>, and
449
+ <a href="https://some.domain.example.com">https://some.domain.example.com</a>, even though they are
450
+ followed by punctuation.
451
+ </p>
452
+ </div>
453
+ </body>
454
+ </html>
455
+ }
456
+ after_html = linkify_this_html(before_html)
457
+ assert_html_equal target_html, after_html
458
+ end
459
+
460
+
461
+ def test_links_followed_by_punctuation
462
+ # Note: if URLs are followed by a hyphen that is punctuation and not a part of
463
+ # the URL, Linkify will treat it as part of the URL. This is not the ideal
464
+ # behavior, but it is very hard to detect this scenario since hyphens are
465
+ # a valid part of URLs.
466
+ before_html = %Q{
467
+ <html>
468
+ <head><title>Testing Rack::Linkify</title></head>
469
+ <body>
470
+ <div id="container">
471
+ <p>
472
+ This test should linkify links like sub.google.com?
473
+ http://www.google.com. http://example.gov/foo/bar! and
474
+ https://some.domain.example.com: even though they are
475
+ followed by punctuation.
476
+ </p>
477
+ </div>
478
+ </body>
479
+ </html>
480
+ }
481
+ target_html = %Q{
482
+ <html>
483
+ <head><title>Testing Rack::Linkify</title></head>
484
+ <body>
485
+ <div id="container">
486
+ <p>
487
+ This test should linkify links like <a href="http://sub.google.com">sub.google.com</a>?
488
+ <a href="http://www.google.com">http://www.google.com</a>. <a href="http://example.gov/foo/bar">http://example.gov/foo/bar</a>! and
489
+ <a href="https://some.domain.example.com">https://some.domain.example.com</a>: even though they are
490
+ followed by punctuation.
491
+ </p>
492
+ </div>
493
+ </body>
494
+ </html>
495
+ }
496
+ after_html = linkify_this_html(before_html)
497
+ assert_html_equal target_html, after_html
498
+ end
499
+
500
+
501
+ def test_links_among_parentheses
502
+ before_html = %Q{
503
+ <html>
504
+ <head><title>Testing Rack::Linkify</title></head>
505
+ <body>
506
+ <div id="container">
507
+ <p>
508
+ This test should linkify links like
509
+ (http://www.google.com) and (http://example.gov/foo/bar)
510
+ even though they are surrounded by parentheses.
511
+ </p>
512
+ </div>
513
+ </body>
514
+ </html>
515
+ }
516
+ target_html = %Q{
517
+ <html>
518
+ <head><title>Testing Rack::Linkify</title></head>
519
+ <body>
520
+ <div id="container">
521
+ <p>
522
+ This test should linkify links like
523
+ (<a href="http://www.google.com">http://www.google.com</a>) and (<a href="http://example.gov/foo/bar">http://example.gov/foo/bar</a>)
524
+ even though they are surrounded by parentheses.
525
+ </p>
526
+ </div>
527
+ </body>
528
+ </html>
529
+ }
530
+ after_html = linkify_this_html(before_html)
531
+ assert_html_equal target_html, after_html
532
+ end
533
+
534
+
535
+ def test_variety_of_links
536
+ before_html = %Q{
537
+ <html>
538
+ <head><title>Testing Rack::Linkify</title></head>
539
+ <body>
540
+ <div id="container">
541
+ <p id="explanation">
542
+ The following is a collection of typical links you would come across.
543
+ </p>
544
+ <p>
545
+ The following links should be linkified:
546
+ http://www.w3schools.com/tags/att_a_href.asp as well as
547
+ http://www.google.com/#hl=en&source=hp&q=what+is+a+spork&aq=f&aqi=l1g1&aql=&oq=&gs_rfai=Cmg1v-jxDTK_FOoGOzQT2jtnPCgAAAKoEBU_QannZ&fp=6f146f4f6152193c
548
+ and http://stackoverflow.com/questions/tagged/linux
549
+ and finally http://www.cnn.com/2010/POLITICS/07/18/tea.party.imbroglio/index.html?hpt=T2.
550
+ </p>
551
+ </div>
552
+ </body>
553
+ </html>
554
+ }
555
+ target_html = %Q{
556
+ <html>
557
+ <head><title>Testing Rack::Linkify</title></head>
558
+ <body>
559
+ <div id="container">
560
+ <p id="explanation">
561
+ The following is a collection of typical links you would come across.
562
+ </p>
563
+ <p>
564
+ The following links should be linkified:
565
+ <a href="http://www.w3schools.com/tags/att_a_href.asp">http://www.w3schools.com/tags/att_a_href.asp</a> as well as
566
+ <a href="http://www.google.com/#hl=en&source=hp&q=what+is+a+spork&aq=f&aqi=l1g1&aql=&oq=&gs_rfai=Cmg1v-jxDTK_FOoGOzQT2jtnPCgAAAKoEBU_QannZ&fp=6f146f4f6152193c">http://www.google.com/#hl=en&source=hp&q=what+is+a+spork&aq=f&aqi=l1g1&aql=&oq=&gs_rfai=Cmg1v-jxDTK_FOoGOzQT2jtnPCgAAAKoEBU_QannZ&fp=6f146f4f6152193c</a>
567
+ and <a href="http://stackoverflow.com/questions/tagged/linux">http://stackoverflow.com/questions/tagged/linux</a>
568
+ and finally <a href="http://www.cnn.com/2010/POLITICS/07/18/tea.party.imbroglio/index.html?hpt=T2">http://www.cnn.com/2010/POLITICS/07/18/tea.party.imbroglio/index.html?hpt=T2</a>.
569
+ </p>
570
+ </div>
571
+ </body>
572
+ </html>
573
+ }
574
+ after_html = linkify_this_html(before_html)
575
+ assert_html_equal target_html, after_html
576
+ end
577
+
578
+
579
+ def test_more_variety_of_links
580
+ before_html = %Q{
581
+ <html>
582
+ <head><title>Testing Rack::Linkify</title></head>
583
+ <body>
584
+ <div id="container">
585
+ <p id="explanation">
586
+ The following is a collection of even more links you would come across.
587
+ </p>
588
+ <p>
589
+ The following links should be linkified:
590
+ http://www.youtube.com/watch?v=_OBlgSz8sSM and
591
+ http://maps.google.com/maps?f=d&source=s_d&saddr=Milwaukee,+WI&daddr=Albuquerque,+NM&hl=en&geocode=Fba4kAIdVqfC-innR4tX1wIFiDGEe0G1IhlfRA%3BFctYFwId_6Gk-Sl7gwnT3QoihzH99tm4zvjTwA&mra=ls&sll=37.0625,-95.677068&sspn=40.460237,79.013672&ie=UTF8&z=6 and then
592
+ https://personal.paypal.com/cgi-bin/marketingweb?cmd=_render-content&content_ID=marketing_us/pay_online&nav=0.1 and then
593
+ and http://rtl.lu/home, http://news.rtl.lu/news/international/74127.html#comments, and http://ipaper.rtl.lu/display/DisplayShopping.188_48.20-48.
594
+ </p>
595
+ </div>
596
+ </body>
597
+ </html>
598
+ }
599
+ target_html = %Q{
600
+ <html>
601
+ <head><title>Testing Rack::Linkify</title></head>
602
+ <body>
603
+ <div id="container">
604
+ <p id="explanation">
605
+ The following is a collection of even more links you would come across.
606
+ </p>
607
+ <p>
608
+ The following links should be linkified:
609
+ <a href="http://www.youtube.com/watch?v=_OBlgSz8sSM">http://www.youtube.com/watch?v=_OBlgSz8sSM</a> and
610
+ <a href="http://maps.google.com/maps?f=d&source=s_d&saddr=Milwaukee,+WI&daddr=Albuquerque,+NM&hl=en&geocode=Fba4kAIdVqfC-innR4tX1wIFiDGEe0G1IhlfRA%3BFctYFwId_6Gk-Sl7gwnT3QoihzH99tm4zvjTwA&mra=ls&sll=37.0625,-95.677068&sspn=40.460237,79.013672&ie=UTF8&z=6">http://maps.google.com/maps?f=d&source=s_d&saddr=Milwaukee,+WI&daddr=Albuquerque,+NM&hl=en&geocode=Fba4kAIdVqfC-innR4tX1wIFiDGEe0G1IhlfRA%3BFctYFwId_6Gk-Sl7gwnT3QoihzH99tm4zvjTwA&mra=ls&sll=37.0625,-95.677068&sspn=40.460237,79.013672&ie=UTF8&z=6</a> and then
611
+ <a href="https://personal.paypal.com/cgi-bin/marketingweb?cmd=_render-content&content_ID=marketing_us/pay_online&nav=0.1">https://personal.paypal.com/cgi-bin/marketingweb?cmd=_render-content&content_ID=marketing_us/pay_online&nav=0.1</a> and then
612
+ and <a href="http://rtl.lu/home">http://rtl.lu/home</a>, <a href="http://news.rtl.lu/news/international/74127.html#comments">http://news.rtl.lu/news/international/74127.html#comments</a>, and <a href="http://ipaper.rtl.lu/display/DisplayShopping.188_48.20-48">http://ipaper.rtl.lu/display/DisplayShopping.188_48.20-48</a>.
613
+ </p>
614
+ </div>
615
+ </body>
616
+ </html>
617
+ }
618
+ after_html = linkify_this_html(before_html)
619
+ assert_html_equal target_html, after_html
620
+ end
621
+
622
+
623
+ def test_false_positives
624
+ before_html = %Q{
625
+ <html>
626
+ <head><title>Testing Rack::Linkify</title></head>
627
+ <body>
628
+ <div id="container">
629
+ <p>
630
+ Nothing in this paragraph should be linkified, including index.html or
631
+ /foo/bar or /foo/bar.html or net gov com.
632
+ </p>
633
+ </div>
634
+ </body>
635
+ </html>
636
+ }
637
+ after_html = linkify_this_html(before_html)
638
+ assert_html_equal before_html, after_html
639
+ end
640
+
641
+
642
+ def test_comments_not_linkified
643
+ before_html = %Q{
644
+ <html>
645
+ <head><title>Testing Rack::Linkify</title></head>
646
+ <body>
647
+ <div id="container">
648
+ <!-- Nothing in HTML comments should be linkified, including foo.html or
649
+ google.com or http://www.google.com or example.com/foo. -->
650
+ <p>
651
+ Alaska has the population of a small city. Just sayin'.
652
+ </p>
653
+ </div>
654
+ </body>
655
+ </html>
656
+ }
657
+ after_html = linkify_this_html(before_html)
658
+ assert_html_equal before_html, after_html
659
+ end
660
+
661
+
662
+ def test_existing_hrefs_untouched
663
+ before_html = %Q{
664
+ <html>
665
+ <head><title>Testing Rack::Linkify</title></head>
666
+ <body>
667
+ <div id="container">
668
+ <p>
669
+ Existing hrefs should remain untouched, such as
670
+ <a href="#">this one</a> and
671
+ <a href="/foo">this one</a> and
672
+ <a href="bar.html">this one</a> and
673
+ <a href="http://google.com">this one</a> and
674
+ <a href="http://www.example.com/foo/bar/baz">this one</a> and
675
+ <a href="http://www.example.de/foo/bar/index.html">this one</a>.
676
+ </p>
677
+ </div>
678
+ </body>
679
+ </html>
680
+ }
681
+ after_html = linkify_this_html(before_html)
682
+ assert_html_equal before_html, after_html
683
+ end
684
+
685
+
686
+ def test_form_elements_not_linkified
687
+ before_html = %Q{
688
+ <html>
689
+ <head><title>Testing Rack::Linkify</title></head>
690
+ <body>
691
+ <div id="container">
692
+ <p>
693
+ The contents of form elements should not be linkified.
694
+ <form action="/users/signup" class="signup_new_user" id="foo" method="post">
695
+ <input id="user_name" name="user[name]" size="80" tabindex="1" type="text"
696
+ value="These should not be linkified: http://www.google.com and example.com.">
697
+ <select id="subscription_id" name="subscription_id" tabindex="2">
698
+ <option value="1" selected>These should not be linkified: http://www.google.com and example.com.</option>
699
+ <option value="2">as well as www.example.com/foo</option>
700
+ <option value="3">and finally http://www.google.com/foo.</option>
701
+ </select>
702
+ <textarea cols="20" id="commitment_note" name="commitment[note]" rows="10" tabindex="18">
703
+ The URL http://www.example.com should not be linkified.
704
+ </textarea>
705
+ <div class="actions">
706
+ <input id="user_submit" name="commit" tabindex="3" type="submit"
707
+ value="These should not be linkified: http://www.google.com and example.com.">
708
+ </div>
709
+ </form>
710
+ </p>
711
+ </div>
712
+ </body>
713
+ </html>
714
+ }
715
+ after_html = linkify_this_html(before_html)
716
+ assert_html_equal before_html, after_html
717
+ end
718
+
719
+
720
+ end
metadata CHANGED
@@ -1,7 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-linkify
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 2
10
+ version: 0.0.2
5
11
  platform: ruby
6
12
  authors:
7
13
  - Wyatt Greene
@@ -9,19 +15,39 @@ autorequire:
9
15
  bindir: bin
10
16
  cert_chain: []
11
17
 
12
- date: 2009-12-03 00:00:00 -05:00
18
+ date: 2010-07-18 00:00:00 -04:00
13
19
  default_executable:
14
20
  dependencies:
15
21
  - !ruby/object:Gem::Dependency
16
22
  name: rack-plastic
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 25
30
+ segments:
31
+ - 0
32
+ - 1
33
+ - 1
34
+ version: 0.1.1
17
35
  type: :runtime
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: redgreen
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
20
42
  requirements:
21
43
  - - ">="
22
44
  - !ruby/object:Gem::Version
23
- version: 0.0.3
24
- version:
45
+ hash: 3
46
+ segments:
47
+ - 0
48
+ version: "0"
49
+ type: :development
50
+ version_requirements: *id002
25
51
  description: "\n Any URLs that occur in the text of the web page are automatically surrounded\n by an anchor tag.\n "
26
52
  email: techiferous@gmail.com
27
53
  executables: []
@@ -30,15 +56,18 @@ extensions: []
30
56
 
31
57
  extra_rdoc_files:
32
58
  - LICENSE
33
- - README
59
+ - README.rdoc
34
60
  files:
35
61
  - CHANGELOG
36
62
  - LICENSE
37
- - README
38
63
  - Rakefile
39
64
  - lib/rack-linkify.rb
40
65
  - test/rackapp/app.rb
41
66
  - test/rackapp/config.ru
67
+ - test/test_helper.rb
68
+ - test/twitter_links_test.rb
69
+ - test/urls_test.rb
70
+ - README.rdoc
42
71
  has_rdoc: true
43
72
  homepage: http://github.com/techiferous/rack-linkify
44
73
  licenses: []
@@ -49,23 +78,32 @@ rdoc_options:
49
78
  require_paths:
50
79
  - lib
51
80
  required_ruby_version: !ruby/object:Gem::Requirement
81
+ none: false
52
82
  requirements:
53
83
  - - ">="
54
84
  - !ruby/object:Gem::Version
85
+ hash: 3
86
+ segments:
87
+ - 0
55
88
  version: "0"
56
- version:
57
89
  required_rubygems_version: !ruby/object:Gem::Requirement
90
+ none: false
58
91
  requirements:
59
92
  - - ">="
60
93
  - !ruby/object:Gem::Version
94
+ hash: 3
95
+ segments:
96
+ - 0
61
97
  version: "0"
62
- version:
63
98
  requirements:
64
99
  - none
65
100
  rubyforge_project:
66
- rubygems_version: 1.3.5
101
+ rubygems_version: 1.3.7
67
102
  signing_key:
68
103
  specification_version: 3
69
104
  summary: Rack middleware that adds anchor tags to URLs in text.
70
105
  test_files:
71
106
  - test/rackapp/app.rb
107
+ - test/test_helper.rb
108
+ - test/twitter_links_test.rb
109
+ - test/urls_test.rb