rails_autolink 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.rdoc CHANGED
@@ -1,3 +1,7 @@
1
+ === 1.0.2 / 2011-06-18
2
+
3
+ * Compatibility with rails 3.1.0.rc4
4
+
1
5
  === 1.0.0 / 2011-05-02
2
6
 
3
7
  * 1 major enhancement
data/Manifest.txt CHANGED
@@ -1,8 +1,7 @@
1
- .autotest
2
1
  CHANGELOG.rdoc
3
- Gemfile
4
2
  Manifest.txt
5
3
  README.rdoc
6
4
  Rakefile
7
5
  lib/rails_autolink.rb
6
+ lib/rails_autolink/helpers.rb
8
7
  test/test_rails_autolink.rb
data/Rakefile CHANGED
@@ -10,6 +10,8 @@ Hoe.plugin :git # `gem install hoe-git`
10
10
 
11
11
  Hoe.spec 'rails_autolink' do
12
12
  developer('Aaron Patterson', 'aaron@tenderlovemaking.com')
13
+ developer('Juanjo Bazan', 'jjbazan@gmail.com')
14
+ developer('Akira Matsuda', 'ronnie@dio.jp')
13
15
  self.readme_file = 'README.rdoc'
14
16
  self.history_file = 'CHANGELOG.rdoc'
15
17
  self.extra_rdoc_files = FileList['*.rdoc']
@@ -1,148 +1,10 @@
1
1
  module RailsAutolink
2
- VERSION = '1.0.1'
2
+ VERSION = '1.0.2'
3
3
 
4
4
  class Railtie < ::Rails::Railtie
5
- initializer 'nested_form' do |app|
5
+ initializer 'rails_autolink' do |app|
6
6
  ActiveSupport.on_load(:action_view) do
7
- require 'active_support/core_ext/object/blank'
8
- require 'active_support/core_ext/array/extract_options'
9
- require 'active_support/core_ext/hash/reverse_merge'
10
- require 'active_support/core_ext/hash/keys'
11
-
12
- module ::ActionView
13
- module Helpers # :nodoc:
14
- module TextHelper
15
- # Turns all URLs and e-mail addresses into clickable links. The <tt>:link</tt> option
16
- # will limit what should be linked. You can add HTML attributes to the links using
17
- # <tt>:html</tt>. Possible values for <tt>:link</tt> are <tt>:all</tt> (default),
18
- # <tt>:email_addresses</tt>, and <tt>:urls</tt>. If a block is given, each URL and
19
- # e-mail address is yielded and the result is used as the link text. By default the
20
- # text given is sanitized, you can override this behaviour setting the
21
- # <tt>:sanitize</tt> option to false.
22
- #
23
- # ==== Examples
24
- # auto_link("Go to http://www.rubyonrails.org and say hello to david@loudthinking.com")
25
- # # => "Go to <a href=\"http://www.rubyonrails.org\">http://www.rubyonrails.org</a> and
26
- # # say hello to <a href=\"mailto:david@loudthinking.com\">david@loudthinking.com</a>"
27
- #
28
- # auto_link("Visit http://www.loudthinking.com/ or e-mail david@loudthinking.com", :link => :urls)
29
- # # => "Visit <a href=\"http://www.loudthinking.com/\">http://www.loudthinking.com/</a>
30
- # # or e-mail david@loudthinking.com"
31
- #
32
- # auto_link("Visit http://www.loudthinking.com/ or e-mail david@loudthinking.com", :link => :email_addresses)
33
- # # => "Visit http://www.loudthinking.com/ or e-mail <a href=\"mailto:david@loudthinking.com\">david@loudthinking.com</a>"
34
- #
35
- # post_body = "Welcome to my new blog at http://www.myblog.com/. Please e-mail me at me@email.com."
36
- # auto_link(post_body, :html => { :target => '_blank' }) do |text|
37
- # truncate(text, :length => 15)
38
- # end
39
- # # => "Welcome to my new blog at <a href=\"http://www.myblog.com/\" target=\"_blank\">http://www.m...</a>.
40
- # Please e-mail me at <a href=\"mailto:me@email.com\">me@email.com</a>."
41
- #
42
- #
43
- # You can still use <tt>auto_link</tt> with the old API that accepts the
44
- # +link+ as its optional second parameter and the +html_options+ hash
45
- # as its optional third parameter:
46
- # post_body = "Welcome to my new blog at http://www.myblog.com/. Please e-mail me at me@email.com."
47
- # auto_link(post_body, :urls)
48
- # # => "Welcome to my new blog at <a href=\"http://www.myblog.com/\">http://www.myblog.com</a>.
49
- # Please e-mail me at me@email.com."
50
- #
51
- # auto_link(post_body, :all, :target => "_blank")
52
- # # => "Welcome to my new blog at <a href=\"http://www.myblog.com/\" target=\"_blank\">http://www.myblog.com</a>.
53
- # Please e-mail me at <a href=\"mailto:me@email.com\">me@email.com</a>."
54
- def auto_link(text, *args, &block)#link = :all, html = {}, &block)
55
- return ''.html_safe if text.blank?
56
-
57
- options = args.size == 2 ? {} : args.extract_options! # this is necessary because the old auto_link API has a Hash as its last parameter
58
- unless args.empty?
59
- options[:link] = args[0] || :all
60
- options[:html] = args[1] || {}
61
- end
62
- options.reverse_merge!(:link => :all, :html => {})
63
- text = sanitize(text) unless options[:sanitize] == false
64
- case options[:link].to_sym
65
- when :all then auto_link_email_addresses(auto_link_urls(text, options[:html], options, &block), options[:html], &block)
66
- when :email_addresses then auto_link_email_addresses(text, options[:html], &block)
67
- when :urls then auto_link_urls(text, options[:html], options, &block)
68
- end
69
- end
70
-
71
- private
72
-
73
- AUTO_LINK_RE = %r{
74
- (?: ([0-9A-Za-z+.:-]+:)// | www\. )
75
- [^\s<]+
76
- }x
77
-
78
- # regexps for determining context, used high-volume
79
- AUTO_LINK_CRE = [/<[^>]+$/, /^[^>]*>/, /<a\b.*?>/i, /<\/a>/i]
80
-
81
- AUTO_EMAIL_RE = /[\w.!#\$%+-]+@[\w-]+(?:\.[\w-]+)+/
82
-
83
- BRACKETS = { ']' => '[', ')' => '(', '}' => '{' }
84
-
85
- # Turns all urls into clickable links. If a block is given, each url
86
- # is yielded and the result is used as the link text.
87
- def auto_link_urls(text, html_options = {}, options = {})
88
- link_attributes = html_options.stringify_keys
89
- text.gsub(AUTO_LINK_RE) do
90
- scheme, href = $1, $&
91
- punctuation = []
92
-
93
- if auto_linked?($`, $')
94
- # do not change string; URL is already linked
95
- href
96
- else
97
- # don't include trailing punctuation character as part of the URL
98
- while href.sub!(/[^\w\/-]$/, '')
99
- punctuation.push $&
100
- if opening = BRACKETS[punctuation.last] and href.scan(opening).size > href.scan(punctuation.last).size
101
- href << punctuation.pop
102
- break
103
- end
104
- end
105
-
106
- link_text = block_given?? yield(href) : href
107
- href = 'http://' + href unless scheme
108
-
109
- unless options[:sanitize] == false
110
- link_text = sanitize(link_text)
111
- href = sanitize(href)
112
- end
113
- content_tag(:a, link_text, link_attributes.merge('href' => href), !!options[:sanitize]) + punctuation.reverse.join('')
114
- end
115
- end
116
- end
117
-
118
- # Turns all email addresses into clickable links. If a block is given,
119
- # each email is yielded and the result is used as the link text.
120
- def auto_link_email_addresses(text, html_options = {}, options = {})
121
- text.gsub(AUTO_EMAIL_RE) do
122
- text = $&
123
-
124
- if auto_linked?($`, $')
125
- text.html_safe
126
- else
127
- display_text = (block_given?) ? yield(text) : text
128
-
129
- unless options[:sanitize] == false
130
- text = sanitize(text)
131
- display_text = sanitize(display_text) unless text == display_text
132
- end
133
- mail_to text, display_text, html_options
134
- end
135
- end
136
- end
137
-
138
- # Detects already linked context or position in the middle of a tag
139
- def auto_linked?(left, right)
140
- (left =~ AUTO_LINK_CRE[0] and right =~ AUTO_LINK_CRE[1]) or
141
- (left.rindex(AUTO_LINK_CRE[2]) and $' !~ AUTO_LINK_CRE[3])
142
- end
143
- end
144
- end
145
- end
7
+ require 'rails_autolink/helpers'
146
8
  end
147
9
  end
148
10
  end
@@ -0,0 +1,150 @@
1
+ module RailsAutolink
2
+ require 'active_support/core_ext/object/blank'
3
+ require 'active_support/core_ext/array/extract_options'
4
+ require 'active_support/core_ext/hash/reverse_merge'
5
+ require 'active_support/core_ext/hash/keys'
6
+
7
+ module ::ActionView
8
+ module Helpers # :nodoc:
9
+ module TextHelper
10
+ # Turns all URLs and e-mail addresses into clickable links. The <tt>:link</tt> option
11
+ # will limit what should be linked. You can add HTML attributes to the links using
12
+ # <tt>:html</tt>. Possible values for <tt>:link</tt> are <tt>:all</tt> (default),
13
+ # <tt>:email_addresses</tt>, and <tt>:urls</tt>. If a block is given, each URL and
14
+ # e-mail address is yielded and the result is used as the link text. By default the
15
+ # text given is sanitized, you can override this behaviour setting the
16
+ # <tt>:sanitize</tt> option to false.
17
+ #
18
+ # ==== Examples
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
+ # Please e-mail me at <a href=\"mailto:me@email.com\">me@email.com</a>."
36
+ #
37
+ #
38
+ # You can still use <tt>auto_link</tt> with the old API that accepts the
39
+ # +link+ as its optional second parameter and the +html_options+ hash
40
+ # as its optional third parameter:
41
+ # post_body = "Welcome to my new blog at http://www.myblog.com/. Please e-mail me at me@email.com."
42
+ # auto_link(post_body, :urls)
43
+ # # => "Welcome to my new blog at <a href=\"http://www.myblog.com/\">http://www.myblog.com</a>.
44
+ # Please e-mail me at me@email.com."
45
+ #
46
+ # auto_link(post_body, :all, :target => "_blank")
47
+ # # => "Welcome to my new blog at <a href=\"http://www.myblog.com/\" target=\"_blank\">http://www.myblog.com</a>.
48
+ # Please e-mail me at <a href=\"mailto:me@email.com\">me@email.com</a>."
49
+ def auto_link(text, *args, &block)#link = :all, html = {}, &block)
50
+ return ''.html_safe if text.blank?
51
+
52
+ options = args.size == 2 ? {} : args.extract_options! # this is necessary because the old auto_link API has a Hash as its last parameter
53
+ unless args.empty?
54
+ options[:link] = args[0] || :all
55
+ options[:html] = args[1] || {}
56
+ end
57
+ options.reverse_merge!(:link => :all, :html => {})
58
+ sanitize = (options[:sanitize] != false)
59
+ text = conditional_sanitize(text, sanitize).to_str
60
+ case options[:link].to_sym
61
+ when :all then conditional_html_safe(auto_link_email_addresses(auto_link_urls(text, options[:html], options, &block), options[:html], &block), sanitize)
62
+ when :email_addresses then conditional_html_safe(auto_link_email_addresses(text, options[:html], &block), sanitize)
63
+ when :urls then conditional_html_safe(auto_link_urls(text, options[:html], options, &block), sanitize)
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
+
140
+ def conditional_sanitize(target, condition)
141
+ condition ? sanitize(target) : target
142
+ end
143
+
144
+ def conditional_html_safe(target, condition)
145
+ condition ? target.html_safe : target
146
+ end
147
+ end
148
+ end
149
+ end
150
+ end
@@ -1,11 +1,11 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  require "minitest/autorun"
4
- require "rails_autolink"
4
+ require "rails"
5
+ require "rails_autolink/helpers"
5
6
  require 'erb'
6
7
  require 'cgi'
7
8
  require 'active_support/core_ext/class/attribute_accessors'
8
- require 'active_support'
9
9
  require 'action_pack'
10
10
  require 'action_view/helpers/capture_helper'
11
11
  require 'action_view/helpers/sanitize_helper'
@@ -13,7 +13,7 @@ require 'action_view/helpers/url_helper'
13
13
  require 'action_view/helpers/tag_helper'
14
14
  require 'active_support/core_ext/module/attribute_accessors'
15
15
  require 'active_support/core_ext/string/encoding'
16
- require 'action_dispatch/testing/assertions/dom'
16
+ require 'action_dispatch/testing/assertions'
17
17
  require 'action_view/helpers/text_helper'
18
18
  require 'action_view/helpers/output_safety_helper'
19
19
 
metadata CHANGED
@@ -1,21 +1,19 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_autolink
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
5
4
  prerelease:
6
- segments:
7
- - 1
8
- - 0
9
- - 1
10
- version: 1.0.1
5
+ version: 1.0.2
11
6
  platform: ruby
12
7
  authors:
13
8
  - Aaron Patterson
9
+ - Juanjo Bazan
10
+ - Akira Matsuda
14
11
  autorequire:
15
12
  bindir: bin
16
13
  cert_chain: []
17
14
 
18
- date: 2011-05-06 00:00:00 Z
15
+ date: 2011-06-18 00:00:00 +02:00
16
+ default_executable:
19
17
  dependencies:
20
18
  - !ruby/object:Gem::Dependency
21
19
  name: rails
@@ -25,37 +23,39 @@ dependencies:
25
23
  requirements:
26
24
  - - ~>
27
25
  - !ruby/object:Gem::Version
28
- hash: 18
29
- segments:
30
- - 3
31
- - 1
32
- - 0
33
- - a
34
26
  version: 3.1.0.a
35
27
  type: :runtime
36
28
  version_requirements: *id001
37
29
  - !ruby/object:Gem::Dependency
38
- name: hoe
30
+ name: minitest
39
31
  prerelease: false
40
32
  requirement: &id002 !ruby/object:Gem::Requirement
41
33
  none: false
42
34
  requirements:
43
35
  - - ">="
44
36
  - !ruby/object:Gem::Version
45
- hash: 35
46
- segments:
47
- - 2
48
- - 9
49
- - 4
50
- version: 2.9.4
37
+ version: 1.6.0
51
38
  type: :development
52
39
  version_requirements: *id002
40
+ - !ruby/object:Gem::Dependency
41
+ name: hoe
42
+ prerelease: false
43
+ requirement: &id003 !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: 2.9.4
49
+ type: :development
50
+ version_requirements: *id003
53
51
  description: |-
54
52
  This is an extraction of the `auto_link` method from rails. The `auto_link`
55
53
  method was removed from Rails in version Rails 3.1. This gem is meant to
56
54
  bridge the gap for people migrating.
57
55
  email:
58
56
  - aaron@tenderlovemaking.com
57
+ - jjbazan@gmail.com
58
+ - ronnie@dio.jp
59
59
  executables: []
60
60
 
61
61
  extensions: []
@@ -65,15 +65,15 @@ extra_rdoc_files:
65
65
  - CHANGELOG.rdoc
66
66
  - README.rdoc
67
67
  files:
68
- - .autotest
69
68
  - CHANGELOG.rdoc
70
- - Gemfile
71
69
  - Manifest.txt
72
70
  - README.rdoc
73
71
  - Rakefile
74
72
  - lib/rails_autolink.rb
73
+ - lib/rails_autolink/helpers.rb
75
74
  - test/test_rails_autolink.rb
76
75
  - .gemtest
76
+ has_rdoc: true
77
77
  homepage: http://github.com/tenderlove/rails_autolink
78
78
  licenses: []
79
79
 
@@ -88,23 +88,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
88
88
  requirements:
89
89
  - - ">="
90
90
  - !ruby/object:Gem::Version
91
- hash: 3
92
- segments:
93
- - 0
94
91
  version: "0"
95
92
  required_rubygems_version: !ruby/object:Gem::Requirement
96
93
  none: false
97
94
  requirements:
98
95
  - - ">="
99
96
  - !ruby/object:Gem::Version
100
- hash: 3
101
- segments:
102
- - 0
103
97
  version: "0"
104
98
  requirements: []
105
99
 
106
100
  rubyforge_project: rails_autolink
107
- rubygems_version: 1.7.2
101
+ rubygems_version: 1.6.2
108
102
  signing_key:
109
103
  specification_version: 3
110
104
  summary: This is an extraction of the `auto_link` method from rails
data/.autotest DELETED
@@ -1,8 +0,0 @@
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
data/Gemfile DELETED
@@ -1,8 +0,0 @@
1
- source 'http://rubygems.org'
2
-
3
- gem 'rails'
4
- gem 'arel'
5
- gem 'rack'
6
- gem 'hoe'
7
- gem 'minitest'
8
-