Permalink
Browse files

Tidy up excerpt separator logic a bit

  • Loading branch information...
1 parent aaa33e6 commit 7b31a430858c40238dec0a7530937a4b7ee95c7b @carlosantoniodasilva carlosantoniodasilva committed Sep 8, 2012
Showing with 11 additions and 17 deletions.
  1. +11 −17 actionpack/lib/action_view/helpers/text_helper.rb
@@ -149,10 +149,8 @@ def highlight(text, phrases, options = {})
# # => ...a very beautiful...
def excerpt(text, phrase, options = {})
return unless text && phrase
- radius = options.fetch(:radius, 100)
- omission = options.fetch(:omission, "...")
- separator = options.fetch(:separator, "")
+ separator = options.fetch(:separator, "")
phrase = Regexp.escape(phrase)
regex = /#{phrase}/i
@@ -168,11 +166,8 @@ def excerpt(text, phrase, options = {})
first_part, second_part = text.split(regex, 2)
- options = options.merge(:part_position => :first)
- prefix, first_part = cut_part(first_part, options)
-
- options = options.merge(:part_position => :second)
- postfix, second_part = cut_part(second_part, options)
+ prefix, first_part = cut_excerpt_part(:first, first_part, separator, options)
+ postfix, second_part = cut_excerpt_part(:second, second_part, separator, options)
prefix + (first_part + separator + phrase + separator + second_part).strip + postfix
end
@@ -420,25 +415,24 @@ def split_paragraphs(text)
end
end
- def cut_part(part, options)
- radius = options.fetch(:radius, 100)
- omission = options.fetch(:omission, "...")
- separator = options.fetch(:separator, "")
- part_position = options.fetch(:part_position)
-
+ def cut_excerpt_part(part_position, part, separator, options)
return "", "" unless part
+ radius = options.fetch(:radius, 100)
+ omission = options.fetch(:omission, "...")
+
part = part.split(separator)
part.delete("")
affix = part.size > radius ? omission : ""
+
part = if part_position == :first
drop_index = [part.length - radius, 0].max
- part.drop(drop_index).join(separator)
+ part.drop(drop_index)
else
- part.first(radius).join(separator)
+ part.first(radius)
end
- return affix, part
+ return affix, part.join(separator)
end
end
end

0 comments on commit 7b31a43

Please sign in to comment.