0
@@ -34,40 +34,69 @@ module ActionView
0
if RUBY_VERSION < '1.9'
0
- # If +text+ is longer than +length+, +text+ will be truncated to the length of
0
- # +length+ (defaults to 30) and the last characters will be replaced with the +truncate_string+
0
- # (defaults to "...").
0
+ # Truncates a given +text+ after a given <tt>:length</tt> if +text+ is longer than <tt>:length</tt>
0
+ # (defaults to 30). The last characters will be replaced with the <tt>:omission</tt> (defaults to "...").
0
- # truncate("Once upon a time in a world far far away", 14)
0
# truncate("Once upon a time in a world far far away")
0
# # => Once upon a time in a world f...
0
- # truncate("And they found that many people were sleeping better.", 25, "(clipped)")
0
+ # truncate("Once upon a time in a world far far away", :length => 14)
0
+ # truncate("And they found that many people were sleeping better.", :length => 25, "(clipped)")
0
# # => And they found that many (clipped)
0
+ # truncate("And they found that many people were sleeping better.", :omission => "... (continued)", :length => 15)
0
+ # # => And they found... (continued)
0
+ # You can still use <tt>truncate</tt> with the old API that accepts the
0
+ # +length+ as its optional second and the +ellipsis+ as its
0
+ # optional third parameter:
0
+ # truncate("Once upon a time in a world far far away", 14)
0
+ # # => Once upon a time in a world f...
0
# truncate("And they found that many people were sleeping better.", 15, "... (continued)")
0
# # => And they found... (continued)
0
- def truncate(text, length = 30, truncate_string = "...")
0
+ def truncate(text, *args)
0
+ options = args.extract_options!
0
+ ActiveSupport::Deprecation.warn('truncate takes an option hash instead of separate ' +
0
+ 'length and omission arguments', caller)
0
+ options[:length] = args[0] || 30
0
+ options[:omission] = args[1] || "..."
0
+ options.reverse_merge!(:length => 30, :omission => "...")
0
- l =
length - truncate_string.chars.length
0
+ l =
options[:length] - options[:omission].chars.length
0
- (chars.length >
length ? chars[0...l] + truncate_string : text).to_s
0
+ (chars.length >
options[:length] ? chars[0...l] + options[:omission] : text).to_s
0
- def truncate(text, length = 30, truncate_string = "...") #:nodoc:
0
+ def truncate(text, *args) #:nodoc:
0
+ options = args.extract_options!
0
+ ActiveSupport::Deprecation.warn('truncate takes an option hash instead of separate ' +
0
+ 'length and omission arguments', caller)
0
+ options[:length] = args[0] || 30
0
+ options[:omission] = args[1] || "..."
0
+ options.reverse_merge!(:length => 30, :omission => "...")
0
- l = length - truncate_string.length
0
- (text.length > length ? text[0...l] + truncate_string : text).to_s
0
+ l = options[:length].to_i - options[:omission].length
0
+ (text.length > options[:length].to_i ? text[0...l] + options[:omission] : text).to_s
0
# Highlights one or more +phrases+ everywhere in +text+ by inserting it into
0
- # a
+highlighter+ string. The highlighter can be specialized by passing +highlighter+0
+ # a
<tt>:highlighter</tt> string. The highlighter can be specialized by passing <tt>:highlighter</tt>0
# as a single-quoted string with \1 where the phrase is to be inserted (defaults to
0
# '<strong class="highlight">\1</strong>')
0
@@ -78,52 +107,75 @@ module ActionView
0
# highlight('You searched for: ruby, rails, dhh', 'actionpack')
0
# # => You searched for: ruby, rails, dhh
0
- # highlight('You searched for: rails', ['for', 'rails'],
'<em>\1</em>')
0
+ # highlight('You searched for: rails', ['for', 'rails'],
:highlighter => '<em>\1</em>')
0
# # => You searched <em>for</em>: <em>rails</em>
0
- # highlight('You searched for: rails', 'rails', "<a href='search?q=\1'>\1</a>")
0
- # # => You searched for: <a href='search?q=rails>rails</a>
0
- def highlight(text, phrases, highlighter = '<strong class="highlight">\1</strong>')
0
+ # highlight('You searched for: rails', 'rails', :highlighter => '<a href="search?q=\1">\1</a>')
0
+ # # => You searched for: <a href="search?q=rails">rails</a>
0
+ # You can still use <tt>highlight</tt> with the old API that accepts the
0
+ # +highlighter+ as its optional third parameter:
0
+ # highlight('You searched for: rails', 'rails', '<a href="search?q=\1">\1</a>') # => You searched for: <a href="search?q=rails">rails</a>
0
+ def highlight(text, phrases, *args)
0
+ options = args.extract_options!
0
+ options[:highlighter] = args[0] || '<strong class="highlight">\1</strong>'
0
+ options.reverse_merge!(:highlighter => '<strong class="highlight">\1</strong>')
0
if text.blank? || phrases.blank?
0
match = Array(phrases).map { |p| Regexp.escape(p) }.join('|')
0
- text.gsub(/(#{match})/i,
highlighter)
0
+ text.gsub(/(#{match})/i,
options[:highlighter])
0
if RUBY_VERSION < '1.9'
0
# Extracts an excerpt from +text+ that matches the first instance of +phrase+.
0
- # The +radius+ expands the excerpt on each side of the first occurrence of +phrase+ by the number of characters
0
- # defined in +radius+ (which defaults to 100). If the excerpt radius overflows the beginning or end of the +text+,
0
- # then the +excerpt_string+ will be prepended/appended accordingly. The resulting string will be stripped in any case.
0
- # If the +phrase+ isn't found, nil is returned.
0
+ # The <tt>:radius</tt> option expands the excerpt on each side of the first occurrence of +phrase+ by the number of characters
0
+ # defined in <tt>:radius</tt> (which defaults to 100). If the excerpt radius overflows the beginning or end of the +text+,
0
+ # then the <tt>:omission</tt> option (which defaults to "...") will be prepended/appended accordingly. The resulting string
0
+ # will be stripped in any case. If the +phrase+ isn't found, nil is returned.
0
- # excerpt('This is an example', 'an', 5)
0
- # # => "...s is an exam..."
0
+ # excerpt('This is an example', 'an', :radius => 5)
0
+ # # => ...s is an exam...
0
- # excerpt('This is an example', 'is', 5)
0
+ # excerpt('This is an example', 'is', :radius => 5)
0
# excerpt('This is an example', 'is')
0
- # # =>
"This is an example"0
+ # # =>
This is an example0
- # excerpt('This next thing is an example', 'ex', 2)
0
+ # excerpt('This next thing is an example', 'ex', :radius => 2)
0
- # excerpt('This is also an example', 'an', 8, '<chop> ')
0
- # # => "<chop> is also an example"
0
- def excerpt(text, phrase, radius = 100, excerpt_string = "...")
0
+ # excerpt('This is also an example', 'an', :radius => 8, :omission => '<chop> ')
0
+ # # => <chop> is also an example
0
+ # You can still use <tt>excerpt</tt> with the old API that accepts the
0
+ # +radius+ as its optional third and the +ellipsis+ as its
0
+ # optional forth parameter:
0
+ # excerpt('This is an example', 'an', 5) # => ...s is an exam...
0
+ # excerpt('This is also an example', 'an', 8, '<chop> ') # => <chop> is also an example
0
+ def excerpt(text, phrase, *args)
0
+ options = args.extract_options!
0
+ options[:radius] = args[0] || 100
0
+ options[:omission] = args[1] || "..."
0
+ options.reverse_merge!(:radius => 100, :omission => "...")
0
phrase = Regexp.escape(phrase)
0
if found_pos = text.chars =~ /(#{phrase})/i
0
- start_pos = [ found_pos - radius, 0 ].max
0
- end_pos = [ [ found_pos + phrase.chars.length + radius - 1, 0].max, text.chars.length ].min
0
+ start_pos = [ found_pos - options[:radius], 0 ].max
0
+ end_pos = [ [ found_pos + phrase.chars.length + options[:radius] - 1, 0].max, text.chars.length ].min
0
- prefix = start_pos > 0 ? excerpt_string : ""
0
- postfix = end_pos < text.chars.length - 1 ? excerpt_string : ""
0
+ prefix = start_pos > 0 ? options[:omission] : ""
0
+ postfix = end_pos < text.chars.length - 1 ? options[:omission] : ""
0
prefix + text.chars[start_pos..end_pos].strip + postfix
0
@@ -132,16 +184,23 @@ module ActionView
0
- def excerpt(text, phrase, radius = 100, excerpt_string = "...") #:nodoc:
0
+ def excerpt(text, phrase, *args) #:nodoc:
0
+ options = args.extract_options!
0
+ options[:radius] = args[0] || 100
0
+ options[:omission] = args[1] || "..."
0
+ options.reverse_merge!(:radius => 100, :omission => "...")
0
phrase = Regexp.escape(phrase)
0
if found_pos = text =~ /(#{phrase})/i
0
- start_pos = [ found_pos - radius, 0 ].max
0
- end_pos = [ [ found_pos + phrase.length + radius - 1, 0].max, text.length ].min
0
+ start_pos = [ found_pos - options[:radius], 0 ].max
0
+ end_pos = [ [ found_pos + phrase.length + options[:radius] - 1, 0].max, text.length ].min
0
- prefix = start_pos > 0 ? excerpt_string : ""
0
- postfix = end_pos < text.length - 1 ? excerpt_string : ""
0
+ prefix = start_pos > 0 ? options[:omission] : ""
0
+ postfix = end_pos < text.length - 1 ? options[:omission] : ""
0
prefix + text[start_pos..end_pos].strip + postfix
0
@@ -176,20 +235,31 @@ module ActionView
0
# (which is 80 by default).
0
- # word_wrap('Once upon a time', 4)
0
- # # => Once\nupon\na\ntime
0
- # word_wrap('Once upon a time', 8)
0
- # # => Once upon\na time
0
# word_wrap('Once upon a time')
0
# # => Once upon a time
0
- # word_wrap('Once upon a time', 1)
0
+ # 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...')
0
+ # # => 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...
0
+ # word_wrap('Once upon a time', :line_width => 8)
0
+ # # => Once upon\na time
0
+ # word_wrap('Once upon a time', :line_width => 1)
0
# # => Once\nupon\na\ntime
0
- def word_wrap(text, line_width = 80)
0
+ # You can still use <tt>word_wrap</tt> with the old API that accepts the
0
+ # +line_width+ as its optional second parameter:
0
+ # word_wrap('Once upon a time', 8) # => Once upon\na time
0
+ def word_wrap(text, *args)
0
+ options = args.extract_options!
0
+ options[:line_width] = args[0] || 80
0
+ options.reverse_merge!(:line_width => 80)
0
text.split("\n").collect do |line|
0
- line.length >
line_width ? line.gsub(/(.{1,#{line_width}})(\s+|$)/, "\\1\n").strip : line
0
+ line.length >
options[:line_width] ? line.gsub(/(.{1,#{options[:line_width]}})(\s+|$)/, "\\1\n").strip : line
0
@@ -336,12 +406,32 @@ module ActionView
0
# # => "Welcome to my new blog at <a href=\"http://www.myblog.com/\" target=\"_blank\">http://www.m...</a>.
0
# Please e-mail me at <a href=\"mailto:me@email.com\">me@email.com</a>."
0
- def auto_link(text, link = :all, href_options = {}, &block)
0
+ # You can still use <tt>auto_link</tt> with the old API that accepts the
0
+ # +link+ as its optional second parameter and the +html_options+ hash
0
+ # as its optional third parameter:
0
+ # post_body = "Welcome to my new blog at http://www.myblog.com/. Please e-mail me at me@email.com."
0
+ # auto_link(post_body, :urls) # => Once upon\na time
0
+ # # => "Welcome to my new blog at <a href=\"http://www.myblog.com/\">http://www.myblog.com</a>.
0
+ # Please e-mail me at me@email.com."
0
+ # auto_link(post_body, :all, :target => "_blank") # => Once upon\na time
0
+ # # => "Welcome to my new blog at <a href=\"http://www.myblog.com/\" target=\"_blank\">http://www.myblog.com</a>.
0
+ # Please e-mail me at <a href=\"mailto:me@email.com\">me@email.com</a>."
0
+ def auto_link(text, *args, &block)#link = :all, href_options = {}, &block)
0
return '' if text.blank?
0
- when :all then auto_link_email_addresses(auto_link_urls(text, href_options, &block), &block)
0
- when :email_addresses then auto_link_email_addresses(text, &block)
0
- when :urls then auto_link_urls(text, href_options, &block)
0
+ options = args.size == 2 ? {} : args.extract_options! # this is necessary because the old auto_link API has a Hash as its last parameter
0
+ options[:link] = args[0] || :all
0
+ options[:html] = args[1] || {}
0
+ options.reverse_merge!(:link => :all, :html => {})
0
+ case options[:link].to_sym
0
+ when :all then auto_link_email_addresses(auto_link_urls(text, options[:html], &block), &block)
0
+ when :email_addresses then auto_link_email_addresses(text, &block)
0
+ when :urls then auto_link_urls(text, options[:html], &block)
0
@@ -468,7 +558,7 @@ module ActionView
0
[-\w]+ # subdomain or domain
0
(?:\.[-\w]+)* # remaining subdomains or domain
0
- (?:/(?:(?:[~\w\+@%=\(\)-]|(?:[,.;:'][^\s$]))
)*)* # path
0
+ (?:/(?:(?:[~\w\+@%=\(\)-]|(?:[,.;:'][^\s$]))
+)?)* # path
0
(?:\?[\w\+@%&=.;-]+)? # query string
0
(?:\#[\w\-]*)? # trailing anchor
0
@@ -477,8 +567,8 @@ module ActionView
0
# Turns all urls into clickable links. If a block is given, each url
0
# is yielded and the result is used as the link text.
0
- def auto_link_urls(text, href_options = {})
0
- extra_options = tag_options(href_options.stringify_keys) || ""
0
+ def auto_link_urls(text, html_options = {})
0
+ extra_options = tag_options(html_options.stringify_keys) || ""
0
text.gsub(AUTO_LINK_RE) do
0
all, a, b, c, d = $&, $1, $2, $3, $4
0
if a =~ /<a\s/i # don't replace URL's that are already linked
0
@@ -508,4 +598,4 @@ module ActionView
0
\ No newline at end of file