Guest User

Untitled

a guest
Apr 17th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  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
Add Comment
Please, Sign In to add comment