Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## application_helper.rb
- # truncate markdown/textile and it won't complain about broken links
- # uses words instead of characters, there are 5 (+-1) characters per word (in English)
- def truncate(text, wordcount = 6, truncate_string = "...")
- if text.nil? then return end
- words = text.split(/\s+/)
- bad_chars = ['[','"', '!', '?', '*', '_', '-', '+', '^','~','%','(','{','\'']
- if words.size > wordcount
- out = ""
- for n in 0..wordcount do
- out << words[n] + ' ' unless ((n == wordcount) and (bad_chars.include?(words[n][0..0])))
- end
- out + truncate_string
- else
- text.to_s
- end
- end
Add Comment
Please, Sign In to add comment