hgimenez / truncate_html

truncates html so you don't have to

This URL has Read+Write access

name age message
file .gitignore Fri Sep 25 18:47:24 -0700 2009 Ignoring coverage directory [hgimenez]
file History.txt Loading commit data...
file README.markdown Tue Aug 25 09:00:10 -0700 2009 Gem installation instructions, now with gemcutt... [hgimenez]
file Rakefile
file VERSION
file init.rb Fri Jul 31 07:11:24 -0700 2009 Initial commit of plugin files [hgimenez]
file install.rb Fri Jul 31 07:11:24 -0700 2009 Initial commit of plugin files [hgimenez]
directory lib/
directory spec/
directory tasks/ Fri Jul 31 07:11:24 -0700 2009 Initial commit of plugin files [hgimenez]
file truncate_html.gemspec
file uninstall.rb Fri Jul 31 07:11:24 -0700 2009 Initial commit of plugin files [hgimenez]
README.markdown

TruncateHtml

truncate_html is a Rails helper plugin that cuts off a string of HTML and takes care of closing any lingering open tags. There are many possible solutions to this. This plugin does not have any dependencies, and does all it's work via regular expressions.

The API is very similar to Rails' own truncate method.

Example

some_html = '<ul><li><a href="http://whatever">This is a link</a></li></ul>'

truncate_html(some_html, :length => 5, :omission => '...(continued)')

  => <ul><li><a href="http://whatever">This is...(continued)</a></li></ul>

A few notes:

  • It will truncate on a word boundary.
  • The default options are:
    • :length => 100
    • :omission => '...'

Installation

As a gem

Add this to your config/environment.rb:

config.gem 'truncate_html',
  :source => 'http://gemcutter.org'

Then either sudo rake gems:install or sudo gem install truncate_html

As a plugin:

script/plugin install git://github.com/hgimenez/truncate_html.git

Issues

Found an issue? Please report it on Github's issue tracker.

Testing

The plugin is tested using RSpec. Install it on your app if you wish to run the tests.

Copyright (c) 2009 Harold A. Giménez, released under the MIT license