public Method

TextHelper.excerpt(text, phrase, radius = 100, excerpt_string = "...")

There's no documentation for this item.

Source Code

# File action_view/helpers/text_helper.rb, line 131
def excerpt(text, phrase, radius = 100, excerpt_string = "...") #:nodoc:
  if text && phrase
    phrase = Regexp.escape(phrase)

    if found_pos = text =~ /(#{phrase})/i
      start_pos = [ found_pos - radius, 0 ].max
      end_pos   = [ found_pos + phrase.length + radius, text.length ].min

      prefix  = start_pos > 0 ? excerpt_string : ""
      postfix = end_pos < text.length ? excerpt_string : ""

      prefix + text[start_pos..end_pos].strip + postfix
    else
      nil
    end
  end
end
Comments

Have your say
Please use Textile formatting (click here for a cheat sheet). Use <code/> and <pre/> for code samples.
Click here to login with OpenID to to post comments.