app/helpers/functions.rb
# I'm not suggesting this is particularly sane, but it works.
#
# The little bit of regex fixes cases where two paragraphs get squished
# together and you end up with the unsightly
#
#   "end of sentence.New sentence"
#
# when you want
#
#   "end of sentence. New sentence"
#
# (I'm sure there are *loads* of edge-cases it will break on, but
# this sort of handles some common cases, including various bits of
# punctuation.)
I'm not suggesting this is particularly sane, but it works.
The little bit of regex fixes cases where two paragraphs get squished
together and you end up with the unsightly



when you want

"end of sentence. New sentence"

(I'm sure there are *loads* of edge-cases it will break on, but
this sort of handles some common cases, including various bits of
punctuation.)
I ' m not suggesting this is particularly sane but it works . The little bit of regex fixes cases where two paragraphs get squished together and you end up with the unsightly when you want " end of sentence . New sentence " ( I ' m sure there are * loads * of edge - cases it will break on but this sort of handles some common cases including various bits of punctuation . )
def truncate(extended, len = 20, ellipsis = "...")
          markdown  = Redcarpet::Markdown.new(Redcarpet::Render::StripDown)
          content   = markdown.render(extended).split " "
          truncated = content[0...len]
          truncated.join(" ").gsub(/([a-z])(\.|[\.\?\!]?[\'|\"]|\!|\?)([A-Z])/) {
              "#{$1}#{$2} #{$3}"
          } << ( content.size > len ? ellipsis : "" )
      end
def truncate ( extended , len = 20 , ellipsis = "..." ) markdown = Redcarpet :: Markdown . new ( Redcarpet :: Render :: StripDown ) content = markdown . render ( extended ) . split " " truncated = content [ 0 ... len ] truncated . join ( " " ) . gsub ( / ([a-z])( \. |[ \. \? \! ]?[ \' | \" ]| \! | \? )([A-Z]) / ) { "#{$1}#{$2} #{$3}" } << ( content . size > len ? ellipsis : "" ) end
I'm not suggesting this is particularly sane, but it works.
I ' m not suggesting this is particularly sane but it works .
extended
<not_specific>
len
<not_specific>
ellipsis
<not_specific>