0
@@ -87,61 +87,41 @@ module ActionView
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
- # excerpt('This is an example', 'an', 5)
0
- # # => "...s is an exam..."
0
- # excerpt('This is an example', 'is', 5)
0
- # excerpt('This is an example', 'is')
0
- # # => "This is an example"
0
- # excerpt('This next thing is an example', 'ex', 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
- phrase = Regexp.escape(phrase)
0
- if found_pos = text.mb_chars =~ /(#{phrase})/i
0
- start_pos = [ found_pos - radius, 0 ].max
0
- end_pos = [ [ found_pos + phrase.mb_chars.length + radius - 1, 0].max, text.mb_chars.length ].min
0
- prefix = start_pos > 0 ? excerpt_string : ""
0
- postfix = end_pos < text.mb_chars.length - 1 ? excerpt_string : ""
0
- prefix + text.mb_chars[start_pos..end_pos].strip + postfix
0
- def excerpt(text, phrase, radius = 100, excerpt_string = "...") #:nodoc:
0
- phrase = Regexp.escape(phrase)
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
+ # excerpt('This is an example', 'an', 5)
0
+ # # => "...s is an exam..."
0
+ # excerpt('This is an example', 'is', 5)
0
+ # excerpt('This is an example', 'is')
0
+ # # => "This is an example"
0
+ # excerpt('This next thing is an example', 'ex', 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
+ 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
+ if found_pos = text.mb_chars =~ /(#{phrase})/i
0
+ start_pos = [ found_pos - radius, 0 ].max
0
+ end_pos = [ [ found_pos + phrase.mb_chars.length + radius - 1, 0].max, text.mb_chars.length ].min
0
- prefix = start_pos > 0 ? excerpt_string : ""
0
- postfix = end_pos < text.length - 1 ? excerpt_string : ""
0
+ prefix = start_pos > 0 ? excerpt_string : ""
0
+ postfix = end_pos < text.mb_chars.length - 1 ? excerpt_string : ""
0
- prefix + text[start_pos..end_pos].strip + postfix
0
+ prefix + text.mb_chars[start_pos..end_pos].strip + postfix
Comments
No one has commented yet.