app/helpers/application_helper.rb
# Custom HTML truncate for PD descriptions, which don't always validate
Custom HTML truncate for PD descriptions, which don't always validate
Custom HTML truncate for PD descriptions which don ' t always validate
def truncate_html(input, len = 30, extension = "...")
    output = input
    output.gsub!(/<\/p>\s*<p>/is, "<br /><br />")
    output = sanitize(input, :tags => %w(br h1 h2 h3 h4 h5 h6 ul ol li))
    output.gsub!(/<br.*?>/, "\v")
    if output.length < len
      return input
    end
    l = len - extension.mb_chars.length
    chars = input.mb_chars
    output = (chars.length > len ? chars[0...l] + extension : input).to_s
    output.strip!
    output.gsub!(/\v/, "<br />")
    output.html_safe
  end
def truncate_html ( input , len = 30 , extension = "..." ) output = input output . gsub! ( / < \/ p> \s *<p> /is , "<br /><br />" ) output = sanitize ( input , :tags => %w( br h1 h2 h3 h4 h5 h6 ul ol li ) ) output . gsub! ( / <br.*?> / , "\v" ) if output . length < len return input end l = len - extension . mb_chars . length chars = input . mb_chars output = ( chars . length > len ? chars [ 0 ... l ] + extension : input ) . to_s output . strip! output . gsub! ( / \v / , "<br />" ) output . html_safe end
Custom HTML truncate for PD descriptions, which don't always validate
Custom HTML truncate for PD descriptions which don ' t always validate
# We need to be able to call .s on the input, but not on the extension, so we # have to use a modified version of truncate() instead of truncate() itself. # output = truncate(input, :length => len, :omission => extension) # Temporarily removing .s, as it takes a while to run on long strings #output = (chars.length > len ? chars[0...l].s + extension : input).to_s
input
<not_specific>
len
<not_specific>
extension
<not_specific>