• Stars
    star
    248
  • Rank 163,560 (Top 4 %)
  • Language
    Ruby
  • License
    MIT License
  • Created almost 14 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Wants to truncate an HTML string properly? This Ruby gem is for you.

HTML Truncator

Wants to truncate an HTML string properly? This gem is for you. It's powered by Nokogiri!

How to use it

It's very simple. Install it with rubygems:

gem install html_truncator

Or, if you use bundler, add it to your Gemfile:

gem "html_truncator", "~>0.2"

Then you can use it in your code:

require "html_truncator"
HTML_Truncator.truncate("<p>Lorem ipsum dolor sit amet.</p>", 3)
# => "<p>Lorem ipsum dolor…</p>"

The HTML_Truncator class has only one method, truncate, with 3 arguments:

  • the HTML-formatted string to truncate
  • the number of words to keep (real words, tags and attributes aren't count)
  • some options like the ellipsis (optional, '…' by default).

And 3 attributes:

  • ellipsable_tags, which lists the tags that can contain the ellipsis (by default: p ol ul li div header article nav section footer aside dd dt dl)
  • self_closing_tags, with the tags to keep when empty (by default: br hr img param embed)
  • punctuation_chars, with the punctation characters to remove before the ellipsis (by default: , . : ; ! ?).

Examples

A simple example:

HTML_Truncator.truncate("<p>Lorem ipsum dolor sit amet.</p>", 3)
# => "<p>Lorem ipsum dolor…</p>"

If the text is too short to be truncated, it won't be modified:

HTML_Truncator.truncate("<p>Lorem ipsum dolor sit amet.</p>", 5)
# => "<p>Lorem ipsum dolor sit amet.</p>"

If you prefer, you can have the length in characters instead of words:

HTML_Truncator.truncate("<p>Lorem ipsum dolor sit amet.</p>", 12, :length_in_chars => true)
# => "<p>Lorem ipsum…</p>"

It doesn't cut inside a word but goes back to the immediately preceding word boundary:

HTML_Truncator.truncate("<p>Lorem ipsum dolor sit amet.</p>", 10, :length_in_chars => true)
# => "<p>Lorem…</p>"

You can customize the ellipsis:

HTML_Truncator.truncate("<p>Lorem ipsum dolor sit amet.</p>", 3, :ellipsis => " (truncated)")
# => "<p>Lorem ipsum dolor (truncated)</p>"

And even have HTML in the ellipsis:

HTML_Truncator.truncate("<p>Lorem ipsum dolor sit amet.</p>", 3, :ellipsis => '<a href="/more-to-read">...</a>')
# => "<p>Lorem ipsum dolor<a href="/more-to-read">...</a></p>"

The ellipsis is put at the right place, inside <p>, but not <i>:

HTML_Truncator.truncate("<p><i>Lorem ipsum dolor sit amet.</i></p>", 3)
# => "<p><i>Lorem ipsum dolor</i>…</p>"

And the punctation just before the ellipsis is not kept:

HTML_Truncator.truncate("<p>Lorem ipsum: lorem ipsum dolor sit amet.</p>", 2)
# => "<p>Lorem ipsum…</p>"

You can indicate that a tag can contain the ellipsis but adding it to the ellipsable_tags:

HTML_Truncator.ellipsable_tags << "blockquote"
HTML_Truncator.truncate("<blockquote>Lorem ipsum dolor sit amet.</blockquote>", 3)
# => "<blockquote>Lorem ipsum dolor…</blockquote>"

You can know if a string was truncated with the html_truncated? method:

HTML_Truncator.truncate("<p>Lorem ipsum dolor sit amet.</p>", 3).html_truncated?
# => true

You can ignore images in the text by overriding the self_closing_tags attribute:

HTML_Truncator.self_closing_tags.delete "img"
HTML_Truncator.truncate("<p>Lorem ipsum <img src='...'>dolor sit amet.</p>", 3)
# => "<p>Lorem ipsum dolor…</p>"

If you already have parsed an HTML document with Nokogiri, you can use it directly to truncate:

document = Nokogiri::HTML::DocumentFragment.parse(text)
# Doing something with this document
options = HTML_Truncator::DEFAULT_OPTIONS.merge(length_in_char: true)
document.truncate(12, options)

Alternatives

Rails has a truncate helper, but as the doc says:

Care should be taken if text contains HTML tags or entities, because truncation may produce invalid HTML (such as unbalanced or incomplete tags).

I know there are some Ruby code to truncate HTML, like:

But I'm not pleased with these solutions: they are either based on regexp for parsing the content (too fragile), they don't put the ellipsis where expected, they cut words and sometimes leave empty DOM nodes. So I made my own gem ;-)

Issues or Suggestions

Found an issue or have a suggestion? Please report it on Github's issue tracker.

If you wants to make a pull request, please check the specs before:

rspec spec

Credits

Thanks to François de Metz for his awesome help! Thanks to kuroir and benhutton for their suggestions.

The code is released under the MIT license. See the MIT-LICENSE file for the full license.

♡2011 by Bruno Michel. Copying is an act of love. Please copy and share.

More Repositories

1

vim-handlebars

[deprecated] Vim plugin for Handlebars
Vim Script
77
star
2

slide-em-up

Write some slides in markdown, choose a style and slide'em up displays them in HTML5.
CSS
48
star
3

github_vim_theme

Github Theme for ViM
Vim Script
42
star
4

Jekyll-plugins

[Deprecated] My plugins for Jekyll
Ruby
39
star
5

dotfiles

My dotfiles (vim, zsh, irssi, etc.)
Vim Script
24
star
6

Presentations

My presentations on conferences and barcamps
JavaScript
19
star
7

jquery.vim

Syntax file for jQuery in ViM
Vim Script
18
star
8

merb.vim

Some Vim files for Merb users
Vim Script
16
star
9

goals-calendar

Goals calendar is a Seinfeld calendar written in Google's Go (unfinished dead code)
Go
15
star
10

mddiff

Semantic diff for markdown
JavaScript
15
star
11

xor_me

Brute force doc/xls passwords
C++
12
star
12

cssgrep

A command line tool ala grep based on XPath or/and CSS selectors
Ruby
12
star
13

Goldorak.Go

Goldorak GO is a mini framework for the Go programming language. (unfinished dead code)
Go
11
star
14

markitup-wikicreole

MarkItUp plugin for Wikicreole syntax
JavaScript
8
star
15

french-rails

A plugin for Rails for simple french localization
Ruby
6
star
16

HTML-Spellchecker

Wants to spellcheck an HTML string properly? This gem is for you.
Ruby
6
star
17

don-t-break-the-chain

My first Django application: a virtual calendar for Jerry Seinfeld's productivity secret (unfinished dead code)
Python
6
star
18

FightForNetNeutrality

This package is a Rack middleware which allow to block some IP Address. By default the french parlement is denied.
Ruby
6
star
19

Trollometre

Partez à la chasse aux trolls
Python
5
star
20

guide-em-up

Guide'em up is a tool for writing guides in markdown
Ruby
5
star
21

ditaa-live

Live preview of ditaa files in your browser
Go
4
star
22

resizer

Resizes images to a ratio just by expanding it (no deformation, no interpolation)
Go
4
star
23

json_select

A ruby version of JSONSelect, an experimental selector language for JSON.
Ruby
3
star
24

gofpdf

My fork of http://code.google.com/p/gofpdf/
Go
3
star
25

drivy-jobs

Ruby
2
star
26

AgileCup-maze

My solution to the first Agile Cup
Ruby
2
star
27

af83-showoff

A template with af83 colors for showoff
JavaScript
2
star
28

koalab-golang-api

Golang version of the Koalab API
Go
2
star
29

rulu-demo

Ruby Lugdunum demo of Goliath
Ruby
2
star
30

dev-links

Playing with Redis: a small app for posting links
Ruby
2
star
31

Ruby-Christmas-Contest

Ruby
2
star
32

Ruby-Easter-Contest

Ruby
1
star
33

Board-LinuxFr.org

[OLD] Push the messages in the boards of LinuxFr.org to the browsers by a long-polling method.
Ruby
1
star
34

NoSQL-trends

NoSQL trends is a web site that graphs the popularity of NoSQL databases in function of the number of their tweets.
Ruby
1
star
35

sprockets_spacely

Sprockets 2 + Mustache.js
Ruby
1
star
36

cozy-nuit-du-hack

Cozy V3 Docker Image for the Nuit du Hack
Shell
1
star
37

nono.github.com

My blog
HTML
1
star
38

cozy-desktop-experiments

Trying stuff to understand better the limitations of cozy-desktop and improve it
Go
1
star
39

chat-linuxfr.org

[Deprecated] A tornado application for LinuxFr.org
Python
1
star
40

xmpp-avatar

Git mirror of http://linkmauve.fr/avatar/
JavaScript
1
star
41

kata_chat_golang

Rules: implement a simple Multi User Chatroom in Golang
Go
1
star
42

Rubylude

A port of Perlude to Ruby 1.9
Ruby
1
star
43

md2glmf

[fr] Un sript pour transformer du markdown en un article pour GNU/Linux Magazine France
Ruby
1
star
44

c3po

Ruby translator client. For Google Translate and Bing Translate
Ruby
1
star