module ActionView::Helpers::TextHelper

def truncate(text, options = {}, &block)

# => "Once upon a time in a world...Continue"
truncate("Once upon a time in a world far far away") { link_to "Continue", "#" }

# => "

Once upon a time in a wo..."
truncate("

Once upon a time in a world far far away

", escape: false)

# => "<p>Once upon a time in a wo..."
truncate("

Once upon a time in a world far far away

")

# => "And they f... (continued)"
truncate("And they found that many people were sleeping better.", length: 25, omission: '... (continued)')

# => "Once upon a..."
truncate("Once upon a time in a world far far away", length: 17, separator: ' ')

# => "Once upon a ti..."
truncate("Once upon a time in a world far far away", length: 17)

# => "Once upon a time in a world..."
truncate("Once upon a time in a world far far away")

==== Examples

Whether to escape the result. Defaults to true.
[+:escape+]

By default, truncation can occur at any character in +text+.
A string or regexp used to find a breaking point at which to truncate.
[+:separator+]

The string to append after truncating. Defaults to "...".
[+:omission+]

any extra content from the block. Defaults to 30.
The maximum number of characters that should be returned, excluding
[+:length+]

==== Options

produce invalid HTML, such as unbalanced or incomplete tags.
if +text+ might contain HTML tags or entities, because truncation could
In any case, the result will be marked HTML-safe. Care should be taken
The result will be escaped unless escape: false is specified.

cause the total length to exceed +:length+ characters.
omission marker when +text+ is truncated. However, this content _can_
You can also pass a block to render and append extra content after the

total length not exceeding +:length+.
is truncated, an omission marker will be appended to the result for a
Truncates +text+ if it is longer than a specified +:length+. If +text+
def truncate(text, options = {}, &block)
  if text
    length  = options.fetch(:length, 30)
    content = text.truncate(length, options)
    content = options[:escape] == false ? content.html_safe : ERB::Util.html_escape(content)
    content << capture(&block) if block_given? && text.length > length
    content
  end
end