Project

abbreviato

0.01
A long-lived project that still receives updates
Truncate HTML to a specific bytesize, while keeping valid markup
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

~> 4.3.4
~> 1.16.2
 Project Readme

abbreviato

Continuous Integration Security Gem Version

abbreviato is a Ruby library for truncating HTML strings keeping the markup valid. It is a fork of jorgemanrubia/truncato but focused on truncating to a bytesize, not on a per-character basis.

Installing

In your Gemfile

gem 'abbreviato'

Usage

truncated_string, was_truncated = Abbreviato.truncate("<p>some text</p>", max_length: 4)
# => ["<p>s...</p>", true]

The configuration options are:

  • max_length: The size, in bytes, to truncate (30 by default)
  • tail: The string to append when the truncation occurs ('…' by default).
  • fragment: Indicates whether the document to be truncated is an HTML fragment or an entire document (with HTML, HEAD & BODY tags). Setting to true prevents automatic addition of these tags if they are missing. Defaults to true.

Performance

Abbreviato was designed with performance in mind. Its main motivation was that existing libs couldn't truncate a multiple-MB document into a few-KB one in a reasonable time. It uses the Nokogiri SAX parser.

Running the tests

bundle exec rake

Running a single test

rspec spec/abbreviato/abbreviato_spec.rb
rspec spec/abbreviato/abbreviato_spec.rb:357

Running all checks

bundle exec rake spec

Contribute

Follow our contribution guidelines.