dejan / auto_html

Rails plugin for transforming urls to appropriate resource (image, link, YouTube, Vimeo video,...)

This URL has Read+Write access

name age message
file .gitignore Fri May 29 15:50:26 -0700 2009 Replaced rspec with test_unit and introduced fu... [dejan]
file CHANGELOG.rdoc Loading commit data...
file MIT-LICENCE Fri May 29 16:07:20 -0700 2009 renamed: LICENCE -> MIT-LICENCE [dejan]
file README.rdoc
file Rakefile Sat Sep 05 12:33:14 -0700 2009 gemspec fix [dejan]
file VERSION.yml
file auto_html.gemspec
directory lib/
directory rails/ Sat Sep 05 12:24:53 -0700 2009 Gemified [dejan]
directory test/
README.rdoc

auto_html

Rails plugin for embedding rich content by converting provided URL’s to appropriate HTML blocks for links, images, youtube, vimeo, google video and more.

Synopsis

auto_html plugin is the perfect choice if you don’t want to bother visitors with rich HTML editor or markup code, but you still want to allow them to embed video, images, links and more on your site, purely by pasting URL.

Let’s say you have model Comment with attribute body. Create another column in table Comments called body_html. Now have something like this:

  class Comment < ActiveRecord::Base
    auto_html_for :body do
      html_escape
      image
      youtube :width => 400, :height => 250
      link :target => "_blank", :rel => "nofollow"
      simple_format
    end
  end

… and you’ll have this behaviour:

  Comment.create(:body => 'Hey check out this cool video: http://www.youtube.com/watch?v=WdsGihou8J4')
  => #<Comment id: 123, body: 'Hey check out this cool video: http://www.youtube.com/watch?v=WdsGihou8J4', body_html: '<p>Hey check out this cool video: <object height="250" width="400"><param name="movie" value="http://www.youtube.com/v/WdsGihou8J4" /><param name="wmode" value="transparent" /><embed src="http://www.youtube.com/v/WdsGihou8J4" type="application/x-shockwave-flash" height="250" wmode="transparent" width="400"></embed></object></p>'>

Note that order of invoking filters is important, ie. you want html_escape as first and link amongst last, so that it doesn’t transform youtube URL to plain link.

Now all you have to do is to display it in template without escaping, since plugin took care of that:

  <% for comment in @comments %>
     <li><%= comment.body_html %></li>
  <% end %>

If you need to display preview, no problem. Have something like this as action in your controller:

  def preview
    comment = Comment.new(params[:comment])
    comment.auto_html_prepare
    render :text => comment.body_html
  end

Plugin is highly customizable, and you can easily create new filters that will transform user input any way you like. For instance, this is the image filter that comes bundled with plugin:

  AutoHtml.add_filter(:image) do |text|
    text.gsub(/http:\/\/.+\.(jpg|jpeg|bmp|gif|png)(\?\S+)?/i) do |match|
      %|<img src="#{match}" alt=""/>|
    end
  end

Bundled filters

For filter list and options they support check: github.com/dejan/auto_html/tree/master/lib/auto_html/filters

Install

As a gem

To enable the library in your Rails 2.1 (or greater) project, use the gem configuration method in "config/environment.rb"

  Rails::Initializer.run do |config|
    config.gem 'dejan-auto_html', :version => '~> 1.1.0', :lib => 'auto_html', :source => 'http://gems.github.com'
  end

As a Rails plugin

  script/plugin install git://github.com/dejan/auto_html.git

Links:

Credits

Author:Dejan Simic
Contributor:Claudio Perez Gamayo