daily pastebin goal
63%
SHARE
TWEET

Untitled

a guest Apr 17th, 2018 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ## application_helper.rb
  2.  
  3.   # truncate markdown/textile and it won't complain about broken links
  4.   # uses words instead of characters, there are 5 (+-1) characters per word (in English)
  5.  
  6.   def truncate(text, wordcount = 6, truncate_string = "...")
  7.     if text.nil? then return end
  8.       words = text.split(/\s+/)
  9.       bad_chars = ['[','"', '!', '?', '*', '_', '-', '+', '^','~','%','(','{','\'']
  10.    
  11.     if words.size > wordcount
  12.       out = ""
  13.       for n in 0..wordcount do
  14.         out << words[n] + ' ' unless ((n == wordcount) and (bad_chars.include?(words[n][0..0])))
  15.       end
  16.       out + truncate_string
  17.     else
  18.       text.to_s
  19.     end
  20.   end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top