I'm building a website using rails 3.1, which at it's core consists of a bunch of articles (similar to blog posts). These are stored and formatted as HTML. Now, what I need is to generate a preview of the article content so that only a short version is displayed in a feed of these articles. The content will have to be shortened without breaking its HTML.
Is there a simple way of doing this? A gem would be perfect, but ideas for a clean way to implement a shortening function myself would be great too. Thanks!
Edit: On doing further experimentation, I'm currently working with something of this sort, using the Sanitize gem, and it seems to work out pretty well:
def shortened_content(length)
Sanitize.clean(content[0..length], Sanitize::Config::RELAXED)
end
I dislike diluting and changing the direction of my question, but I guess I must.
Is there a better solution for things of this sort? (the sanitize gem, I assume, strips out unwanted content as well as cleans up HTML. I only really need it to clean up the HTML here)
Am I right in thinking I should probably move this processing to the upstream side of things? That is, store the shortened content as well as the full content. I figure it'll save me enough processing time to make it worth the extra storage.
Yes, the Sanitize gem is the right way to go. I did something similar using the Nokogiri gem - which you can see that the Sanitize gem relies on. It's definitely saner to store the shortened preview of the HTML in a new column in addition to the full HTML. So something like this in a before_save
def generate_preview
self.preview_content ||= shortened_content(100)
end
A nice side-effect is that an admin can now manually write a preview if needed.