Guest User

Untitled

a guest
Apr 20th, 2018
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. require 'rexml/parsers/pullparser'
  2. require 'htmlentities'
  3.  
  4. class String
  5. # Truncate strings containing HTML code
  6. # Usage example: "string".truncate_html(50, :word_cut => false, :tail => '[+]')
  7. def truncate_html(len = 30, opts = {})
  8. opts = {:word_cut => true, :tail => ' ...'}.merge(opts)
  9. p = REXML::Parsers::PullParser.new(self)
  10. tags = []
  11. new_len = len
  12. results = ''
  13. while p.has_next? && new_len > 0
  14. p_e = p.pull
  15. case p_e.event_type
  16. when :start_element
  17. tags.push p_e[0]
  18. results << "<#{tags.last} #{attrs_to_s(p_e[1])}>"
  19. when :end_element
  20. results << "</#{tags.pop}>"
  21. when :text
  22. text = HTMLEntities.decode_entities(p_e[0])
  23. if (text.length > new_len) and !opts[:word_cut]
  24. piece = text.first(text.index(' ', new_len))
  25. else
  26. piece = text.first(new_len)
  27. end
  28. results << HTMLEntities.encode_entities(piece)
  29. new_len -= text.length
  30. else
  31. results << "<!-- #{p_e.inspect} -->"
  32. end
  33. end
  34. tags.reverse.each do |tag|
  35. results << "</#{tag}>"
  36. end
  37. results << opts[:tail]
  38. end
  39.  
  40. private
  41.  
  42. def attrs_to_s(attrs)
  43. if attrs.empty?
  44. ''
  45. else
  46. attrs.to_a.map { |attr| %{#{attr[0]}="#{attr[1]}"} }.join(' ')
  47. end
  48. end
  49. end
Add Comment
Please, Sign In to add comment