Created
June 5, 2011 19:55
-
-
Save maxschulze/1009336 to your computer and use it in GitHub Desktop.
HTML Aware truncate for Ruby / Rails using nokogiri
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Source: | |
# http://blog.madebydna.com/all/code/2010/06/04/ruby-helper-to-cleanly-truncate-html.html | |
require "rubygems" | |
require "nokogiri" | |
module TextHelper | |
def truncate_html(text, max_length, ellipsis = "...") | |
ellipsis_length = ellipsis.length | |
doc = Nokogiri::HTML::DocumentFragment.parse text | |
content_length = doc.inner_text.length | |
actual_length = max_length - ellipsis_length | |
content_length > actual_length ? doc.truncate(actual_length).inner_html + ellipsis : text.to_s | |
end | |
end | |
module NokogiriTruncator | |
module NodeWithChildren | |
def truncate(max_length) | |
return self if inner_text.length <= max_length | |
truncated_node = self.dup | |
truncated_node.children.remove | |
self.children.each do |node| | |
remaining_length = max_length - truncated_node.inner_text.length | |
break if remaining_length <= 0 | |
truncated_node.add_child node.truncate(remaining_length) | |
end | |
truncated_node | |
end | |
end | |
module TextNode | |
def truncate(max_length) | |
Nokogiri::XML::Text.new(content[0..(max_length - 1)], parent) | |
end | |
end | |
end | |
Nokogiri::HTML::DocumentFragment.send(:include, NokogiriTruncator::NodeWithChildren) | |
Nokogiri::XML::Element.send(:include, NokogiriTruncator::NodeWithChildren) | |
Nokogiri::XML::Text.send(:include, NokogiriTruncator::TextNode) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment