lib/middleman-blog/truncate_html.rb in middleman-blog-3.2.0 vs lib/middleman-blog/truncate_html.rb in middleman-blog-3.3.0
- old
+ new
@@ -6,11 +6,12 @@
# Taken and modified from http://madebydna.com/all/code/2010/06/04/ruby-helper-to-cleanly-truncate-html.html
# MIT license
module TruncateHTML
def self.truncate_html(text, max_length, ellipsis = "...")
- ellipsis_length = ellipsis.length
+ ellipsis_length = ellipsis.length
+ text = text.encode('UTF-8') if text.respond_to?(:encode)
doc = Nokogiri::HTML::DocumentFragment.parse text
content_length = doc.inner_text.length
actual_length = max_length - ellipsis_length
if content_length > actual_length
doc.truncate(actual_length, ellipsis).inner_html
@@ -44,10 +45,18 @@
Nokogiri::XML::Text.new(trimmed_content, parent)
end
end
+ module CommentNode
+ def truncate(*args)
+ # Don't truncate comments, since they aren't visible
+ self
+ end
+ end
+
end
Nokogiri::HTML::DocumentFragment.send(:include, NokogiriTruncator::NodeWithChildren)
Nokogiri::XML::Element.send(:include, NokogiriTruncator::NodeWithChildren)
Nokogiri::XML::Text.send(:include, NokogiriTruncator::TextNode)
+Nokogiri::XML::Comment.send(:include, NokogiriTruncator::CommentNode)