rails_autolink 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,8 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'autotest/restart'
4
+
5
+ Autotest.add_hook :initialize do |at|
6
+ at.testlib = 'minitest/autorun'
7
+ at.find_directories = ARGV unless ARGV.empty?
8
+ end
File without changes
@@ -0,0 +1,6 @@
1
+ === 1.0.0 / 2011-05-02
2
+
3
+ * 1 major enhancement
4
+
5
+ * Birthday!
6
+
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gem 'rails', :path => '/Users/aaron/git/rails'
4
+ gem 'arel', :path => '/Users/aaron/git/arel'
5
+ gem 'rack', :path => '/Users/aaron/git/rack'
6
+ gem 'hoe'
7
+ gem 'minitest'
8
+
@@ -0,0 +1,8 @@
1
+ .autotest
2
+ CHANGELOG.rdoc
3
+ Gemfile
4
+ Manifest.txt
5
+ README.rdoc
6
+ Rakefile
7
+ lib/rails_autolink.rb
8
+ test/test_rails_autolink.rb
@@ -0,0 +1,67 @@
1
+ = rails_autolink
2
+
3
+ * http://github.com/tenderlove/rails_autolink
4
+
5
+ == DESCRIPTION:
6
+
7
+ This is an extraction of the `auto_link` method from rails. The `auto_link`
8
+ method was removed from Rails in version Rails 3.1. This gem is meant to
9
+ bridge the gap for people migrating.
10
+
11
+ == FEATURES/PROBLEMS:
12
+
13
+ * May not be safe!
14
+
15
+ == SYNOPSIS:
16
+
17
+ require 'rails_autolink'
18
+
19
+ auto_link("Go to http://www.rubyonrails.org and say hello to david@loudthinking.com")
20
+ # => "Go to <a href=\"http://www.rubyonrails.org\">http://www.rubyonrails.org</a> and
21
+ # say hello to <a href=\"mailto:david@loudthinking.com\">david@loudthinking.com</a>"
22
+
23
+ auto_link("Visit http://www.loudthinking.com/ or e-mail david@loudthinking.com", :link => :urls)
24
+ # => "Visit <a href=\"http://www.loudthinking.com/\">http://www.loudthinking.com/</a>
25
+ # or e-mail david@loudthinking.com"
26
+
27
+ auto_link("Visit http://www.loudthinking.com/ or e-mail david@loudthinking.com", :link => :email_addresses)
28
+ # => "Visit http://www.loudthinking.com/ or e-mail <a href=\"mailto:david@loudthinking.com\">david@loudthinking.com</a>"
29
+
30
+ post_body = "Welcome to my new blog at http://www.myblog.com/. Please e-mail me at me@email.com."
31
+ auto_link(post_body, :html => { :target => '_blank' }) do |text|
32
+ truncate(text, :length => 15)
33
+ end
34
+ # => "Welcome to my new blog at <a href=\"http://www.myblog.com/\" target=\"_blank\">http://www.m...</a>.
35
+
36
+ == REQUIREMENTS:
37
+
38
+ * rails
39
+
40
+ == INSTALL:
41
+
42
+ * gem install rails
43
+
44
+ == LICENSE:
45
+
46
+ (The MIT License)
47
+
48
+ Copyright (c) 2011 DHH
49
+
50
+ Permission is hereby granted, free of charge, to any person obtaining
51
+ a copy of this software and associated documentation files (the
52
+ 'Software'), to deal in the Software without restriction, including
53
+ without limitation the rights to use, copy, modify, merge, publish,
54
+ distribute, sublicense, and/or sell copies of the Software, and to
55
+ permit persons to whom the Software is furnished to do so, subject to
56
+ the following conditions:
57
+
58
+ The above copyright notice and this permission notice shall be
59
+ included in all copies or substantial portions of the Software.
60
+
61
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
62
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
63
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
64
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
65
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
66
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
67
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,19 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'rubygems'
4
+ require 'hoe'
5
+
6
+ Hoe.plugins.delete :rubyforge
7
+ Hoe.plugin :minitest
8
+ Hoe.plugin :gemspec # `gem install hoe-gemspec`
9
+ Hoe.plugin :git # `gem install hoe-git`
10
+
11
+ Hoe.spec 'rails_autolink' do
12
+ developer('Aaron Patterson', 'aaron@tenderlovemaking.com')
13
+ self.readme_file = 'README.rdoc'
14
+ self.history_file = 'CHANGELOG.rdoc'
15
+ self.extra_rdoc_files = FileList['*.rdoc']
16
+ self.extra_deps << ['rails', '~> 3.1.0']
17
+ end
18
+
19
+ # vim: syntax=ruby
@@ -0,0 +1,141 @@
1
+ require 'active_support/core_ext/object/blank'
2
+ require 'active_support/core_ext/array/extract_options'
3
+ require 'active_support/core_ext/hash/reverse_merge'
4
+ require 'active_support/core_ext/hash/keys'
5
+
6
+ module RailsAutolink
7
+ VERSION = '1.0.0'
8
+ end
9
+
10
+ module ActionView
11
+ module Helpers # :nodoc:
12
+ module TextHelper
13
+ # Turns all URLs and e-mail addresses into clickable links. The <tt>:link</tt> option
14
+ # will limit what should be linked. You can add HTML attributes to the links using
15
+ # <tt>:html</tt>. Possible values for <tt>:link</tt> are <tt>:all</tt> (default),
16
+ # <tt>:email_addresses</tt>, and <tt>:urls</tt>. If a block is given, each URL and
17
+ # e-mail address is yielded and the result is used as the link text.
18
+ #
19
+ # ==== Examples
20
+ # auto_link("Go to http://www.rubyonrails.org and say hello to david@loudthinking.com")
21
+ # # => "Go to <a href=\"http://www.rubyonrails.org\">http://www.rubyonrails.org</a> and
22
+ # # say hello to <a href=\"mailto:david@loudthinking.com\">david@loudthinking.com</a>"
23
+ #
24
+ # auto_link("Visit http://www.loudthinking.com/ or e-mail david@loudthinking.com", :link => :urls)
25
+ # # => "Visit <a href=\"http://www.loudthinking.com/\">http://www.loudthinking.com/</a>
26
+ # # or e-mail david@loudthinking.com"
27
+ #
28
+ # auto_link("Visit http://www.loudthinking.com/ or e-mail david@loudthinking.com", :link => :email_addresses)
29
+ # # => "Visit http://www.loudthinking.com/ or e-mail <a href=\"mailto:david@loudthinking.com\">david@loudthinking.com</a>"
30
+ #
31
+ # post_body = "Welcome to my new blog at http://www.myblog.com/. Please e-mail me at me@email.com."
32
+ # auto_link(post_body, :html => { :target => '_blank' }) do |text|
33
+ # truncate(text, :length => 15)
34
+ # end
35
+ # # => "Welcome to my new blog at <a href=\"http://www.myblog.com/\" target=\"_blank\">http://www.m...</a>.
36
+ # Please e-mail me at <a href=\"mailto:me@email.com\">me@email.com</a>."
37
+ #
38
+ #
39
+ # You can still use <tt>auto_link</tt> with the old API that accepts the
40
+ # +link+ as its optional second parameter and the +html_options+ hash
41
+ # as its optional third parameter:
42
+ # post_body = "Welcome to my new blog at http://www.myblog.com/. Please e-mail me at me@email.com."
43
+ # auto_link(post_body, :urls)
44
+ # # => "Welcome to my new blog at <a href=\"http://www.myblog.com/\">http://www.myblog.com</a>.
45
+ # Please e-mail me at me@email.com."
46
+ #
47
+ # auto_link(post_body, :all, :target => "_blank")
48
+ # # => "Welcome to my new blog at <a href=\"http://www.myblog.com/\" target=\"_blank\">http://www.myblog.com</a>.
49
+ # Please e-mail me at <a href=\"mailto:me@email.com\">me@email.com</a>."
50
+ def auto_link(text, *args, &block)#link = :all, html = {}, &block)
51
+ return '' if text.blank?
52
+
53
+ options = args.size == 2 ? {} : args.extract_options! # this is necessary because the old auto_link API has a Hash as its last parameter
54
+ unless args.empty?
55
+ options[:link] = args[0] || :all
56
+ options[:html] = args[1] || {}
57
+ end
58
+ options.reverse_merge!(:link => :all, :html => {})
59
+
60
+ case options[:link].to_sym
61
+ when :all then auto_link_email_addresses(auto_link_urls(text, options[:html], options, &block), options[:html], &block)
62
+ when :email_addresses then auto_link_email_addresses(text, options[:html], &block)
63
+ when :urls then auto_link_urls(text, options[:html], options, &block)
64
+ end
65
+ end
66
+
67
+ private
68
+
69
+ AUTO_LINK_RE = %r{
70
+ (?: ([0-9A-Za-z+.:-]+:)// | www\. )
71
+ [^\s<]+
72
+ }x
73
+
74
+ # regexps for determining context, used high-volume
75
+ AUTO_LINK_CRE = [/<[^>]+$/, /^[^>]*>/, /<a\b.*?>/i, /<\/a>/i]
76
+
77
+ AUTO_EMAIL_RE = /[\w.!#\$%+-]+@[\w-]+(?:\.[\w-]+)+/
78
+
79
+ BRACKETS = { ']' => '[', ')' => '(', '}' => '{' }
80
+
81
+ # Turns all urls into clickable links. If a block is given, each url
82
+ # is yielded and the result is used as the link text.
83
+ def auto_link_urls(text, html_options = {}, options = {})
84
+ link_attributes = html_options.stringify_keys
85
+ text.gsub(AUTO_LINK_RE) do
86
+ scheme, href = $1, $&
87
+ punctuation = []
88
+
89
+ if auto_linked?($`, $')
90
+ # do not change string; URL is already linked
91
+ href
92
+ else
93
+ # don't include trailing punctuation character as part of the URL
94
+ while href.sub!(/[^\w\/-]$/, '')
95
+ punctuation.push $&
96
+ if opening = BRACKETS[punctuation.last] and href.scan(opening).size > href.scan(punctuation.last).size
97
+ href << punctuation.pop
98
+ break
99
+ end
100
+ end
101
+
102
+ link_text = block_given?? yield(href) : href
103
+ href = 'http://' + href unless scheme
104
+
105
+ unless options[:sanitize] == false
106
+ link_text = sanitize(link_text)
107
+ href = sanitize(href)
108
+ end
109
+ content_tag(:a, link_text, link_attributes.merge('href' => href), !!options[:sanitize]) + punctuation.reverse.join('')
110
+ end
111
+ end
112
+ end
113
+
114
+ # Turns all email addresses into clickable links. If a block is given,
115
+ # each email is yielded and the result is used as the link text.
116
+ def auto_link_email_addresses(text, html_options = {}, options = {})
117
+ text.gsub(AUTO_EMAIL_RE) do
118
+ text = $&
119
+
120
+ if auto_linked?($`, $')
121
+ text.html_safe
122
+ else
123
+ display_text = (block_given?) ? yield(text) : text
124
+
125
+ unless options[:sanitize] == false
126
+ text = sanitize(text)
127
+ display_text = sanitize(display_text) unless text == display_text
128
+ end
129
+ mail_to text, display_text, html_options
130
+ end
131
+ end
132
+ end
133
+
134
+ # Detects already linked context or position in the middle of a tag
135
+ def auto_linked?(left, right)
136
+ (left =~ AUTO_LINK_CRE[0] and right =~ AUTO_LINK_CRE[1]) or
137
+ (left.rindex(AUTO_LINK_CRE[2]) and $' !~ AUTO_LINK_CRE[3])
138
+ end
139
+ end
140
+ end
141
+ end
@@ -0,0 +1,304 @@
1
+ # encoding: utf-8
2
+
3
+ require "minitest/autorun"
4
+ require "rails_autolink"
5
+ require 'erb'
6
+ require 'cgi'
7
+ require 'active_support/core_ext/class/attribute_accessors'
8
+ require 'active_support'
9
+ require 'action_pack'
10
+ require 'action_view/helpers/capture_helper'
11
+ require 'action_view/helpers/sanitize_helper'
12
+ require 'action_view/helpers/url_helper'
13
+ require 'action_view/helpers/tag_helper'
14
+ require 'active_support/core_ext/module/attribute_accessors'
15
+ require 'active_support/core_ext/string/encoding'
16
+ require 'action_dispatch/testing/assertions/dom'
17
+ require 'action_view/helpers/text_helper'
18
+ require 'action_view/helpers/output_safety_helper'
19
+
20
+ class TestRailsAutolink < MiniTest::Unit::TestCase
21
+ include ActionView::Helpers::CaptureHelper
22
+ include ActionView::Helpers::TextHelper
23
+ include ActionView::Helpers::SanitizeHelper
24
+ include ActionView::Helpers::TagHelper
25
+ include ActionView::Helpers::UrlHelper
26
+ include ActionView::Helpers::OutputSafetyHelper
27
+ include ActionDispatch::Assertions::DomAssertions
28
+
29
+ def test_auto_link_within_tags
30
+ link_raw = 'http://www.rubyonrails.org/images/rails.png'
31
+ link_result = %Q(<img src="#{link_raw}" />)
32
+ assert_equal link_result, auto_link(link_result)
33
+ end
34
+
35
+ def test_auto_link_with_brackets
36
+ link1_raw = 'http://en.wikipedia.org/wiki/Sprite_(computer_graphics)'
37
+ link1_result = generate_result(link1_raw)
38
+ assert_equal link1_result, auto_link(link1_raw)
39
+ assert_equal "(link: #{link1_result})", auto_link("(link: #{link1_raw})")
40
+
41
+ link2_raw = 'http://en.wikipedia.org/wiki/Sprite_[computer_graphics]'
42
+ link2_result = generate_result(link2_raw)
43
+ assert_equal link2_result, auto_link(link2_raw)
44
+ assert_equal "[link: #{link2_result}]", auto_link("[link: #{link2_raw}]")
45
+
46
+ link3_raw = 'http://en.wikipedia.org/wiki/Sprite_{computer_graphics}'
47
+ link3_result = generate_result(link3_raw)
48
+ assert_equal link3_result, auto_link(link3_raw)
49
+ assert_equal "{link: #{link3_result}}", auto_link("{link: #{link3_raw}}")
50
+ end
51
+
52
+ def test_auto_link_with_options_hash
53
+ assert_dom_equal 'Welcome to my new blog at <a href="http://www.myblog.com/" class="menu" target="_blank">http://www.myblog.com/</a>. Please e-mail me at <a href="mailto:me@email.com" class="menu" target="_blank">me@email.com</a>.',
54
+ auto_link("Welcome to my new blog at http://www.myblog.com/. Please e-mail me at me@email.com.",
55
+ :link => :all, :html => { :class => "menu", :target => "_blank" })
56
+ end
57
+
58
+ def test_auto_link_with_multiple_trailing_punctuations
59
+ url = "http://youtube.com"
60
+ url_result = generate_result(url)
61
+ assert_equal url_result, auto_link(url)
62
+ assert_equal "(link: #{url_result}).", auto_link("(link: #{url}).")
63
+ end
64
+
65
+ def test_auto_link_with_block
66
+ url = "http://api.rubyonrails.com/Foo.html"
67
+ email = "fantabulous@shiznadel.ic"
68
+
69
+ assert_equal %(<p><a href="#{url}">#{url[0...7]}...</a><br /><a href="mailto:#{email}">#{email[0...7]}...</a><br /></p>), auto_link("<p>#{url}<br />#{email}<br /></p>") { |_url| truncate(_url, :length => 10) }
70
+ end
71
+
72
+ def test_auto_link_with_block_with_html
73
+ pic = "http://example.com/pic.png"
74
+ url = "http://example.com/album?a&b=c"
75
+
76
+ assert_equal %(My pic: <a href="#{pic}"><img src="#{pic}" width="160px"></a> -- full album here #{generate_result(url)}), auto_link("My pic: #{pic} -- full album here #{url}") { |link|
77
+ if link =~ /\.(jpg|gif|png|bmp|tif)$/i
78
+ raw %(<img src="#{link}" width="160px">)
79
+ else
80
+ link
81
+ end
82
+ }
83
+ end
84
+
85
+ def test_auto_link_should_sanitize_input_when_sanitize_option_is_not_false
86
+ link_raw = %{http://www.rubyonrails.com?id=1&num=2}
87
+ assert_equal %{<a href="http://www.rubyonrails.com?id=1&num=2">http://www.rubyonrails.com?id=1&num=2</a>}, auto_link(link_raw)
88
+ end
89
+
90
+ def test_auto_link_should_not_sanitize_input_when_sanitize_option_is_false
91
+ link_raw = %{http://www.rubyonrails.com?id=1&num=2}
92
+ assert_equal %{<a href="http://www.rubyonrails.com?id=1&num=2">http://www.rubyonrails.com?id=1&num=2</a>}, auto_link(link_raw, :sanitize => false)
93
+ end
94
+
95
+ def test_auto_link_other_protocols
96
+ ftp_raw = 'ftp://example.com/file.txt'
97
+ assert_equal %(Download #{generate_result(ftp_raw)}), auto_link("Download #{ftp_raw}")
98
+
99
+ file_scheme = 'file:///home/username/RomeoAndJuliet.pdf'
100
+ z39_scheme = 'z39.50r://host:696/db'
101
+ chrome_scheme = 'chrome://package/section/path'
102
+ view_source = 'view-source:http://en.wikipedia.org/wiki/URI_scheme'
103
+ assert_equal generate_result(file_scheme), auto_link(file_scheme)
104
+ assert_equal generate_result(z39_scheme), auto_link(z39_scheme)
105
+ assert_equal generate_result(chrome_scheme), auto_link(chrome_scheme)
106
+ assert_equal generate_result(view_source), auto_link(view_source)
107
+ end
108
+
109
+ def test_auto_link_already_linked
110
+ linked1 = generate_result('Ruby On Rails', 'http://www.rubyonrails.com')
111
+ linked2 = %('<a href="http://www.example.com">www.example.com</a>')
112
+ linked3 = %('<a href="http://www.example.com" rel="nofollow">www.example.com</a>')
113
+ linked4 = %('<a href="http://www.example.com"><b>www.example.com</b></a>')
114
+ linked5 = %('<a href="#close">close</a> <a href="http://www.example.com"><b>www.example.com</b></a>')
115
+ assert_equal linked1, auto_link(linked1)
116
+ assert_equal linked2, auto_link(linked2)
117
+ assert_equal linked3, auto_link(linked3)
118
+ assert_equal linked4, auto_link(linked4)
119
+ assert_equal linked5, auto_link(linked5)
120
+
121
+ linked_email = %Q(<a href="mailto:david@loudthinking.com">Mail me</a>)
122
+ assert_equal linked_email, auto_link(linked_email)
123
+ end
124
+
125
+
126
+ def test_auto_link_at_eol
127
+ url1 = "http://api.rubyonrails.com/Foo.html"
128
+ url2 = "http://www.ruby-doc.org/core/Bar.html"
129
+
130
+ assert_equal %(<p><a href="#{url1}">#{url1}</a><br /><a href="#{url2}">#{url2}</a><br /></p>), auto_link("<p>#{url1}<br />#{url2}<br /></p>")
131
+ end
132
+
133
+ def test_auto_link_should_not_be_html_safe
134
+ email_raw = 'santiago@wyeworks.com'
135
+ link_raw = 'http://www.rubyonrails.org'
136
+
137
+ assert !auto_link(nil).html_safe?, 'should not be html safe'
138
+ assert !auto_link('').html_safe?, 'should not be html safe'
139
+ assert !auto_link("#{link_raw} #{link_raw} #{link_raw}").html_safe?, 'should not be html safe'
140
+ assert !auto_link("hello #{email_raw}").html_safe?, 'should not be html safe'
141
+ end
142
+
143
+ def test_auto_link_email_address
144
+ email_raw = 'aaron@tenderlovemaking.com'
145
+ email_result = %{<a href="mailto:#{email_raw}">#{email_raw}</a>}
146
+ assert !auto_link_email_addresses(email_result).html_safe?, 'should not be html safe'
147
+ end
148
+
149
+ def test_auto_link
150
+ email_raw = 'david@loudthinking.com'
151
+ email_result = %{<a href="mailto:#{email_raw}">#{email_raw}</a>}
152
+ link_raw = 'http://www.rubyonrails.com'
153
+ link_result = generate_result(link_raw)
154
+ link_result_with_options = %{<a href="#{link_raw}" target="_blank">#{link_raw}</a>}
155
+
156
+ assert_equal '', auto_link(nil)
157
+ assert_equal '', auto_link('')
158
+ assert_equal "#{link_result} #{link_result} #{link_result}", auto_link("#{link_raw} #{link_raw} #{link_raw}")
159
+
160
+ assert_equal %(hello #{email_result}), auto_link("hello #{email_raw}", :email_addresses)
161
+ assert_equal %(Go to #{link_result}), auto_link("Go to #{link_raw}", :urls)
162
+ assert_equal %(Go to #{link_raw}), auto_link("Go to #{link_raw}", :email_addresses)
163
+ assert_equal %(Go to #{link_result} and say hello to #{email_result}), auto_link("Go to #{link_raw} and say hello to #{email_raw}")
164
+ assert_equal %(<p>Link #{link_result}</p>), auto_link("<p>Link #{link_raw}</p>")
165
+ assert_equal %(<p>#{link_result} Link</p>), auto_link("<p>#{link_raw} Link</p>")
166
+ assert_equal %(<p>Link #{link_result_with_options}</p>), auto_link("<p>Link #{link_raw}</p>", :all, {:target => "_blank"})
167
+ assert_equal %(Go to #{link_result}.), auto_link(%(Go to #{link_raw}.))
168
+ assert_equal %(<p>Go to #{link_result}, then say hello to #{email_result}.</p>), auto_link(%(<p>Go to #{link_raw}, then say hello to #{email_raw}.</p>))
169
+ assert_equal %(#{link_result} #{link_result}), auto_link(%(#{link_result} #{link_raw}))
170
+
171
+ email2_raw = '+david@loudthinking.com'
172
+ email2_result = %{<a href="mailto:#{email2_raw}">#{email2_raw}</a>}
173
+ assert_equal email2_result, auto_link(email2_raw)
174
+
175
+ email3_raw = '+david@loudthinking.com'
176
+ email3_result = %{<a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;+%64%61%76%69%64@%6c%6f%75%64%74%68%69%6e%6b%69%6e%67.%63%6f%6d">#{email3_raw}</a>}
177
+ assert_equal email3_result, auto_link(email3_raw, :all, :encode => :hex)
178
+ assert_equal email3_result, auto_link(email3_raw, :email_addresses, :encode => :hex)
179
+
180
+ link2_raw = 'www.rubyonrails.com'
181
+ link2_result = generate_result(link2_raw, "http://#{link2_raw}")
182
+ assert_equal %(Go to #{link2_result}), auto_link("Go to #{link2_raw}", :urls)
183
+ assert_equal %(Go to #{link2_raw}), auto_link("Go to #{link2_raw}", :email_addresses)
184
+ assert_equal %(<p>Link #{link2_result}</p>), auto_link("<p>Link #{link2_raw}</p>")
185
+ assert_equal %(<p>#{link2_result} Link</p>), auto_link("<p>#{link2_raw} Link</p>")
186
+ assert_equal %(Go to #{link2_result}.), auto_link(%(Go to #{link2_raw}.))
187
+ assert_equal %(<p>Say hello to #{email_result}, then go to #{link2_result}.</p>), auto_link(%(<p>Say hello to #{email_raw}, then go to #{link2_raw}.</p>))
188
+
189
+ link3_raw = 'http://manuals.ruby-on-rails.com/read/chapter.need_a-period/103#page281'
190
+ link3_result = generate_result(link3_raw)
191
+ assert_equal %(Go to #{link3_result}), auto_link("Go to #{link3_raw}", :urls)
192
+ assert_equal %(Go to #{link3_raw}), auto_link("Go to #{link3_raw}", :email_addresses)
193
+ assert_equal %(<p>Link #{link3_result}</p>), auto_link("<p>Link #{link3_raw}</p>")
194
+ assert_equal %(<p>#{link3_result} Link</p>), auto_link("<p>#{link3_raw} Link</p>")
195
+ assert_equal %(Go to #{link3_result}.), auto_link(%(Go to #{link3_raw}.))
196
+ assert_equal %(<p>Go to #{link3_result}. Seriously, #{link3_result}? I think I'll say hello to #{email_result}. Instead.</p>),
197
+ auto_link(%(<p>Go to #{link3_raw}. Seriously, #{link3_raw}? I think I'll say hello to #{email_raw}. Instead.</p>))
198
+
199
+ link4_raw = 'http://foo.example.com/controller/action?parm=value&p2=v2#anchor123'
200
+ link4_result = generate_result(link4_raw)
201
+ assert_equal %(<p>Link #{link4_result}</p>), auto_link("<p>Link #{link4_raw}</p>")
202
+ assert_equal %(<p>#{link4_result} Link</p>), auto_link("<p>#{link4_raw} Link</p>")
203
+
204
+ link5_raw = 'http://foo.example.com:3000/controller/action'
205
+ link5_result = generate_result(link5_raw)
206
+ assert_equal %(<p>#{link5_result} Link</p>), auto_link("<p>#{link5_raw} Link</p>")
207
+
208
+ link6_raw = 'http://foo.example.com:3000/controller/action+pack'
209
+ link6_result = generate_result(link6_raw)
210
+ assert_equal %(<p>#{link6_result} Link</p>), auto_link("<p>#{link6_raw} Link</p>")
211
+
212
+ link7_raw = 'http://foo.example.com/controller/action?parm=value&p2=v2#anchor-123'
213
+ link7_result = generate_result(link7_raw)
214
+ assert_equal %(<p>#{link7_result} Link</p>), auto_link("<p>#{link7_raw} Link</p>")
215
+
216
+ link8_raw = 'http://foo.example.com:3000/controller/action.html'
217
+ link8_result = generate_result(link8_raw)
218
+ assert_equal %(Go to #{link8_result}), auto_link("Go to #{link8_raw}", :urls)
219
+ assert_equal %(Go to #{link8_raw}), auto_link("Go to #{link8_raw}", :email_addresses)
220
+ assert_equal %(<p>Link #{link8_result}</p>), auto_link("<p>Link #{link8_raw}</p>")
221
+ assert_equal %(<p>#{link8_result} Link</p>), auto_link("<p>#{link8_raw} Link</p>")
222
+ assert_equal %(Go to #{link8_result}.), auto_link(%(Go to #{link8_raw}.))
223
+ assert_equal %(<p>Go to #{link8_result}. Seriously, #{link8_result}? I think I'll say hello to #{email_result}. Instead.</p>),
224
+ auto_link(%(<p>Go to #{link8_raw}. Seriously, #{link8_raw}? I think I'll say hello to #{email_raw}. Instead.</p>))
225
+
226
+ link9_raw = 'http://business.timesonline.co.uk/article/0,,9065-2473189,00.html'
227
+ link9_result = generate_result(link9_raw)
228
+ assert_equal %(Go to #{link9_result}), auto_link("Go to #{link9_raw}", :urls)
229
+ assert_equal %(Go to #{link9_raw}), auto_link("Go to #{link9_raw}", :email_addresses)
230
+ assert_equal %(<p>Link #{link9_result}</p>), auto_link("<p>Link #{link9_raw}</p>")
231
+ assert_equal %(<p>#{link9_result} Link</p>), auto_link("<p>#{link9_raw} Link</p>")
232
+ assert_equal %(Go to #{link9_result}.), auto_link(%(Go to #{link9_raw}.))
233
+ assert_equal %(<p>Go to #{link9_result}. Seriously, #{link9_result}? I think I'll say hello to #{email_result}. Instead.</p>),
234
+ auto_link(%(<p>Go to #{link9_raw}. Seriously, #{link9_raw}? I think I'll say hello to #{email_raw}. Instead.</p>))
235
+
236
+ link10_raw = 'http://www.mail-archive.com/ruby-talk@ruby-lang.org/'
237
+ link10_result = generate_result(link10_raw)
238
+ assert_equal %(<p>#{link10_result} Link</p>), auto_link("<p>#{link10_raw} Link</p>")
239
+
240
+ link11_raw = 'http://asakusa.rubyist.net/'
241
+ link11_result = generate_result(link11_raw)
242
+ with_kcode 'u' do
243
+ assert_equal %(浅草.rbの公式サイトはこちら#{link11_result}), auto_link("浅草.rbの公式サイトはこちら#{link11_raw}")
244
+ end
245
+ end
246
+
247
+ def test_auto_link_parsing
248
+ urls = %w(
249
+ http://www.rubyonrails.com
250
+ http://www.rubyonrails.com:80
251
+ http://www.rubyonrails.com/~minam
252
+ https://www.rubyonrails.com/~minam
253
+ http://www.rubyonrails.com/~minam/url%20with%20spaces
254
+ http://www.rubyonrails.com/foo.cgi?something=here
255
+ http://www.rubyonrails.com/foo.cgi?something=here&and=here
256
+ http://www.rubyonrails.com/contact;new
257
+ http://www.rubyonrails.com/contact;new%20with%20spaces
258
+ http://www.rubyonrails.com/contact;new?with=query&string=params
259
+ http://www.rubyonrails.com/~minam/contact;new?with=query&string=params
260
+ http://en.wikipedia.org/wiki/Wikipedia:Today%27s_featured_picture_%28animation%29/January_20%2C_2007
261
+ http://www.mail-archive.com/rails@lists.rubyonrails.org/
262
+ http://www.amazon.com/Testing-Equal-Sign-In-Path/ref=pd_bbs_sr_1?ie=UTF8&s=books&qid=1198861734&sr=8-1
263
+ http://en.wikipedia.org/wiki/Texas_hold'em
264
+ https://www.google.com/doku.php?id=gps:resource:scs:start
265
+ http://connect.oraclecorp.com/search?search[q]=green+france&search[type]=Group
266
+ http://of.openfoundry.org/projects/492/download#4th.Release.3
267
+ 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
268
+ )
269
+
270
+ urls.each do |url|
271
+ assert_equal generate_result(url), auto_link(url)
272
+ end
273
+ end
274
+
275
+ private
276
+ def generate_result(link_text, href = nil, escape = false)
277
+ href ||= link_text
278
+ if escape
279
+ %{<a href="#{CGI::escapeHTML href}">#{CGI::escapeHTML link_text}</a>}
280
+ else
281
+ %{<a href="#{href}">#{link_text}</a>}
282
+ end
283
+ end
284
+
285
+ # from ruby core
286
+ def build_message(head, template=nil, *arguments)
287
+ template &&= template.chomp
288
+ template.gsub(/\?/) { mu_pp(arguments.shift) }
289
+ end
290
+
291
+ # Temporarily replaces KCODE for the block
292
+ def with_kcode(kcode)
293
+ if RUBY_VERSION < '1.9'
294
+ old_kcode, $KCODE = $KCODE, kcode
295
+ begin
296
+ yield
297
+ ensure
298
+ $KCODE = old_kcode
299
+ end
300
+ else
301
+ yield
302
+ end
303
+ end
304
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails_autolink
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease:
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Aaron Patterson
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-05-02 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rails
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 3
31
+ - 1
32
+ - 0
33
+ version: 3.1.0
34
+ type: :runtime
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: hoe
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ hash: 35
45
+ segments:
46
+ - 2
47
+ - 9
48
+ - 4
49
+ version: 2.9.4
50
+ type: :development
51
+ version_requirements: *id002
52
+ description: |-
53
+ This is an extraction of the `auto_link` method from rails. The `auto_link`
54
+ method was removed from Rails in version Rails 3.1. This gem is meant to
55
+ bridge the gap for people migrating.
56
+ email:
57
+ - aaron@tenderlovemaking.com
58
+ executables: []
59
+
60
+ extensions: []
61
+
62
+ extra_rdoc_files:
63
+ - Manifest.txt
64
+ - CHANGELOG.rdoc
65
+ - README.rdoc
66
+ files:
67
+ - .autotest
68
+ - CHANGELOG.rdoc
69
+ - Gemfile
70
+ - Manifest.txt
71
+ - README.rdoc
72
+ - Rakefile
73
+ - lib/rails_autolink.rb
74
+ - test/test_rails_autolink.rb
75
+ - .gemtest
76
+ homepage: http://github.com/tenderlove/rails_autolink
77
+ licenses: []
78
+
79
+ post_install_message:
80
+ rdoc_options:
81
+ - --main
82
+ - README.rdoc
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ hash: 3
91
+ segments:
92
+ - 0
93
+ version: "0"
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ hash: 3
100
+ segments:
101
+ - 0
102
+ version: "0"
103
+ requirements: []
104
+
105
+ rubyforge_project: rails_autolink
106
+ rubygems_version: 1.7.2
107
+ signing_key:
108
+ specification_version: 3
109
+ summary: This is an extraction of the `auto_link` method from rails
110
+ test_files:
111
+ - test/test_rails_autolink.rb