/auto_excerpt

Ruby gem to generate excerpts from html formatted text. Great for blogs.

Primary LanguageRubyMIT LicenseMIT

AutoExcerpt

[sudo] gem install auto_excerpt

Creates excerpts of html formatted text.

AutoExcerpt.new("<span>This is <strong>some</strong> fancy html formatted text homie</span>", {:words => 5}) 
    #   => "<span>This is <strong>some</strong> fancy html...</span>"

Features

  • There are 4 different ways to limit the length of an excerpt: characters, words, sentences, paragraphs
  • If the excerpt would be shorter than the limit that is set, the entire text will be shown.
  • If limiting by characters the gem will ensure that the excerpt does not cutoff in the middle of a word.
  • HTML can be stripped. You can also set specific tags that you don’t want stripped.
  • HTML tags are automatically closed.

Options

:characters
The number of characters to display from the text.
Default: 150 (does not need to be set to 0 if you choose another option)

If you need to be 100% accurate in your character count, then remove the :ending

AutoExcerpt.new("<h1>Hello World!</h1>", {:characters => 5, :ending => nil}) 
     # => <h1>Hello</h1>

:words, :sentences, :paragraphs
The number of [which] to display from the text.

:ending
The text that will be displayed at the end of the excerpt when generating the excerpt by length or words. Set to nil if you don’t want it.
Default: “…”

AutoExcerpt.new("This is cool stuff man!", :ending => ". Srsly!", :words => 3)
    # => "This is cool. Srsly!"

:strip_html
Strips HTML tags from the excerpt that is displayed.
Default: false

:allowed_tags
If using :strip_html then this setting will allow the listed tags to be shown.
default: []

AutoExcerpt.new("<p>This <em>is</em> some <strong>formatted</strong> html</p>", {:strip_html => true, :allowed_tags => %w(p em)})
    # => "<p>This <em>is</em> some formatted html</p>"

:strip_paragraphs
Strip all paragraph tags from the html.
Default: false

:skip_words, :skip_sentences, :skip_paragraphs
The number of [which] to skip at the beginning of the html when returned.
Default: 0

Command Line

new: v0.7.1 Run auto_excerpt from the command line with all of the options shown above!
$ auto_excerpt --words 10 "<p>Some html string</p>"

Help out on Github!

  • Fork the project.
  • Make your feature addition or bug fix.
  • Commit, do not mess with Rakefile, Version, or LICENSE.
  • Send me a pull request :)
  • Thanks for helping out!

Credits

This is based on the rss_auto_excerpt plugin for Textpattern by the great Rob Sable.

Copyright

Copyright © 2010 Kabari Hendrick. See LICENSE for details.