Git Product home page Git Product logo

truncate_html's Introduction

TruncateHtml

Build Status Code Climate

truncate_html cuts off a string of HTML and takes care of closing any lingering open tags. There are many ways to solve this. This library does not have any dependencies, and parses HTML using regular expressions.

It can be used with or without Rails.

Example

some_html = '<ul><li><a href="http://whatever">This is a link</a></li></ul>'
truncate_html(some_html, :length => 15, :omission => '...(continued)')
  => <ul><li><a href="http://whatever">This...(continued)</a></li></ul>

A few notes:

  • By default, it will truncate on word boundary. To truncate the HTML string strictly at the specified length, pass in the :word_boundary => false option.

  • If the input HTML is nil, it will return an empty string.

  • The omission text's length does count toward the resulting string's length.

  • <script> tags will pass right through - they will not count toward the resulting string's length, or be truncated.

  • The default options are:

    • :length: 100
    • :omission: '...'
    • :word_boundary: /\S/

You may also set global configuration options. For example, place the following on application boot, something like config/initializers/truncate_html.rb

TruncateHtml.configure do |config|
  config.length        = 50
  config.omission      = '...(continued)'
end

If you really want, you can even set a custom word boundary regexp. For example, to truncate at the end of the nearest sentence:

TruncateHtml.configure do |config|
  config.word_boundary = /\S[\.\?\!]/
end

You can also truncate the html at a specific point not based on length but content. To do that, place the :break_token in your source. This allows the truncation to be data driven, breaking after a leading paragraph or sentence. If the :break_token is in your content before the specified :length, :length will be ignored and the content truncated at :break_token If the :break_token is in your content after the specified :length, :break_token will be ignored and the content truncated at :length

TruncateHtml.configure do |config|
  config.break_token = '<!-- truncate -->'
end

Installation

The latest gem version for the Rails 2.x series is 0.3.2. To use truncate_html on a Rails 2 app, please install the 0.3.2 version:

gem install truncate_html -v 0.3.2

For Rails 3, use the latest truncate_html:

gem install truncate_html

Issues or Suggestions

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

Testing

bundle
rake

All green? Go hack.

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

Thanks to all the contributors!

truncate_html's People

Contributors

hgmnz avatar parndt avatar ghazel avatar arturdryomov avatar bcardarella avatar csquared avatar dmitry avatar dougjohnston avatar tinygrasshopper avatar jbirdjavi avatar coneybeare avatar zapnap avatar halida avatar

Watchers

James Cloos avatar Gleb 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.