This is a helper for Webby that excerpts the leading content of a rendered HTML block. It will close any dangling HTML tags at the point of cutoff. It borrows heavily from the truncate_html Rails helper by Ian White, itself based on code by Mike Burns.
Copy excerpt_helper.rb
into the Webby project's lib
directory. In your Webby Sitefile:
require 'lib/excerpt_helper'
Webby::Helpers.register(ExcerptHelper)
Given the following HTML:
foo = <div><h1>A Header</h1><p>Paragraph one.</p><!--break--><p>Paragraph two.</p></div>
excerpt(foo)
=> <div><h1>A Header</h1><p>Paragraph one.</p><p>Paragraph two.</p></div>
excerpt(foo, :count => 1)
=> <div><h1>A Header</h1><p>Paragraph one.</p></div>
excerpt(foo, :count => 1, :tag => 'h1')
=> <div><h1>A Header</h1></div>
excerpt(foo, :delimiter => 'break')
=> <div><h1>A Header</h1><p>Paragraph one.</p></div>