@@ -4,154 +4,7 @@ module RailsAutolink
44 class Railtie < ::Rails ::Railtie
55 initializer 'rails_autolink' do |app |
66 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 [email protected] ") 25- # # => "Go to <a href=\"http://www.rubyonrails.org\">http://www.rubyonrails.org</a> and
26- # # say hello to <a href=\"mailto:[email protected] \">[email protected] </a>" 27- #
28- # auto_link("Visit http://www.loudthinking.com/ or e-mail [email protected] ", :link => :urls) 29- # # => "Visit <a href=\"http://www.loudthinking.com/\">http://www.loudthinking.com/</a>
30- 31- #
32- # auto_link("Visit http://www.loudthinking.com/ or e-mail [email protected] ", :link => :email_addresses) 33- # # => "Visit http://www.loudthinking.com/ or e-mail <a href=\"mailto:[email protected] \">[email protected] </a>" 34- #
35- # post_body = "Welcome to my new blog at http://www.myblog.com/. Please e-mail me at [email protected] ." 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:[email protected] \">[email protected] </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 [email protected] ." 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 [email protected] ." 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:[email protected] \">[email protected] </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- sanitize = ( options [ :sanitize ] != false )
64- text = conditional_sanitize ( text , sanitize ) . to_str
65- case options [ :link ] . to_sym
66- when :all then conditional_html_safe ( auto_link_email_addresses ( auto_link_urls ( text , options [ :html ] , options , &block ) , options [ :html ] , &block ) , sanitize )
67- when :email_addresses then conditional_html_safe ( auto_link_email_addresses ( text , options [ :html ] , &block ) , sanitize )
68- when :urls then conditional_html_safe ( auto_link_urls ( text , options [ :html ] , options , &block ) , sanitize )
69- end
70- end
71-
72- private
73-
74- AUTO_LINK_RE = %r{
75- (?: ([0-9A-Za-z+.:-]+:)// | www\. )
76- [^\s <]+
77- }x
78-
79- # regexps for determining context, used high-volume
80- AUTO_LINK_CRE = [ /<[^>]+$/ , /^[^>]*>/ , /<a\b .*?>/i , /<\/ a>/i ]
81-
82- AUTO_EMAIL_RE = /[\w .!#\$ %+-]+@[\w -]+(?:\. [\w -]+)+/
83-
84- BRACKETS = { ']' => '[' , ')' => '(' , '}' => '{' }
85-
86- # Turns all urls into clickable links. If a block is given, each url
87- # is yielded and the result is used as the link text.
88- def auto_link_urls ( text , html_options = { } , options = { } )
89- link_attributes = html_options . stringify_keys
90- text . gsub ( AUTO_LINK_RE ) do
91- scheme , href = $1, $&
92- punctuation = [ ]
93-
94- if auto_linked? ( $`, $')
95- # do not change string; URL is already linked
96- href
97- else
98- # don't include trailing punctuation character as part of the URL
99- while href . sub! ( /[^\w \/ -]$/ , '' )
100- punctuation . push $&
101- if opening = BRACKETS [ punctuation . last ] and href . scan ( opening ) . size > href . scan ( punctuation . last ) . size
102- href << punctuation . pop
103- break
104- end
105- end
106-
107- link_text = block_given? ? yield ( href ) : href
108- href = 'http://' + href unless scheme
109-
110- unless options [ :sanitize ] == false
111- link_text = sanitize ( link_text )
112- href = sanitize ( href )
113- end
114- content_tag ( :a , link_text , link_attributes . merge ( 'href' => href ) , !!options [ :sanitize ] ) + punctuation . reverse . join ( '' )
115- end
116- end
117- end
118-
119- # Turns all email addresses into clickable links. If a block is given,
120- # each email is yielded and the result is used as the link text.
121- def auto_link_email_addresses ( text , html_options = { } , options = { } )
122- text . gsub ( AUTO_EMAIL_RE ) do
123- text = $&
124-
125- if auto_linked? ( $`, $')
126- text . html_safe
127- else
128- display_text = ( block_given? ) ? yield ( text ) : text
129-
130- unless options [ :sanitize ] == false
131- text = sanitize ( text )
132- display_text = sanitize ( display_text ) unless text == display_text
133- end
134- mail_to text , display_text , html_options
135- end
136- end
137- end
138-
139- # Detects already linked context or position in the middle of a tag
140- def auto_linked? ( left , right )
141- ( left =~ AUTO_LINK_CRE [ 0 ] and right =~ AUTO_LINK_CRE [ 1 ] ) or
142- ( left . rindex ( AUTO_LINK_CRE [ 2 ] ) and $' !~ AUTO_LINK_CRE [ 3 ] )
143- end
144-
145- def conditional_sanitize ( target , condition )
146- condition ? sanitize ( target ) : target
147- end
148-
149- def conditional_html_safe ( target , condition )
150- condition ? target . html_safe : target
151- end
152- end
153- end
154- end
7+ require 'rails_autolink/helpers'
1558 end
1569 end
15710 end
0 commit comments