Giter Site home page Giter Site logo

truncato's Introduction

truncato

truncato is a Ruby library for truncating HTML strings keeping the markup valid.

Installing

In your Gemfile

gem 'truncato'

Usage

Truncato.truncate "<p>some text</p>", max_length: 4 => "<p>s...</p>"
Truncato.truncate "<p>some text</p>", max_length: 4, count_tags: false => "<p>some...</p>"

The configuration options are:

  • max_length: The size, in characters, to truncate (30 by default)
  • tail: The string to append when the truncation occurs ('...' by default)
  • count_tags: Boolean value indicating whether tags size should be considered when truncating (true by default)
  • filtered_attributes: Array of attribute names that will be removed from the output. This allows you to make the truncated string shorter by excluding the content of attributes you can discard in some given context, e.g HTML style attribute.

Performance

Truncato 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.

There is a benchmark included that generates a synthetic XML of 4MB and truncates it to 400 KB. You can run the benchmark using

rake truncato:benchmark

There is a also a comparison benchmark that tests the previous data with other alternatives

rake truncato:vendor_compare

The results comparing truncato with other libs:

Truncato truncate_html HTML Truncator peppercorn
Time for truncating a 4MB XML document to 4KB 1.5 s 20 s 220 s 232 s

Running the tests

rake spec

truncato's People

Contributors

jorgemanrubia avatar skyeagle avatar rewinfrey avatar sguha00 avatar

Watchers

Martin Clifford avatar Sam de Freyssinet avatar Chris Bandy avatar Robert G avatar Jeremy Bush avatar Alex Faraone avatar Carly Ho avatar Jazmin Schroeder avatar Ben Downey avatar Chris Bake avatar Bob Vonderau avatar James Cloos avatar Kelsey Beyer avatar  avatar  avatar Edwin Mourant avatar Phil Peble avatar Shane Gibbs avatar Philip Szalwinski avatar  avatar Andrés Fortier avatar Novak avatar Maria Tz avatar Eleanor Lee avatar Matt McCabe avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.