Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jorgemanrubia/truncato
A tool for truncating HTML strings efficiently
https://github.com/jorgemanrubia/truncato
Last synced: about 10 hours ago
JSON representation
A tool for truncating HTML strings efficiently
- Host: GitHub
- URL: https://github.com/jorgemanrubia/truncato
- Owner: jorgemanrubia
- License: mit
- Created: 2012-11-10T07:30:25.000Z (about 12 years ago)
- Default Branch: master
- Last Pushed: 2022-07-05T05:48:45.000Z (over 2 years ago)
- Last Synced: 2024-05-02T12:38:36.139Z (7 months ago)
- Language: Ruby
- Size: 71.3 KB
- Stars: 57
- Watchers: 3
- Forks: 17
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# truncato
*truncato* is a Ruby library for truncating HTML strings keeping the markup valid.
## Installing
In your `Gemfile`
```ruby
gem 'truncato'
```## Usage
```ruby
Truncato.truncate "some text
", max_length: 4 #=> "s...
"
Truncato.truncate "some text
", max_length: 4, count_tags: false #=> "some...
"
```The configuration options are:
* `max_length`: The size, in characters, to truncate (`30` by default)
* `filtered_attributes`: Array of attribute names that will be removed in the truncated string. 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.
* `filtered_tags`: Array of tags that will be removed in the truncated string. If a tag is excluded, all the nested tags under it will be excluded too.
* `count_tags`: Boolean value indicating whether tags size should be considered when truncating (`true` by default)
* `tail_before_final_tag`: Boolean value indicating whether to apply a tail before the final closing tag (`false` by default)
* `comments`: Boolean value indicating whether to include comments in parsed results (`false` by default)
* `tail`: The string to append when the truncation occurs ('...' by default)
* `count_tail`: Boolean value indicating whether to include the tail within the bounds of the provided max length (`false` by default)## 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](http://nokogiri.org/) 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
```ruby
rake truncato:benchmark
```There is a also a comparison benchmark that tests the previous data with other alternatives
```ruby
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
```ruby
rake spec
```