|
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 | 1 | 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. By default the |
18 | | - # text given is sanitized, you can override this behaviour setting the |
19 | | - # <tt>:sanitize</tt> option to false. |
20 | | - # |
21 | | - # ==== Examples |
22 | | - # auto_link("Go to http://www.rubyonrails.org and say hello to [email protected]") |
23 | | - # # => "Go to <a href=\"http://www.rubyonrails.org\">http://www.rubyonrails.org</a> and |
24 | | - # # say hello to <a href=\"mailto:[email protected]\">[email protected]</a>" |
25 | | - # |
26 | | - # auto_link("Visit http://www.loudthinking.com/ or e-mail [email protected]", :link => :urls) |
27 | | - # # => "Visit <a href=\"http://www.loudthinking.com/\">http://www.loudthinking.com/</a> |
28 | | - |
29 | | - # |
30 | | - # auto_link("Visit http://www.loudthinking.com/ or e-mail [email protected]", :link => :email_addresses) |
31 | | - # # => "Visit http://www.loudthinking.com/ or e-mail <a href=\"mailto:[email protected]\">[email protected]</a>" |
32 | | - # |
33 | | - # post_body = "Welcome to my new blog at http://www.myblog.com/. Please e-mail me at [email protected]." |
34 | | - # auto_link(post_body, :html => { :target => '_blank' }) do |text| |
35 | | - # truncate(text, :length => 15) |
36 | | - # end |
37 | | - # # => "Welcome to my new blog at <a href=\"http://www.myblog.com/\" target=\"_blank\">http://www.m...</a>. |
38 | | - # Please e-mail me at <a href=\"mailto:[email protected]\">[email protected]</a>." |
39 | | - # |
40 | | - # |
41 | | - # You can still use <tt>auto_link</tt> with the old API that accepts the |
42 | | - # +link+ as its optional second parameter and the +html_options+ hash |
43 | | - # as its optional third parameter: |
44 | | - # post_body = "Welcome to my new blog at http://www.myblog.com/. Please e-mail me at [email protected]." |
45 | | - # auto_link(post_body, :urls) |
46 | | - # # => "Welcome to my new blog at <a href=\"http://www.myblog.com/\">http://www.myblog.com</a>. |
47 | | - # Please e-mail me at [email protected]." |
48 | | - # |
49 | | - # auto_link(post_body, :all, :target => "_blank") |
50 | | - # # => "Welcome to my new blog at <a href=\"http://www.myblog.com/\" target=\"_blank\">http://www.myblog.com</a>. |
51 | | - # Please e-mail me at <a href=\"mailto:[email protected]\">[email protected]</a>." |
52 | | - def auto_link(text, *args, &block)#link = :all, html = {}, &block) |
53 | | - return ''.html_safe if text.blank? |
54 | | - |
55 | | - options = args.size == 2 ? {} : args.extract_options! # this is necessary because the old auto_link API has a Hash as its last parameter |
56 | | - unless args.empty? |
57 | | - options[:link] = args[0] || :all |
58 | | - options[:html] = args[1] || {} |
59 | | - end |
60 | | - options.reverse_merge!(:link => :all, :html => {}) |
61 | | - text = sanitize(text) unless options[:sanitize] == false |
62 | | - case options[:link].to_sym |
63 | | - when :all then auto_link_email_addresses(auto_link_urls(text, options[:html], options, &block), options[:html], &block) |
64 | | - when :email_addresses then auto_link_email_addresses(text, options[:html], &block) |
65 | | - when :urls then auto_link_urls(text, options[:html], options, &block) |
66 | | - end |
67 | | - end |
68 | | - |
69 | | - private |
70 | | - |
71 | | - AUTO_LINK_RE = %r{ |
72 | | - (?: ([0-9A-Za-z+.:-]+:)// | www\. ) |
73 | | - [^\s<]+ |
74 | | - }x |
75 | | - |
76 | | - # regexps for determining context, used high-volume |
77 | | - AUTO_LINK_CRE = [/<[^>]+$/, /^[^>]*>/, /<a\b.*?>/i, /<\/a>/i] |
78 | | - |
79 | | - AUTO_EMAIL_RE = /[\w.!#\$%+-]+@[\w-]+(?:\.[\w-]+)+/ |
80 | | - |
81 | | - BRACKETS = { ']' => '[', ')' => '(', '}' => '{' } |
82 | | - |
83 | | - # Turns all urls into clickable links. If a block is given, each url |
84 | | - # is yielded and the result is used as the link text. |
85 | | - def auto_link_urls(text, html_options = {}, options = {}) |
86 | | - link_attributes = html_options.stringify_keys |
87 | | - text.gsub(AUTO_LINK_RE) do |
88 | | - scheme, href = $1, $& |
89 | | - punctuation = [] |
90 | | - |
91 | | - if auto_linked?($`, $') |
92 | | - # do not change string; URL is already linked |
93 | | - href |
94 | | - else |
95 | | - # don't include trailing punctuation character as part of the URL |
96 | | - while href.sub!(/[^\w\/-]$/, '') |
97 | | - punctuation.push $& |
98 | | - if opening = BRACKETS[punctuation.last] and href.scan(opening).size > href.scan(punctuation.last).size |
99 | | - href << punctuation.pop |
100 | | - break |
| 2 | + VERSION = '1.0.1' |
| 3 | + |
| 4 | + class Railtie < ::Rails::Railtie |
| 5 | + initializer 'nested_form' do |app| |
| 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 [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 | + 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) |
101 | 68 | end |
102 | 69 | end |
103 | 70 |
|
104 | | - link_text = block_given?? yield(href) : href |
105 | | - href = 'http://' + href unless scheme |
106 | | - |
107 | | - unless options[:sanitize] == false |
108 | | - link_text = sanitize(link_text) |
109 | | - href = sanitize(href) |
110 | | - end |
111 | | - content_tag(:a, link_text, link_attributes.merge('href' => href), !!options[:sanitize]) + punctuation.reverse.join('') |
112 | | - end |
113 | | - end |
114 | | - end |
115 | | - |
116 | | - # Turns all email addresses into clickable links. If a block is given, |
117 | | - # each email is yielded and the result is used as the link text. |
118 | | - def auto_link_email_addresses(text, html_options = {}, options = {}) |
119 | | - text.gsub(AUTO_EMAIL_RE) do |
120 | | - text = $& |
| 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 |
121 | 117 |
|
122 | | - if auto_linked?($`, $') |
123 | | - text.html_safe |
124 | | - else |
125 | | - display_text = (block_given?) ? yield(text) : text |
| 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 |
126 | 137 |
|
127 | | - unless options[:sanitize] == false |
128 | | - text = sanitize(text) |
129 | | - display_text = sanitize(display_text) unless text == display_text |
130 | | - end |
131 | | - mail_to text, display_text, html_options |
| 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 |
132 | 143 | end |
133 | 144 | end |
134 | 145 | end |
135 | | - |
136 | | - # Detects already linked context or position in the middle of a tag |
137 | | - def auto_linked?(left, right) |
138 | | - (left =~ AUTO_LINK_CRE[0] and right =~ AUTO_LINK_CRE[1]) or |
139 | | - (left.rindex(AUTO_LINK_CRE[2]) and $' !~ AUTO_LINK_CRE[3]) |
140 | | - end |
| 146 | + end |
141 | 147 | end |
142 | 148 | end |
143 | 149 | end |
0 commit comments