Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 647 lines (597 sloc) 27.958 kb
0eacdcf @jeremy Use a consistent load path to avoid double requires. Fix some scattered ...
jeremy authored
1 require 'action_view/helpers/tag_helper'
9853134 @josh Require missing libraries and check for defined ActionController constan...
josh authored
2
3 begin
4 require 'html/document'
5 rescue LoadError
6 html_scanner_path = "#{File.dirname(__FILE__)}/../../action_controller/vendor/html-scanner"
7 if File.directory?(html_scanner_path)
8 $:.unshift html_scanner_path
9 require 'html/document'
10 end
11 end
5b3bc31 @dhh Added a third parameter to TextHelper#auto_link called href_options for ...
dhh authored
12
db045db @dhh Initial
dhh authored
13 module ActionView
14 module Helpers #:nodoc:
603c853 @josh Don't fallback to just adding "'s" in TextHelper#pluralize, because the ...
josh authored
15 # The TextHelper module provides a set of methods for filtering, formatting
16 # and transforming strings, which can reduce the amount of inline Ruby code in
17 # your views. These helper methods extend ActionView making them callable
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
18 # within your template files.
603c853 @josh Don't fallback to just adding "'s" in TextHelper#pluralize, because the ...
josh authored
19 module TextHelper
20 # The preferred method of outputting text in your views is to use the
21 # <%= "text" %> eRuby syntax. The regular _puts_ and _print_ methods
22 # do not operate as expected in an eRuby code block. If you absolutely must
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
23 # output text within a non-output code block (i.e., <% %>), you can use the concat method.
b34ed9b @dhh Docfix (closes #6393)
dhh authored
24 #
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
25 # ==== Examples
603c853 @josh Don't fallback to just adding "'s" in TextHelper#pluralize, because the ...
josh authored
26 # <%
4d4c8e2 @jeremy Don't pass block binding to concat
jeremy authored
27 # concat "hello"
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
28 # # is the equivalent of <%= "hello" %>
29 #
30 # if (logged_in == true):
4d4c8e2 @jeremy Don't pass block binding to concat
jeremy authored
31 # concat "Logged in!"
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
32 # else
4d4c8e2 @jeremy Don't pass block binding to concat
jeremy authored
33 # concat link_to('login', :action => login)
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
34 # end
35 # # will either display "Logged in!" or a login link
36 # %>
f545e19 @technoweenie add deprecation for the #concat helper's 2nd argument, which is no longe...
technoweenie authored
37 def concat(string, unused_binding = nil)
38 if unused_binding
350faf1 @jeremy Pass caller to concat deprecation warning
jeremy authored
39 ActiveSupport::Deprecation.warn("The binding argument of #concat is no longer needed. Please remove it from your views and helpers.", caller)
f545e19 @technoweenie add deprecation for the #concat helper's 2nd argument, which is no longe...
technoweenie authored
40 end
41
72f93b5 @jeremy Check whether blocks are called from erb using a special __in_erb_templa...
jeremy authored
42 output_buffer << string
db045db @dhh Initial
dhh authored
43 end
44
607511c @jeremy Ruby 1.9 compat: truncate and excerpt helpers
jeremy authored
45 if RUBY_VERSION < '1.9'
10d9fe4 @clemens Refactored TextHelper#truncate, highlight, excerpt, word_wrap and auto_l...
clemens authored
46 # Truncates a given +text+ after a given <tt>:length</tt> if +text+ is longer than <tt>:length</tt>
47 # (defaults to 30). The last characters will be replaced with the <tt>:omission</tt> (defaults to "...").
607511c @jeremy Ruby 1.9 compat: truncate and excerpt helpers
jeremy authored
48 #
49 # ==== Examples
50 #
603c853 @josh Don't fallback to just adding "'s" in TextHelper#pluralize, because the ...
josh authored
51 # truncate("Once upon a time in a world far far away")
607511c @jeremy Ruby 1.9 compat: truncate and excerpt helpers
jeremy authored
52 # # => Once upon a time in a world f...
53 #
10d9fe4 @clemens Refactored TextHelper#truncate, highlight, excerpt, word_wrap and auto_l...
clemens authored
54 # truncate("Once upon a time in a world far far away", :length => 14)
55 # # => Once upon a...
56 #
57 # truncate("And they found that many people were sleeping better.", :length => 25, "(clipped)")
607511c @jeremy Ruby 1.9 compat: truncate and excerpt helpers
jeremy authored
58 # # => And they found that many (clipped)
59 #
10d9fe4 @clemens Refactored TextHelper#truncate, highlight, excerpt, word_wrap and auto_l...
clemens authored
60 # truncate("And they found that many people were sleeping better.", :omission => "... (continued)", :length => 15)
61 # # => And they found... (continued)
62 #
63 # You can still use <tt>truncate</tt> with the old API that accepts the
64 # +length+ as its optional second and the +ellipsis+ as its
65 # optional third parameter:
66 # truncate("Once upon a time in a world far far away", 14)
67 # # => Once upon a time in a world f...
68 #
607511c @jeremy Ruby 1.9 compat: truncate and excerpt helpers
jeremy authored
69 # truncate("And they found that many people were sleeping better.", 15, "... (continued)")
70 # # => And they found... (continued)
10d9fe4 @clemens Refactored TextHelper#truncate, highlight, excerpt, word_wrap and auto_l...
clemens authored
71 def truncate(text, *args)
72 options = args.extract_options!
73 unless args.empty?
74 ActiveSupport::Deprecation.warn('truncate takes an option hash instead of separate ' +
75 'length and omission arguments', caller)
76
77 options[:length] = args[0] || 30
78 options[:omission] = args[1] || "..."
79 end
80 options.reverse_merge!(:length => 30, :omission => "...")
81
607511c @jeremy Ruby 1.9 compat: truncate and excerpt helpers
jeremy authored
82 if text
10d9fe4 @clemens Refactored TextHelper#truncate, highlight, excerpt, word_wrap and auto_l...
clemens authored
83 l = options[:length] - options[:omission].chars.length
607511c @jeremy Ruby 1.9 compat: truncate and excerpt helpers
jeremy authored
84 chars = text.chars
10d9fe4 @clemens Refactored TextHelper#truncate, highlight, excerpt, word_wrap and auto_l...
clemens authored
85 (chars.length > options[:length] ? chars[0...l] + options[:omission] : text).to_s
607511c @jeremy Ruby 1.9 compat: truncate and excerpt helpers
jeremy authored
86 end
87 end
88 else
10d9fe4 @clemens Refactored TextHelper#truncate, highlight, excerpt, word_wrap and auto_l...
clemens authored
89 def truncate(text, *args) #:nodoc:
90 options = args.extract_options!
91 unless args.empty?
92 ActiveSupport::Deprecation.warn('truncate takes an option hash instead of separate ' +
93 'length and omission arguments', caller)
94
95 options[:length] = args[0] || 30
96 options[:omission] = args[1] || "..."
97 end
98 options.reverse_merge!(:length => 30, :omission => "...")
99
607511c @jeremy Ruby 1.9 compat: truncate and excerpt helpers
jeremy authored
100 if text
10d9fe4 @clemens Refactored TextHelper#truncate, highlight, excerpt, word_wrap and auto_l...
clemens authored
101 l = options[:length].to_i - options[:omission].length
102 (text.length > options[:length].to_i ? text[0...l] + options[:omission] : text).to_s
607511c @jeremy Ruby 1.9 compat: truncate and excerpt helpers
jeremy authored
103 end
104 end
db045db @dhh Initial
dhh authored
105 end
106
67d5a1a @jeremy Highlight helper highlights one or many terms in a single pass.
jeremy authored
107 # Highlights one or more +phrases+ everywhere in +text+ by inserting it into
10d9fe4 @clemens Refactored TextHelper#truncate, highlight, excerpt, word_wrap and auto_l...
clemens authored
108 # a <tt>:highlighter</tt> string. The highlighter can be specialized by passing <tt>:highlighter</tt>
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
109 # as a single-quoted string with \1 where the phrase is to be inserted (defaults to
110 # '<strong class="highlight">\1</strong>')
b34ed9b @dhh Docfix (closes #6393)
dhh authored
111 #
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
112 # ==== Examples
603c853 @josh Don't fallback to just adding "'s" in TextHelper#pluralize, because the ...
josh authored
113 # highlight('You searched for: rails', 'rails')
67d5a1a @jeremy Highlight helper highlights one or many terms in a single pass.
jeremy authored
114 # # => You searched for: <strong class="highlight">rails</strong>
115 #
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
116 # highlight('You searched for: ruby, rails, dhh', 'actionpack')
603c853 @josh Don't fallback to just adding "'s" in TextHelper#pluralize, because the ...
josh authored
117 # # => You searched for: ruby, rails, dhh
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
118 #
10d9fe4 @clemens Refactored TextHelper#truncate, highlight, excerpt, word_wrap and auto_l...
clemens authored
119 # highlight('You searched for: rails', ['for', 'rails'], :highlighter => '<em>\1</em>')
67d5a1a @jeremy Highlight helper highlights one or many terms in a single pass.
jeremy authored
120 # # => You searched <em>for</em>: <em>rails</em>
603c853 @josh Don't fallback to just adding "'s" in TextHelper#pluralize, because the ...
josh authored
121 #
10d9fe4 @clemens Refactored TextHelper#truncate, highlight, excerpt, word_wrap and auto_l...
clemens authored
122 # highlight('You searched for: rails', 'rails', :highlighter => '<a href="search?q=\1">\1</a>')
123 # # => You searched for: <a href="search?q=rails">rails</a>
124 #
125 # You can still use <tt>highlight</tt> with the old API that accepts the
126 # +highlighter+ as its optional third parameter:
127 # highlight('You searched for: rails', 'rails', '<a href="search?q=\1">\1</a>') # => You searched for: <a href="search?q=rails">rails</a>
128 def highlight(text, phrases, *args)
129 options = args.extract_options!
130 unless args.empty?
131 options[:highlighter] = args[0] || '<strong class="highlight">\1</strong>'
132 end
133 options.reverse_merge!(:highlighter => '<strong class="highlight">\1</strong>')
134
67d5a1a @jeremy Highlight helper highlights one or many terms in a single pass.
jeremy authored
135 if text.blank? || phrases.blank?
136 text
137 else
138 match = Array(phrases).map { |p| Regexp.escape(p) }.join('|')
10d9fe4 @clemens Refactored TextHelper#truncate, highlight, excerpt, word_wrap and auto_l...
clemens authored
139 text.gsub(/(#{match})/i, options[:highlighter])
67d5a1a @jeremy Highlight helper highlights one or many terms in a single pass.
jeremy authored
140 end
db045db @dhh Initial
dhh authored
141 end
dfac1ce @dhh Fixed that form helpers would treat string and symbol keys differently i...
dhh authored
142
607511c @jeremy Ruby 1.9 compat: truncate and excerpt helpers
jeremy authored
143 if RUBY_VERSION < '1.9'
603c853 @josh Don't fallback to just adding "'s" in TextHelper#pluralize, because the ...
josh authored
144 # Extracts an excerpt from +text+ that matches the first instance of +phrase+.
10d9fe4 @clemens Refactored TextHelper#truncate, highlight, excerpt, word_wrap and auto_l...
clemens authored
145 # The <tt>:radius</tt> option expands the excerpt on each side of the first occurrence of +phrase+ by the number of characters
146 # defined in <tt>:radius</tt> (which defaults to 100). If the excerpt radius overflows the beginning or end of the +text+,
147 # then the <tt>:omission</tt> option (which defaults to "...") will be prepended/appended accordingly. The resulting string
148 # will be stripped in any case. If the +phrase+ isn't found, nil is returned.
607511c @jeremy Ruby 1.9 compat: truncate and excerpt helpers
jeremy authored
149 #
150 # ==== Examples
10d9fe4 @clemens Refactored TextHelper#truncate, highlight, excerpt, word_wrap and auto_l...
clemens authored
151 # excerpt('This is an example', 'an', :radius => 5)
152 # # => ...s is an exam...
607511c @jeremy Ruby 1.9 compat: truncate and excerpt helpers
jeremy authored
153 #
10d9fe4 @clemens Refactored TextHelper#truncate, highlight, excerpt, word_wrap and auto_l...
clemens authored
154 # excerpt('This is an example', 'is', :radius => 5)
155 # # => This is a...
607511c @jeremy Ruby 1.9 compat: truncate and excerpt helpers
jeremy authored
156 #
603c853 @josh Don't fallback to just adding "'s" in TextHelper#pluralize, because the ...
josh authored
157 # excerpt('This is an example', 'is')
10d9fe4 @clemens Refactored TextHelper#truncate, highlight, excerpt, word_wrap and auto_l...
clemens authored
158 # # => This is an example
607511c @jeremy Ruby 1.9 compat: truncate and excerpt helpers
jeremy authored
159 #
10d9fe4 @clemens Refactored TextHelper#truncate, highlight, excerpt, word_wrap and auto_l...
clemens authored
160 # excerpt('This next thing is an example', 'ex', :radius => 2)
161 # # => ...next...
607511c @jeremy Ruby 1.9 compat: truncate and excerpt helpers
jeremy authored
162 #
10d9fe4 @clemens Refactored TextHelper#truncate, highlight, excerpt, word_wrap and auto_l...
clemens authored
163 # excerpt('This is also an example', 'an', :radius => 8, :omission => '<chop> ')
164 # # => <chop> is also an example
165 #
166 # You can still use <tt>excerpt</tt> with the old API that accepts the
167 # +radius+ as its optional third and the +ellipsis+ as its
168 # optional forth parameter:
169 # excerpt('This is an example', 'an', 5) # => ...s is an exam...
170 # excerpt('This is also an example', 'an', 8, '<chop> ') # => <chop> is also an example
171 def excerpt(text, phrase, *args)
172 options = args.extract_options!
173 unless args.empty?
174 options[:radius] = args[0] || 100
175 options[:omission] = args[1] || "..."
176 end
177 options.reverse_merge!(:radius => 100, :omission => "...")
178
607511c @jeremy Ruby 1.9 compat: truncate and excerpt helpers
jeremy authored
179 if text && phrase
180 phrase = Regexp.escape(phrase)
dfac1ce @dhh Fixed that form helpers would treat string and symbol keys differently i...
dhh authored
181
607511c @jeremy Ruby 1.9 compat: truncate and excerpt helpers
jeremy authored
182 if found_pos = text.chars =~ /(#{phrase})/i
10d9fe4 @clemens Refactored TextHelper#truncate, highlight, excerpt, word_wrap and auto_l...
clemens authored
183 start_pos = [ found_pos - options[:radius], 0 ].max
184 end_pos = [ [ found_pos + phrase.chars.length + options[:radius] - 1, 0].max, text.chars.length ].min
db045db @dhh Initial
dhh authored
185
10d9fe4 @clemens Refactored TextHelper#truncate, highlight, excerpt, word_wrap and auto_l...
clemens authored
186 prefix = start_pos > 0 ? options[:omission] : ""
187 postfix = end_pos < text.chars.length - 1 ? options[:omission] : ""
db045db @dhh Initial
dhh authored
188
607511c @jeremy Ruby 1.9 compat: truncate and excerpt helpers
jeremy authored
189 prefix + text.chars[start_pos..end_pos].strip + postfix
190 else
191 nil
192 end
193 end
194 end
195 else
10d9fe4 @clemens Refactored TextHelper#truncate, highlight, excerpt, word_wrap and auto_l...
clemens authored
196 def excerpt(text, phrase, *args) #:nodoc:
197 options = args.extract_options!
198 unless args.empty?
199 options[:radius] = args[0] || 100
200 options[:omission] = args[1] || "..."
201 end
202 options.reverse_merge!(:radius => 100, :omission => "...")
203
607511c @jeremy Ruby 1.9 compat: truncate and excerpt helpers
jeremy authored
204 if text && phrase
205 phrase = Regexp.escape(phrase)
206
207 if found_pos = text =~ /(#{phrase})/i
10d9fe4 @clemens Refactored TextHelper#truncate, highlight, excerpt, word_wrap and auto_l...
clemens authored
208 start_pos = [ found_pos - options[:radius], 0 ].max
209 end_pos = [ [ found_pos + phrase.length + options[:radius] - 1, 0].max, text.length ].min
607511c @jeremy Ruby 1.9 compat: truncate and excerpt helpers
jeremy authored
210
10d9fe4 @clemens Refactored TextHelper#truncate, highlight, excerpt, word_wrap and auto_l...
clemens authored
211 prefix = start_pos > 0 ? options[:omission] : ""
212 postfix = end_pos < text.length - 1 ? options[:omission] : ""
607511c @jeremy Ruby 1.9 compat: truncate and excerpt helpers
jeremy authored
213
214 prefix + text[start_pos..end_pos].strip + postfix
215 else
216 nil
217 end
218 end
db045db @dhh Initial
dhh authored
219 end
220 end
221
603c853 @josh Don't fallback to just adding "'s" in TextHelper#pluralize, because the ...
josh authored
222 # Attempts to pluralize the +singular+ word unless +count+ is 1. If
223 # +plural+ is supplied, it will use that when count is > 1, otherwise
224 # it will use the Inflector to determine the plural form
b34ed9b @dhh Docfix (closes #6393)
dhh authored
225 #
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
226 # ==== Examples
603c853 @josh Don't fallback to just adding "'s" in TextHelper#pluralize, because the ...
josh authored
227 # pluralize(1, 'person')
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
228 # # => 1 person
229 #
603c853 @josh Don't fallback to just adding "'s" in TextHelper#pluralize, because the ...
josh authored
230 # pluralize(2, 'person')
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
231 # # => 2 people
232 #
603c853 @josh Don't fallback to just adding "'s" in TextHelper#pluralize, because the ...
josh authored
233 # pluralize(3, 'person', 'users')
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
234 # # => 3 users
235 #
236 # pluralize(0, 'person')
237 # # => 0 people
db045db @dhh Initial
dhh authored
238 def pluralize(count, singular, plural = nil)
603c853 @josh Don't fallback to just adding "'s" in TextHelper#pluralize, because the ...
josh authored
239 "#{count || 0} " + ((count == 1 || count == '1') ? singular : (plural || singular.pluralize))
db045db @dhh Initial
dhh authored
240 end
241
b34ed9b @dhh Docfix (closes #6393)
dhh authored
242 # Wraps the +text+ into lines no longer than +line_width+ width. This method
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
243 # breaks on the first whitespace character that does not exceed +line_width+
244 # (which is 80 by default).
b34ed9b @dhh Docfix (closes #6393)
dhh authored
245 #
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
246 # ==== Examples
247 #
248 # word_wrap('Once upon a time')
249 # # => Once upon a time
250 #
10d9fe4 @clemens Refactored TextHelper#truncate, highlight, excerpt, word_wrap and auto_l...
clemens authored
251 # word_wrap('Once upon a time, in a kingdom called Far Far Away, a king fell ill, and finding a successor to the throne turned out to be more trouble than anyone could have imagined...')
252 # # => Once upon a time, in a kingdom called Far Far Away, a king fell ill, and finding\n a successor to the throne turned out to be more trouble than anyone could have\n imagined...
253 #
254 # word_wrap('Once upon a time', :line_width => 8)
255 # # => Once upon\na time
256 #
257 # word_wrap('Once upon a time', :line_width => 1)
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
258 # # => Once\nupon\na\ntime
10d9fe4 @clemens Refactored TextHelper#truncate, highlight, excerpt, word_wrap and auto_l...
clemens authored
259 #
260 # You can still use <tt>word_wrap</tt> with the old API that accepts the
261 # +line_width+ as its optional second parameter:
262 # word_wrap('Once upon a time', 8) # => Once upon\na time
263 def word_wrap(text, *args)
264 options = args.extract_options!
265 unless args.blank?
266 options[:line_width] = args[0] || 80
267 end
268 options.reverse_merge!(:line_width => 80)
269
9686dcd @dhh Fixed TextHelper#word_wrap for multiline strings with extra carrier retu...
dhh authored
270 text.split("\n").collect do |line|
10d9fe4 @clemens Refactored TextHelper#truncate, highlight, excerpt, word_wrap and auto_l...
clemens authored
271 line.length > options[:line_width] ? line.gsub(/(.{1,#{options[:line_width]}})(\s+|$)/, "\\1\n").strip : line
9686dcd @dhh Fixed TextHelper#word_wrap for multiline strings with extra carrier retu...
dhh authored
272 end * "\n"
c608ebe @dhh Added TextHelper#word_wrap(text, line_length = 80) #1449 [tuxie@dekadanc...
dhh authored
273 end
274
db045db @dhh Initial
dhh authored
275 begin
a9e02fd @technoweenie only require redcloth/bluecloth if they are not already loaded [Rick]
technoweenie authored
276 require_library_or_gem "redcloth" unless Object.const_defined?(:RedCloth)
db045db @dhh Initial
dhh authored
277
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
278 # Returns the text with all the Textile[http://www.textism.com/tools/textile] codes turned into HTML tags.
279 #
280 # You can learn more about Textile's syntax at its website[http://www.textism.com/tools/textile].
b34ed9b @dhh Docfix (closes #6393)
dhh authored
281 # <i>This method is only available if RedCloth[http://whytheluckystiff.net/ruby/redcloth/]
282 # is available</i>.
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
283 #
284 # ==== Examples
285 # textilize("*This is Textile!* Rejoice!")
286 # # => "<p><strong>This is Textile!</strong> Rejoice!</p>"
287 #
288 # textilize("I _love_ ROR(Ruby on Rails)!")
289 # # => "<p>I <em>love</em> <acronym title="Ruby on Rails">ROR</acronym>!</p>"
290 #
291 # textilize("h2. Textile makes markup -easy- simple!")
292 # # => "<h2>Textile makes markup <del>easy</del> simple!</h2>"
293 #
294 # textilize("Visit the Rails website "here":http://www.rubyonrails.org/.)
295 # # => "<p>Visit the Rails website <a href="http://www.rubyonrails.org/">here</a>.</p>"
db045db @dhh Initial
dhh authored
296 def textilize(text)
810ebf7 @dhh Forced newer versions of RedCloth to use hard breaks [DHH]
dhh authored
297 if text.blank?
298 ""
299 else
300 textilized = RedCloth.new(text, [ :hard_breaks ])
a978701 @clemens More symbols for send and respond_to?.
clemens authored
301 textilized.hard_breaks = true if textilized.respond_to?(:hard_breaks=)
810ebf7 @dhh Forced newer versions of RedCloth to use hard breaks [DHH]
dhh authored
302 textilized.to_html
f13534e @dhh Forced newer versions of RedCloth to use hard breaks [DHH]
dhh authored
303 end
db045db @dhh Initial
dhh authored
304 end
305
603c853 @josh Don't fallback to just adding "'s" in TextHelper#pluralize, because the ...
josh authored
306 # Returns the text with all the Textile codes turned into HTML tags,
b34ed9b @dhh Docfix (closes #6393)
dhh authored
307 # but without the bounding <p> tag that RedCloth adds.
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
308 #
309 # You can learn more about Textile's syntax at its website[http://www.textism.com/tools/textile].
b34ed9b @dhh Docfix (closes #6393)
dhh authored
310 # <i>This method is only available if RedCloth[http://whytheluckystiff.net/ruby/redcloth/]
311 # is available</i>.
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
312 #
313 # ==== Examples
314 # textilize_without_paragraph("*This is Textile!* Rejoice!")
315 # # => "<strong>This is Textile!</strong> Rejoice!"
316 #
317 # textilize_without_paragraph("I _love_ ROR(Ruby on Rails)!")
318 # # => "I <em>love</em> <acronym title="Ruby on Rails">ROR</acronym>!"
319 #
320 # textilize_without_paragraph("h2. Textile makes markup -easy- simple!")
321 # # => "<h2>Textile makes markup <del>easy</del> simple!</h2>"
322 #
323 # textilize_without_paragraph("Visit the Rails website "here":http://www.rubyonrails.org/.)
324 # # => "Visit the Rails website <a href="http://www.rubyonrails.org/">here</a>."
db045db @dhh Initial
dhh authored
325 def textilize_without_paragraph(text)
326 textiled = textilize(text)
327 if textiled[0..2] == "<p>" then textiled = textiled[3..-1] end
328 if textiled[-4..-1] == "</p>" then textiled = textiled[0..-5] end
329 return textiled
330 end
331 rescue LoadError
332 # We can't really help what's not there
333 end
334
335 begin
a9e02fd @technoweenie only require redcloth/bluecloth if they are not already loaded [Rick]
technoweenie authored
336 require_library_or_gem "bluecloth" unless Object.const_defined?(:BlueCloth)
db045db @dhh Initial
dhh authored
337
b34ed9b @dhh Docfix (closes #6393)
dhh authored
338 # Returns the text with all the Markdown codes turned into HTML tags.
339 # <i>This method is only available if BlueCloth[http://www.deveiate.org/projects/BlueCloth]
340 # is available</i>.
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
341 #
342 # ==== Examples
343 # markdown("We are using __Markdown__ now!")
344 # # => "<p>We are using <strong>Markdown</strong> now!</p>"
345 #
346 # markdown("We like to _write_ `code`, not just _read_ it!")
347 # # => "<p>We like to <em>write</em> <code>code</code>, not just <em>read</em> it!</p>"
348 #
349 # markdown("The [Markdown website](http://daringfireball.net/projects/markdown/) has more information.")
603c853 @josh Don't fallback to just adding "'s" in TextHelper#pluralize, because the ...
josh authored
350 # # => "<p>The <a href="http://daringfireball.net/projects/markdown/">Markdown website</a>
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
351 # # has more information.</p>"
352 #
353 # markdown('![The ROR logo](http://rubyonrails.com/images/rails.png "Ruby on Rails")')
603c853 @josh Don't fallback to just adding "'s" in TextHelper#pluralize, because the ...
josh authored
354 # # => '<p><img src="http://rubyonrails.com/images/rails.png" alt="The ROR logo" title="Ruby on Rails" /></p>'
db045db @dhh Initial
dhh authored
355 def markdown(text)
1b93da3 @dhh Added TextHelper#simple_format as a non-dependency text presentation hel...
dhh authored
356 text.blank? ? "" : BlueCloth.new(text).to_html
db045db @dhh Initial
dhh authored
357 end
358 rescue LoadError
359 # We can't really help what's not there
360 end
603c853 @josh Don't fallback to just adding "'s" in TextHelper#pluralize, because the ...
josh authored
361
b34ed9b @dhh Docfix (closes #6393)
dhh authored
362 # Returns +text+ transformed into HTML using simple formatting rules.
603c853 @josh Don't fallback to just adding "'s" in TextHelper#pluralize, because the ...
josh authored
363 # Two or more consecutive newlines(<tt>\n\n</tt>) are considered as a
b34ed9b @dhh Docfix (closes #6393)
dhh authored
364 # paragraph and wrapped in <tt><p></tt> tags. One newline (<tt>\n</tt>) is
365 # considered as a linebreak and a <tt><br /></tt> tag is appended. This
603c853 @josh Don't fallback to just adding "'s" in TextHelper#pluralize, because the ...
josh authored
366 # method does not remove the newlines from the +text+.
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
367 #
603c853 @josh Don't fallback to just adding "'s" in TextHelper#pluralize, because the ...
josh authored
368 # You can pass any HTML attributes into <tt>html_options</tt>. These
5bf4cbb @technoweenie Allow the #simple_format text_helper to take an html_options hash for ea...
technoweenie authored
369 # will be added to all created paragraphs.
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
370 # ==== Examples
1b41178 @dhh Docfix (closes #11230) [Henrik N]
dhh authored
371 # my_text = "Here is some basic text...\n...with a line break."
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
372 #
373 # simple_format(my_text)
1b41178 @dhh Docfix (closes #11230) [Henrik N]
dhh authored
374 # # => "<p>Here is some basic text...\n<br />...with a line break.</p>"
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
375 #
1b41178 @dhh Docfix (closes #11230) [Henrik N]
dhh authored
376 # more_text = "We want to put a paragraph...\n\n...right there."
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
377 #
378 # simple_format(more_text)
1b41178 @dhh Docfix (closes #11230) [Henrik N]
dhh authored
379 # # => "<p>We want to put a paragraph...</p>\n\n<p>...right there.</p>"
5bf4cbb @technoweenie Allow the #simple_format text_helper to take an html_options hash for ea...
technoweenie authored
380 #
381 # simple_format("Look ma! A class!", :class => 'description')
382 # # => "<p class='description'>Look ma! A class!</p>"
383 def simple_format(text, html_options={})
384 start_tag = tag('p', html_options, true)
385 text = text.to_s.dup
386 text.gsub!(/\r\n?/, "\n") # \r\n and \r -> \n
387 text.gsub!(/\n\n+/, "</p>\n\n#{start_tag}") # 2+ newline -> paragraph
388 text.gsub!(/([^\n]\n)(?=[^\n])/, '\1<br />') # 1 newline -> br
389 text.insert 0, start_tag
390 text << "</p>"
1b93da3 @dhh Added TextHelper#simple_format as a non-dependency text presentation hel...
dhh authored
391 end
db045db @dhh Initial
dhh authored
392
603c853 @josh Don't fallback to just adding "'s" in TextHelper#pluralize, because the ...
josh authored
393 # Turns all URLs and e-mail addresses into clickable links. The +link+ parameter
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
394 # will limit what should be linked. You can add HTML attributes to the links using
603c853 @josh Don't fallback to just adding "'s" in TextHelper#pluralize, because the ...
josh authored
395 # +href_options+. Options for +link+ are <tt>:all</tt> (default),
396 # <tt>:email_addresses</tt>, and <tt>:urls</tt>. If a block is given, each URL and
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
397 # e-mail address is yielded and the result is used as the link text.
398 #
399 # ==== Examples
603c853 @josh Don't fallback to just adding "'s" in TextHelper#pluralize, because the ...
josh authored
400 # auto_link("Go to http://www.rubyonrails.org and say hello to david@loudthinking.com")
c92a7ce @dhh Docfix (closes #10493)
dhh authored
401 # # => "Go to <a href=\"http://www.rubyonrails.org\">http://www.rubyonrails.org</a> and
402 # # say hello to <a href=\"mailto:david@loudthinking.com\">david@loudthinking.com</a>"
0369ec7 @dhh Made auto_link the only public method of its clan and added an option to...
dhh authored
403 #
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
404 # auto_link("Visit http://www.loudthinking.com/ or e-mail david@loudthinking.com", :urls)
603c853 @josh Don't fallback to just adding "'s" in TextHelper#pluralize, because the ...
josh authored
405 # # => "Visit <a href=\"http://www.loudthinking.com/\">http://www.loudthinking.com/</a>
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
406 # # or e-mail david@loudthinking.com"
adba181 @jeremy The auto_link text helper accepts an optional block to format the link t...
jeremy authored
407 #
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
408 # auto_link("Visit http://www.loudthinking.com/ or e-mail david@loudthinking.com", :email_addresses)
409 # # => "Visit http://www.loudthinking.com/ or e-mail <a href=\"mailto:david@loudthinking.com\">david@loudthinking.com</a>"
b34ed9b @dhh Docfix (closes #6393)
dhh authored
410 #
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
411 # post_body = "Welcome to my new blog at http://www.myblog.com/. Please e-mail me at me@email.com."
412 # auto_link(post_body, :all, :target => '_blank') do |text|
adba181 @jeremy The auto_link text helper accepts an optional block to format the link t...
jeremy authored
413 # truncate(text, 15)
414 # end
603c853 @josh Don't fallback to just adding "'s" in TextHelper#pluralize, because the ...
josh authored
415 # # => "Welcome to my new blog at <a href=\"http://www.myblog.com/\" target=\"_blank\">http://www.m...</a>.
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
416 # Please e-mail me at <a href=\"mailto:me@email.com\">me@email.com</a>."
603c853 @josh Don't fallback to just adding "'s" in TextHelper#pluralize, because the ...
josh authored
417 #
10d9fe4 @clemens Refactored TextHelper#truncate, highlight, excerpt, word_wrap and auto_l...
clemens authored
418 #
419 # You can still use <tt>auto_link</tt> with the old API that accepts the
420 # +link+ as its optional second parameter and the +html_options+ hash
421 # as its optional third parameter:
422 # post_body = "Welcome to my new blog at http://www.myblog.com/. Please e-mail me at me@email.com."
423 # auto_link(post_body, :urls) # => Once upon\na time
424 # # => "Welcome to my new blog at <a href=\"http://www.myblog.com/\">http://www.myblog.com</a>.
425 # Please e-mail me at me@email.com."
426 #
427 # auto_link(post_body, :all, :target => "_blank") # => Once upon\na time
428 # # => "Welcome to my new blog at <a href=\"http://www.myblog.com/\" target=\"_blank\">http://www.myblog.com</a>.
429 # Please e-mail me at <a href=\"mailto:me@email.com\">me@email.com</a>."
430 def auto_link(text, *args, &block)#link = :all, href_options = {}, &block)
63c822a Make auto_link handle nil by returning quickly if blank?
Scott Barron authored
431 return '' if text.blank?
10d9fe4 @clemens Refactored TextHelper#truncate, highlight, excerpt, word_wrap and auto_l...
clemens authored
432
433 options = args.size == 2 ? {} : args.extract_options! # this is necessary because the old auto_link API has a Hash as its last parameter
434 unless args.empty?
435 options[:link] = args[0] || :all
436 options[:html] = args[1] || {}
437 end
438 options.reverse_merge!(:link => :all, :html => {})
439
440 case options[:link].to_sym
441 when :all then auto_link_email_addresses(auto_link_urls(text, options[:html], &block), &block)
442 when :email_addresses then auto_link_email_addresses(text, &block)
443 when :urls then auto_link_urls(text, options[:html], &block)
0369ec7 @dhh Made auto_link the only public method of its clan and added an option to...
dhh authored
444 end
b305756 @dhh Added TextHelper#auto_link, TextHelper#auto_link_urls, and TextHelper#au...
dhh authored
445 end
603c853 @josh Don't fallback to just adding "'s" in TextHelper#pluralize, because the ...
josh authored
446
b34ed9b @dhh Docfix (closes #6393)
dhh authored
447 # Creates a Cycle object whose _to_s_ method cycles through elements of an
603c853 @josh Don't fallback to just adding "'s" in TextHelper#pluralize, because the ...
josh authored
448 # array every time it is called. This can be used for example, to alternate
449 # classes for table rows. You can use named cycles to allow nesting in loops.
450 # Passing a Hash as the last parameter with a <tt>:name</tt> key will create a
f277e1d @metaskills Added TextHelper#current_cycle to return the current cycle for better de...
metaskills authored
451 # named cycle. The default name for a cycle without a +:name+ key is
452 # <tt>"default"</tt>. You can manually reset a cycle by calling reset_cycle
453 # and passing the name of the cycle. The current cycle string can be obtained
454 # anytime using the current_cycle method.
2fe8610 @dhh Added TextHelper#cycle to cycle over an array of values on each hit (use...
dhh authored
455 #
603c853 @josh Don't fallback to just adding "'s" in TextHelper#pluralize, because the ...
josh authored
456 # ==== Examples
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
457 # # Alternate CSS classes for even and odd numbers...
458 # @items = [1,2,3,4]
459 # <table>
b34ed9b @dhh Docfix (closes #6393)
dhh authored
460 # <% @items.each do |item| %>
461 # <tr class="<%= cycle("even", "odd") -%>">
462 # <td>item</td>
4d007ce Fix text_helper.rb documentation rendering. Closes #4725. [Frederick Ros...
Marcel Molina authored
463 # </tr>
b34ed9b @dhh Docfix (closes #6393)
dhh authored
464 # <% end %>
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
465 # </table>
2fe8610 @dhh Added TextHelper#cycle to cycle over an array of values on each hit (use...
dhh authored
466 #
467 #
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
468 # # Cycle CSS classes for rows, and text colors for values within each row
603c853 @josh Don't fallback to just adding "'s" in TextHelper#pluralize, because the ...
josh authored
469 # @items = x = [{:first => 'Robert', :middle => 'Daniel', :last => 'James'},
470 # {:first => 'Emily', :middle => 'Shannon', :maiden => 'Pike', :last => 'Hicks'},
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
471 # {:first => 'June', :middle => 'Dae', :last => 'Jones'}]
b34ed9b @dhh Docfix (closes #6393)
dhh authored
472 # <% @items.each do |item| %>
458ef3b Fix syntax error in documentation example for cycle method. Closes #8735...
Marcel Molina authored
473 # <tr class="<%= cycle("even", "odd", :name => "row_class") -%>">
4d007ce Fix text_helper.rb documentation rendering. Closes #4725. [Frederick Ros...
Marcel Molina authored
474 # <td>
b34ed9b @dhh Docfix (closes #6393)
dhh authored
475 # <% item.values.each do |value| %>
e71465b @dhh Avoid RDoc warning
dhh authored
476 # <%# Create a named cycle "colors" %>
b34ed9b @dhh Docfix (closes #6393)
dhh authored
477 # <span style="color:<%= cycle("red", "green", "blue", :name => "colors") -%>">
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
478 # <%= value %>
4d007ce Fix text_helper.rb documentation rendering. Closes #4725. [Frederick Ros...
Marcel Molina authored
479 # </span>
b34ed9b @dhh Docfix (closes #6393)
dhh authored
480 # <% end %>
481 # <% reset_cycle("colors") %>
4d007ce Fix text_helper.rb documentation rendering. Closes #4725. [Frederick Ros...
Marcel Molina authored
482 # </td>
483 # </tr>
b34ed9b @dhh Docfix (closes #6393)
dhh authored
484 # <% end %>
2fe8610 @dhh Added TextHelper#cycle to cycle over an array of values on each hit (use...
dhh authored
485 def cycle(first_value, *values)
486 if (values.last.instance_of? Hash)
487 params = values.pop
488 name = params[:name]
489 else
490 name = "default"
491 end
492 values.unshift(first_value)
493
494 cycle = get_cycle(name)
495 if (cycle.nil? || cycle.values != values)
496 cycle = set_cycle(name, Cycle.new(*values))
497 end
498 return cycle.to_s
499 end
603c853 @josh Don't fallback to just adding "'s" in TextHelper#pluralize, because the ...
josh authored
500
f277e1d @metaskills Added TextHelper#current_cycle to return the current cycle for better de...
metaskills authored
501 # Returns the current cycle string after a cycle has been started. Useful
502 # for complex table highlighing or any other design need which requires
503 # the current cycle string in more than one place.
504 #
505 # ==== Example
506 # # Alternate background colors
507 # @items = [1,2,3,4]
508 # <% @items.each do |item| %>
509 # <div style="background-color:<%= cycle("red","white","blue") %>">
6ef3546 @lifo Merge docrails
lifo authored
510 # <span style="background-color:<%= current_cycle %>"><%= item %></span>
f277e1d @metaskills Added TextHelper#current_cycle to return the current cycle for better de...
metaskills authored
511 # </div>
512 # <% end %>
513 def current_cycle(name = "default")
514 cycle = get_cycle(name)
515 cycle.current_value unless cycle.nil?
516 end
517
603c853 @josh Don't fallback to just adding "'s" in TextHelper#pluralize, because the ...
josh authored
518 # Resets a cycle so that it starts from the first element the next time
b34ed9b @dhh Docfix (closes #6393)
dhh authored
519 # it is called. Pass in +name+ to reset a named cycle.
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
520 #
521 # ==== Example
522 # # Alternate CSS classes for even and odd numbers...
523 # @items = [[1,2,3,4], [5,6,3], [3,4,5,6,7,4]]
524 # <table>
525 # <% @items.each do |item| %>
526 # <tr class="<%= cycle("even", "odd") -%>">
527 # <% item.each do |value| %>
528 # <span style="color:<%= cycle("#333", "#666", "#999", :name => "colors") -%>">
529 # <%= value %>
530 # </span>
531 # <% end %>
532 #
533 # <% reset_cycle("colors") %>
534 # </tr>
535 # <% end %>
536 # </table>
2fe8610 @dhh Added TextHelper#cycle to cycle over an array of values on each hit (use...
dhh authored
537 def reset_cycle(name = "default")
538 cycle = get_cycle(name)
71234da @jeremy r4487@asus: jeremy | 2006-04-29 12:21:39 -0700
jeremy authored
539 cycle.reset unless cycle.nil?
2fe8610 @dhh Added TextHelper#cycle to cycle over an array of values on each hit (use...
dhh authored
540 end
541
1c057b7 @jamis Update/clean up AP documentation (rdoc)
jamis authored
542 class Cycle #:nodoc:
2fe8610 @dhh Added TextHelper#cycle to cycle over an array of values on each hit (use...
dhh authored
543 attr_reader :values
603c853 @josh Don't fallback to just adding "'s" in TextHelper#pluralize, because the ...
josh authored
544
2fe8610 @dhh Added TextHelper#cycle to cycle over an array of values on each hit (use...
dhh authored
545 def initialize(first_value, *values)
546 @values = values.unshift(first_value)
547 reset
548 end
603c853 @josh Don't fallback to just adding "'s" in TextHelper#pluralize, because the ...
josh authored
549
2fe8610 @dhh Added TextHelper#cycle to cycle over an array of values on each hit (use...
dhh authored
550 def reset
551 @index = 0
552 end
553
f277e1d @metaskills Added TextHelper#current_cycle to return the current cycle for better de...
metaskills authored
554 def current_value
555 @values[previous_index].to_s
556 end
557
2fe8610 @dhh Added TextHelper#cycle to cycle over an array of values on each hit (use...
dhh authored
558 def to_s
559 value = @values[@index].to_s
f277e1d @metaskills Added TextHelper#current_cycle to return the current cycle for better de...
metaskills authored
560 @index = next_index
2fe8610 @dhh Added TextHelper#cycle to cycle over an array of values on each hit (use...
dhh authored
561 return value
562 end
f277e1d @metaskills Added TextHelper#current_cycle to return the current cycle for better de...
metaskills authored
563
564 private
565
566 def next_index
567 step_index(1)
568 end
569
570 def previous_index
571 step_index(-1)
572 end
573
574 def step_index(n)
575 (@index + n) % @values.size
576 end
2fe8610 @dhh Added TextHelper#cycle to cycle over an array of values on each hit (use...
dhh authored
577 end
da0725a @technoweenie move TextHelper#sanitize config options to the TextHelper module so it c...
technoweenie authored
578
db045db @dhh Initial
dhh authored
579 private
2fe8610 @dhh Added TextHelper#cycle to cycle over an array of values on each hit (use...
dhh authored
580 # The cycle helpers need to store the cycles in a place that is
581 # guaranteed to be reset every time a page is rendered, so it
582 # uses an instance variable of ActionView::Base.
583 def get_cycle(name)
71234da @jeremy r4487@asus: jeremy | 2006-04-29 12:21:39 -0700
jeremy authored
584 @_cycles = Hash.new unless defined?(@_cycles)
2fe8610 @dhh Added TextHelper#cycle to cycle over an array of values on each hit (use...
dhh authored
585 return @_cycles[name]
586 end
603c853 @josh Don't fallback to just adding "'s" in TextHelper#pluralize, because the ...
josh authored
587
2fe8610 @dhh Added TextHelper#cycle to cycle over an array of values on each hit (use...
dhh authored
588 def set_cycle(name, cycle_object)
71234da @jeremy r4487@asus: jeremy | 2006-04-29 12:21:39 -0700
jeremy authored
589 @_cycles = Hash.new unless defined?(@_cycles)
2fe8610 @dhh Added TextHelper#cycle to cycle over an array of values on each hit (use...
dhh authored
590 @_cycles[name] = cycle_object
591 end
8e82e29 @madrobby Make TextHelper::auto_link recognize URLs with colons in path correctly,...
madrobby authored
592
bdf91d6 @jamis Make auto_link parse a greater subset of valid url formats.
jamis authored
593 AUTO_LINK_RE = %r{
8e82e29 @madrobby Make TextHelper::auto_link recognize URLs with colons in path correctly,...
madrobby authored
594 ( # leading text
595 <\w+.*?>| # leading HTML tag, or
603c853 @josh Don't fallback to just adding "'s" in TextHelper#pluralize, because the ...
josh authored
596 [^=!:'"/]| # leading punctuation, or
1a73e98 @kjg Fix auto_link helper for already linked urls. [#72 state:resolved]
kjg authored
597 ^ # beginning of line
b97f4e4 @jamis Fix autolinking to not include trailing tags as part of the URL
jamis authored
598 )
599 (
8e82e29 @madrobby Make TextHelper::auto_link recognize URLs with colons in path correctly,...
madrobby authored
600 (?:https?://)| # protocol spec, or
601 (?:www\.) # www.*
603c853 @josh Don't fallback to just adding "'s" in TextHelper#pluralize, because the ...
josh authored
602 )
b97f4e4 @jamis Fix autolinking to not include trailing tags as part of the URL
jamis authored
603 (
8e82e29 @madrobby Make TextHelper::auto_link recognize URLs with colons in path correctly,...
madrobby authored
604 [-\w]+ # subdomain or domain
605 (?:\.[-\w]+)* # remaining subdomains or domain
606 (?::\d+)? # port
b263050 Fixed autolink regexp compatibility for ruby 1.9 [#783 state:resolved]
Stefan Kaes authored
607 (?:/(?:[~\w\+@%=\(\)-]|(?:[,.;:'][^\s$]))*)* # path
09e76e6 @jeremy Autolink behaves well with emails embedded in URLs. Closes #7313.
jeremy authored
608 (?:\?[\w\+@%&=.;-]+)? # query string
8e82e29 @madrobby Make TextHelper::auto_link recognize URLs with colons in path correctly,...
madrobby authored
609 (?:\#[\w\-]*)? # trailing anchor
b97f4e4 @jamis Fix autolinking to not include trailing tags as part of the URL
jamis authored
610 )
1a73e98 @kjg Fix auto_link helper for already linked urls. [#72 state:resolved]
kjg authored
611 ([[:punct:]]|<|$|) # trailing text
bdf91d6 @jamis Make auto_link parse a greater subset of valid url formats.
jamis authored
612 }x unless const_defined?(:AUTO_LINK_RE)
b97f4e4 @jamis Fix autolinking to not include trailing tags as part of the URL
jamis authored
613
adba181 @jeremy The auto_link text helper accepts an optional block to format the link t...
jeremy authored
614 # Turns all urls into clickable links. If a block is given, each url
b34ed9b @dhh Docfix (closes #6393)
dhh authored
615 # is yielded and the result is used as the link text.
10d9fe4 @clemens Refactored TextHelper#truncate, highlight, excerpt, word_wrap and auto_l...
clemens authored
616 def auto_link_urls(text, html_options = {})
617 extra_options = tag_options(html_options.stringify_keys) || ""
b97f4e4 @jamis Fix autolinking to not include trailing tags as part of the URL
jamis authored
618 text.gsub(AUTO_LINK_RE) do
bdf91d6 @jamis Make auto_link parse a greater subset of valid url formats.
jamis authored
619 all, a, b, c, d = $&, $1, $2, $3, $4
480150e @dhh Fixed autolinking to work better in more cases #1013 [Jamis Buck]
dhh authored
620 if a =~ /<a\s/i # don't replace URL's that are already linked
621 all
622 else
adba181 @jeremy The auto_link text helper accepts an optional block to format the link t...
jeremy authored
623 text = b + c
624 text = yield(text) if block_given?
625 %(#{a}<a href="#{b=="www."?"http://www.":b}#{c}"#{extra_options}>#{text}</a>#{d})
480150e @dhh Fixed autolinking to work better in more cases #1013 [Jamis Buck]
dhh authored
626 end
627 end
0369ec7 @dhh Made auto_link the only public method of its clan and added an option to...
dhh authored
628 end
629
adba181 @jeremy The auto_link text helper accepts an optional block to format the link t...
jeremy authored
630 # Turns all email addresses into clickable links. If a block is given,
631 # each email is yielded and the result is used as the link text.
0369ec7 @dhh Made auto_link the only public method of its clan and added an option to...
dhh authored
632 def auto_link_email_addresses(text)
09e76e6 @jeremy Autolink behaves well with emails embedded in URLs. Closes #7313.
jeremy authored
633 body = text.dup
adba181 @jeremy The auto_link text helper accepts an optional block to format the link t...
jeremy authored
634 text.gsub(/([\w\.!#\$%\-+.]+@[A-Za-z0-9\-]+(\.[A-Za-z0-9\-]+)+)/) do
635 text = $1
603c853 @josh Don't fallback to just adding "'s" in TextHelper#pluralize, because the ...
josh authored
636
09e76e6 @jeremy Autolink behaves well with emails embedded in URLs. Closes #7313.
jeremy authored
637 if body.match(/<a\b[^>]*>(.*)(#{Regexp.escape(text)})(.*)<\/a>/)
638 text
639 else
640 display_text = (block_given?) ? yield(text) : text
641 %{<a href="mailto:#{text}">#{display_text}</a>}
642 end
adba181 @jeremy The auto_link text helper accepts an optional block to format the link t...
jeremy authored
643 end
0369ec7 @dhh Made auto_link the only public method of its clan and added an option to...
dhh authored
644 end
db045db @dhh Initial
dhh authored
645 end
646 end
10d9fe4 @clemens Refactored TextHelper#truncate, highlight, excerpt, word_wrap and auto_l...
clemens authored
647 end
Something went wrong with that request. Please try again.