GithubHelp home page GithubHelp logo

erikeliason / auto_html Goto Github PK

View Code? Open in Web Editor NEW

This project forked from dejan/auto_html

1.0 1.0 0.0 351 KB

Rails extension for transforming URLs to appropriate resource (image, link, YouTube, Vimeo video,...)

Home Page: http://auto-html.rors.org

License: MIT License

auto_html's Introduction

auto_html Build Status

auto_html is a Rails extension for transforming URLs to appropriate resource (image, link, YouTube, Vimeo video,...). It's 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. Check out the live demo.

Example usage

Transforming string with text and URLs is done with auto_html method:

include AutoHtml

auto_html('Hey! Checkout out: http://vukajlija.com') { simple_format; link(:target => 'blank') }
=> "<p>Hey! Checkout out: <a href='http://vukajlija.com' target='blank'>http://vukajlija.com</a></p>"

You'll probably have user input stored in model, so it's a good place to automate and even store this conversion for performance reason. This is done with auto_html_for method. Let's say you have model Comment with attribute body. Create another column in table Comments called body_html (again, this is optional but recommended for performance reasons). 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: '<p>Hey check out this cool video: <iframe class="youtube-player" type="text/html" width="587" height="350" src="http://www.youtube.com/embed/WdsGihou8J4" frameborder="0"> <br /></iframe></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])
  render :text => comment.body_html
end

AutoHtml 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

Non-ActiveRecord models

AutoHtml uses standard ActiveModel API, which means that you can include AutoHtmlFor module (that automates transformation of the field) in any non-ActiveRecord model that uses ActiveModel. Here's working mongoid example:

class Post
  include Mongoid::Document
  include AutoHtmlFor

  field :body

  auto_html_for :body do
    simple_format
    link
  end
end

Rake and Capistrano tasks

AutoHtml has a Rake task for rebuilding cached in DB column values Usage: rake auto_html:rebuild CLASS=[your model] Where [your model] is the name of model which values you want to rebuild.

If you want to run it on remote server, just add this to your deploy.rb:

require 'auto_html/capistrano'

Now you can run cap auto_html:rebuild CLASS=[your_model].

Bundled filters

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

Install

Specify the gem in Gemfile of the project

gem "auto_html"

Credits

Author: Dejan Simic
Contributors: https://github.com/dejan/auto_html/contributors

auto_html's People

Contributors

dejan avatar crossblaim avatar danielweinmann avatar mattpolito avatar subicura avatar henningthies avatar kirs avatar ademange-legion avatar ebeigarts avatar hinrik avatar joaomilho avatar rbq avatar

Stargazers

Erik Eliason avatar

Watchers

Erik Eliason 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.