GitHub Sale: sign up for any paid plan this week and pay nothing until January 1, 2009!  [ hide ]

public
Fork of rails/rails
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/Manfred/rails.git
Remove special 1.9 version of excerpt helper.
Manfred (author)
Fri Jun 13 06:34:51 -0700 2008
commit  44543600dcdcfd51300330645729b174ef5c1f3c
tree    10c0d1cfc0327ffda25ff25cde6b510b229eb7d2
parent  1a3e4e46f132457946ce84725b5aaa6ebee2dfdb
...
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
133
134
135
136
 
 
 
137
138
139
 
 
140
141
142
143
144
 
 
 
145
146
147
...
87
88
89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
 
 
 
115
116
117
118
 
 
119
120
121
 
 
 
 
122
123
124
125
126
127
0
@@ -87,61 +87,41 @@ module ActionView
0
         end
0
       end
0
 
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
- #
0
- # ==== Examples
0
- # excerpt('This is an example', 'an', 5)
0
- # # => "...s is an exam..."
0
- #
0
- # excerpt('This is an example', 'is', 5)
0
- # # => "This is a..."
0
- #
0
- # excerpt('This is an example', 'is')
0
- # # => "This is an example"
0
- #
0
- # excerpt('This next thing is an example', 'ex', 2)
0
- # # => "...next..."
0
- #
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
- if text && phrase
0
- phrase = Regexp.escape(phrase)
0
-
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
-
0
- prefix = start_pos > 0 ? excerpt_string : ""
0
- postfix = end_pos < text.mb_chars.length - 1 ? excerpt_string : ""
0
-
0
- prefix + text.mb_chars[start_pos..end_pos].strip + postfix
0
- else
0
- nil
0
- end
0
- end
0
- end
0
- else
0
- def excerpt(text, phrase, radius = 100, excerpt_string = "...") #:nodoc:
0
- if text && phrase
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
+ #
0
+ # ==== Examples
0
+ # excerpt('This is an example', 'an', 5)
0
+ # # => "...s is an exam..."
0
+ #
0
+ # excerpt('This is an example', 'is', 5)
0
+ # # => "This is a..."
0
+ #
0
+ # excerpt('This is an example', 'is')
0
+ # # => "This is an example"
0
+ #
0
+ # excerpt('This next thing is an example', 'ex', 2)
0
+ # # => "...next..."
0
+ #
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
+ if text && phrase
0
+ phrase = Regexp.escape(phrase)
0
 
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
 
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
 
0
- prefix + text[start_pos..end_pos].strip + postfix
0
- else
0
- nil
0
- end
0
+ prefix + text.mb_chars[start_pos..end_pos].strip + postfix
0
+ else
0
+ nil
0
           end
0
         end
0
       end

Comments

    No one has commented yet.