NokogiriTruncateHtml is just like the vanilla truncate rails helper, except it respects tags and html entities, and returns valid html.
Nokogiri and HtmlEntities are used for the parsing.
Inspired and heavily influenced by this article by Mike Burns and the links contained therein.
when post.title
is
<p>This is <em>my <strong>first</strong> post</em></p>
Then
<%= truncate_html post.title, :length => 15 %>
results in
<p>This is <em>my <strong>fir…</strong></em></p>
Requires the htmlentities
and nokogiri
gems:
in Gemfile:
gem "nokogiri_truncate_html"
-
Coverage is 100% (C0)
-
CI: tested against all 2.x branches of rails
RSpec is used for testing, so the tests are in spec/
rather than test/
Do rake –tasks for more details.