lib/app_helpers/truncate.rb
def better_truncate(text, max_length = 30, ellipsis = "...")
    return if text.nil?

    doc = Hpricot(text.to_s)
    ellipsis_length = Hpricot(ellipsis).inner_text.chars.length
    content_length = doc.inner_text.chars.length
    actual_length = max_length - ellipsis_length

    content_length > max_length ? doc.truncate(actual_length, logger).inner_html + ellipsis : text.to_s
  end
# Like the Rails _truncate_ helper but doesn't break HTML tags or entities.
Like the Rails _truncate_ helper but doesn't break HTML tags or entities.
Like the Rails _truncate_ helper but doesn ' t break HTML tags or entities .
def better_truncate(text, max_length = 30, ellipsis = "...")
    return if text.nil?
    doc = Hpricot(text.to_s)
    ellipsis_length = Hpricot(ellipsis).inner_text.chars.length
    content_length = doc.inner_text.chars.length
    actual_length = max_length - ellipsis_length
    content_length > max_length ? doc.truncate(actual_length, logger).inner_html + ellipsis : text.to_s
  end
def better_truncate ( text , max_length = 30 , ellipsis = "..." ) return if text . nil? doc = Hpricot ( text . to_s ) ellipsis_length = Hpricot ( ellipsis ) . inner_text . chars . length content_length = doc . inner_text . chars . length actual_length = max_length - ellipsis_length content_length > max_length ? doc . truncate ( actual_length , logger ) . inner_html + ellipsis : text . to_s end
Like the Rails _truncate_ helper but doesn't break HTML tags or entities.
Like the Rails _truncate_ helper but doesn ' t break HTML tags or entities .
text
max_length
ellipsis