marcosinger-auto_html 1.3.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +94 -0
- data/Rakefile +30 -0
- data/lib/auto_html.rb +11 -0
- data/lib/auto_html/auto_html_for.rb +46 -0
- data/lib/auto_html/base.rb +11 -0
- data/lib/auto_html/builder.rb +21 -0
- data/lib/auto_html/filter.rb +22 -0
- data/lib/auto_html/filters/dailymotion.rb +6 -0
- data/lib/auto_html/filters/google_video.rb +6 -0
- data/lib/auto_html/filters/html_escape.rb +9 -0
- data/lib/auto_html/filters/image.rb +5 -0
- data/lib/auto_html/filters/link.rb +5 -0
- data/lib/auto_html/filters/metacafe.rb +13 -0
- data/lib/auto_html/filters/sanitize.rb +6 -0
- data/lib/auto_html/filters/simple_format.rb +12 -0
- data/lib/auto_html/filters/vimeo.rb +15 -0
- data/lib/auto_html/filters/vimeo_image.rb +13 -0
- data/lib/auto_html/filters/youtube.rb +12 -0
- data/lib/auto_html/filters/youtube_image.rb +5 -0
- data/lib/auto_html/filters/youtube_js_api.rb +6 -0
- data/test/fixture_setup.rb +9 -0
- data/test/fixtures/database.yml +5 -0
- data/test/fixtures/schema.rb +15 -0
- data/test/functional/auto_html_for_options_test.rb +45 -0
- data/test/functional/auto_html_for_test.rb +40 -0
- data/test/functional/filter_test.rb +27 -0
- data/test/test_helper.rb +14 -0
- data/test/unit/auto_html_test.rb +36 -0
- data/test/unit/filters/dailymotion_test.rb +20 -0
- data/test/unit/filters/google_video_test.rb +15 -0
- data/test/unit/filters/html_escape_test.rb +15 -0
- data/test/unit/filters/image_test.rb +45 -0
- data/test/unit/filters/link_test.rb +52 -0
- data/test/unit/filters/metacafe_test.rb +29 -0
- data/test/unit/filters/sanitize_test.rb +36 -0
- data/test/unit/filters/simple_format_test.rb +15 -0
- data/test/unit/filters/vimeo_image_test.rb +30 -0
- data/test/unit/filters/vimeo_test.rb +45 -0
- data/test/unit/filters/youtube_image_test.rb +9 -0
- data/test/unit/filters/youtube_js_api_test.rb +30 -0
- data/test/unit/filters/youtube_test.rb +35 -0
- data/test/unit/unit_test_helper.rb +3 -0
- metadata +111 -0
data/README.md
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
auto_html
|
2
|
+
=========
|
3
|
+
|
4
|
+
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](http://auto-html.rors.org).
|
5
|
+
|
6
|
+
|
7
|
+
## Example usage
|
8
|
+
|
9
|
+
Transforming string with text and URLs is done with *auto_html* method:
|
10
|
+
|
11
|
+
include AutoHtml
|
12
|
+
|
13
|
+
auto_html('Hey! Checkout out: http://vukajlija.com') { simple_format; link(:target => 'blank') }
|
14
|
+
=> "<p>Hey! Checkout out: <a href='http://vukajlija.com' target='blank'>http://vukajlija.com</a></p>"
|
15
|
+
|
16
|
+
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:
|
17
|
+
|
18
|
+
class Comment < ActiveRecord::Base
|
19
|
+
auto_html_for :body do
|
20
|
+
html_escape
|
21
|
+
image
|
22
|
+
youtube(:width => 400, :height => 250)
|
23
|
+
link :target => "_blank", :rel => "nofollow"
|
24
|
+
simple_format
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
... and you'll have this behaviour:
|
29
|
+
|
30
|
+
Comment.create(:body => 'Hey check out this cool video: http://www.youtube.com/watch?v=WdsGihou8J4')
|
31
|
+
=> #<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>'>
|
32
|
+
|
33
|
+
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.
|
34
|
+
|
35
|
+
|
36
|
+
Now all you have to do is to display it in template without escaping, since plugin took care of that:
|
37
|
+
|
38
|
+
<% for comment in @comments %>
|
39
|
+
<li><%= comment.body_html %></li>
|
40
|
+
<% end %>
|
41
|
+
|
42
|
+
|
43
|
+
If you need to display preview, no problem. Have something like this as action in your controller:
|
44
|
+
|
45
|
+
def preview
|
46
|
+
comment = Comment.new(params[:comment])
|
47
|
+
render :text => comment.body_html
|
48
|
+
end
|
49
|
+
|
50
|
+
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:
|
51
|
+
|
52
|
+
AutoHtml.add_filter(:image) do |text|
|
53
|
+
text.gsub(/http:\/\/.+\.(jpg|jpeg|bmp|gif|png)(\?\S+)?/i) do |match|
|
54
|
+
%|<img src="#{match}" alt=""/>|
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
|
59
|
+
|
60
|
+
## Bundled filters
|
61
|
+
|
62
|
+
For filter list and options they support check: <http://github.com/dejan/auto_html/tree/master/lib/auto_html/filters>
|
63
|
+
|
64
|
+
|
65
|
+
## Install
|
66
|
+
|
67
|
+
### Important note on versions
|
68
|
+
|
69
|
+
As from version 1.2.0 auto_html uses Rails' engine for discovering links. There are some bugs with that engine in versions under Rails 2.3.2. so it's recommended you use auto_html 1.1.2 in that case, since internal engine is used in that version.
|
70
|
+
|
71
|
+
for Rails <= 2.3.1 use auto_html 1.1.2<br/>
|
72
|
+
for Rails >= 2.3.2 use the latest auto_html
|
73
|
+
|
74
|
+
### As a gem
|
75
|
+
|
76
|
+
To enable the library in your Rails 2.1-2.3 project, use the gem configuration method in "config/environment.rb"
|
77
|
+
|
78
|
+
Rails::Initializer.run do |config|
|
79
|
+
config.gem 'auto_html'
|
80
|
+
end
|
81
|
+
|
82
|
+
In Rails 3.0 specify the gem in your Gemfile
|
83
|
+
|
84
|
+
gem "auto_html"
|
85
|
+
|
86
|
+
### As a Rails plugin
|
87
|
+
|
88
|
+
script/plugin install git://github.com/dejan/auto_html.git
|
89
|
+
|
90
|
+
|
91
|
+
## Credits
|
92
|
+
|
93
|
+
Author: [Dejan Simic](http://github.com/dejan)<br/>
|
94
|
+
Contributors: [Claudio Perez Gamayo](http://github.com/crossblaim), [Matt Polito](http://github.com/mattpolito), [Ryan Heneise](http://github.com/mysmallidea), [Caleb Wright](http://github.com/fabrikagency), [Derrick Camerino](https://github.com/robustdj)
|
data/Rakefile
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'rake/testtask'
|
2
|
+
|
3
|
+
desc 'Default: run tests'
|
4
|
+
task :default => :test
|
5
|
+
|
6
|
+
desc 'Test AutoHtml'
|
7
|
+
Rake::TestTask.new(:test) do |t|
|
8
|
+
t.pattern = 'test/**/*_test.rb'
|
9
|
+
end
|
10
|
+
|
11
|
+
desc 'Test with recent versions of Rails'
|
12
|
+
task :test_with_recent do
|
13
|
+
versions = ['2.3.8', '3.0.3']
|
14
|
+
versions.each do |v|
|
15
|
+
puts "\n###### TESTING WITH RAILS #{v}"
|
16
|
+
ENV['RAILS_VERSION'] = v
|
17
|
+
Rake::Task['test'].execute
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
desc 'Test with versions of Rails available on the system'
|
22
|
+
task :test_with_installed do
|
23
|
+
versions = `gem list rails | grep rails`.gsub("rails (", "").chop.split(', ')
|
24
|
+
exclude = []
|
25
|
+
(versions-exclude).each do |v|
|
26
|
+
puts "\n###### TESTING WITH RAILS #{v}"
|
27
|
+
ENV['RAILS_VERSION'] = v
|
28
|
+
Rake::Task['test'].execute
|
29
|
+
end
|
30
|
+
end
|
data/lib/auto_html.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
%w(base filter builder auto_html_for).each do |f|
|
2
|
+
require File.expand_path("../auto_html/#{f}", __FILE__)
|
3
|
+
end
|
4
|
+
|
5
|
+
Dir["#{File.dirname(__FILE__) + '/auto_html/filters'}/**/*"].each do |filter|
|
6
|
+
require "#{filter}"
|
7
|
+
end
|
8
|
+
|
9
|
+
if defined? ActiveRecord::Base
|
10
|
+
ActiveRecord::Base.send :include, AutoHtmlFor
|
11
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module AutoHtmlFor
|
2
|
+
|
3
|
+
# default options that can be overridden on the global level
|
4
|
+
@@auto_html_for_options = {
|
5
|
+
:htmlized_attribute_suffix => '_html'
|
6
|
+
}
|
7
|
+
|
8
|
+
mattr_reader :auto_html_for_options
|
9
|
+
|
10
|
+
def self.included(base)
|
11
|
+
base.extend(ClassMethods)
|
12
|
+
end
|
13
|
+
|
14
|
+
module ClassMethods
|
15
|
+
def auto_html_for(raw_attrs, &proc)
|
16
|
+
include AutoHtmlFor::InstanceMethods
|
17
|
+
before_save :auto_html_prepare
|
18
|
+
|
19
|
+
define_method("auto_html_prepare") do
|
20
|
+
auto_html_methods = self.methods.select { |m| m=~/^auto_html_prepare_/ }
|
21
|
+
auto_html_methods.each do |method|
|
22
|
+
self.send(method)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
raw_attrs = { raw_attrs => AutoHtmlFor.auto_html_for_options[:htmlized_attribute_suffix] } unless raw_attrs.is_a?(Hash)
|
27
|
+
|
28
|
+
raw_attrs.each do |raw_attr, suffix|
|
29
|
+
define_method("#{raw_attr}#{suffix}=") do |val|
|
30
|
+
write_attribute("#{raw_attr}#{suffix}", val)
|
31
|
+
end
|
32
|
+
define_method("#{raw_attr}#{suffix}") do
|
33
|
+
read_attribute("#{raw_attr}#{suffix}") || send("auto_html_prepare_#{raw_attr}#{suffix}")
|
34
|
+
end
|
35
|
+
define_method("auto_html_prepare_#{raw_attr}#{suffix}") do
|
36
|
+
self.send(raw_attr.to_s + suffix + "=",
|
37
|
+
auto_html(self.send(raw_attr), &proc))
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
module InstanceMethods
|
44
|
+
include AutoHtml
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module AutoHtml
|
2
|
+
class Builder
|
3
|
+
@@filters = {}
|
4
|
+
|
5
|
+
def initialize(text)
|
6
|
+
@text = text.dup
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.add_filter(name, &block)
|
10
|
+
filter = Filter.new(block)
|
11
|
+
@@filters.merge!(name => filter)
|
12
|
+
src = %|
|
13
|
+
def #{name}(options = {})
|
14
|
+
@text = @@filters["#{name}".to_sym].apply(@text, options)
|
15
|
+
end
|
16
|
+
|
|
17
|
+
class_eval src, __FILE__, __LINE__
|
18
|
+
filter
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module AutoHtml
|
2
|
+
class Filter
|
3
|
+
def initialize(block)
|
4
|
+
@block = block
|
5
|
+
@options = nil
|
6
|
+
end
|
7
|
+
|
8
|
+
def with(options, &block)
|
9
|
+
@options = options
|
10
|
+
@block = block
|
11
|
+
end
|
12
|
+
|
13
|
+
def apply(text, options = {})
|
14
|
+
_options = @options && @options.merge(options)
|
15
|
+
if _options
|
16
|
+
@block.call(text.to_s.dup, _options)
|
17
|
+
else
|
18
|
+
@block.call(text.to_s.dup)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,6 @@
|
|
1
|
+
AutoHtml.add_filter(:dailymotion).with(:width => 480, :height => 360) do |text, options|
|
2
|
+
text.gsub(/http:\/\/www\.dailymotion\.com.*\/video\/(.+)_*/) do
|
3
|
+
video_id = $1
|
4
|
+
%{<object type="application/x-shockwave-flash" data="http://www.dailymotion.com/swf/#{video_id}&related=0" width="#{options[:width]}" height="#{options[:height]}"><param name="movie" value="http://www.dailymotion.com/swf/#{video_id}&related=0"></param><param name="allowFullScreen" value="true"></param><param name="allowScriptAccess" value="always"></param><a href="http://www.dailymotion.com/video/#{video_id}?embed=1"><img src="http://www.dailymotion.com/thumbnail/video/#{video_id}" width="#{options[:width]}" height="#{options[:height]}"/></a></object>}
|
5
|
+
end
|
6
|
+
end
|
@@ -0,0 +1,6 @@
|
|
1
|
+
AutoHtml.add_filter(:google_video).with(:width => 650, :height => 391) do |text, options|
|
2
|
+
text.gsub(/http:\/\/video\.google\.com\/videoplay\?docid\=(-?[0-9]*)[&\w;=\+_\-]*/) do
|
3
|
+
docid = $1
|
4
|
+
%{<object width="#{options[:width]}" height="#{options[:height]}"><param name="movie" value="http://video.google.com/googleplayer.swf?docid=#{docid}&hl=en&fs=true"></param><param name="wmode" value="transparent"></param><embed src="http://video.google.com/googleplayer.swf?docid=#{docid}" type="application/x-shockwave-flash" wmode="transparent" width="#{options[:width]}" height="#{options[:height]}"></embed></object>}
|
5
|
+
end
|
6
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
AutoHtml.add_filter(:metacafe).with(:width => 440, :height => 272, :show_stats => false, :autoplay => false) do |text, options|
|
2
|
+
text.gsub(/http:\/\/www\.metacafe\.com\/watch\/([A-Za-z0-9._%-]*)\/([A-Za-z0-9._%-]*)(\/)?/) do
|
3
|
+
metacafe_id = $1
|
4
|
+
metacafe_slug = $2
|
5
|
+
width = options[:width]
|
6
|
+
height = options[:height]
|
7
|
+
show_stats = options[:show_stats] ? "showStats=yes" : "showStats=no"
|
8
|
+
autoplay = options[:autoplay] ? "autoPlay=yes" : "autoPlay=no"
|
9
|
+
flash_vars = [show_stats, autoplay].join("|")
|
10
|
+
|
11
|
+
%{<div style="background:#000000;width:#{width}px;height:#{height}px"><embed flashVars="playerVars=#{flash_vars}" src="http://www.metacafe.com/fplayer/#{metacafe_id}/#{metacafe_slug}.swf" width="#{width}" height="#{height}" wmode="transparent" allowFullScreen="true" allowScriptAccess="always" name="Metacafe_#{metacafe_id}" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash"></embed></div>}
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'action_view'
|
2
|
+
|
3
|
+
AutoHtml.add_filter(:simple_format).with({}) do |text, html_options|
|
4
|
+
args = [text, {}, {:sanitize => false}]
|
5
|
+
begin
|
6
|
+
ActionView::Base.new.simple_format(*args)
|
7
|
+
rescue ArgumentError
|
8
|
+
# Rails 2 support
|
9
|
+
args.pop
|
10
|
+
retry
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
AutoHtml.add_filter(:vimeo).with(:width => 440, :height => 248, :show_title => false, :show_byline => false, :show_portrait => false) do |text, options|
|
2
|
+
text.gsub(/http:\/\/(www.)?vimeo\.com\/([A-Za-z0-9._%-]*)((\?|#)\S+)?/) do
|
3
|
+
vimeo_id = $2
|
4
|
+
width = options[:width]
|
5
|
+
height = options[:height]
|
6
|
+
show_title = "title=0" unless options[:show_title]
|
7
|
+
show_byline = "byline=0" unless options[:show_byline]
|
8
|
+
show_portrait = "portrait=0" unless options[:show_portrait]
|
9
|
+
frameborder = options[:frameborder] || 0
|
10
|
+
query_string_variables = [show_title, show_byline, show_portrait].compact.join("&")
|
11
|
+
query_string = "?" + query_string_variables unless query_string_variables.empty?
|
12
|
+
|
13
|
+
%{<iframe src="http://player.vimeo.com/video/#{vimeo_id}#{query_string}" width="#{width}" height="#{height}" frameborder="#{frameborder}"></iframe>}
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'hpricot'
|
2
|
+
require 'open-uri'
|
3
|
+
|
4
|
+
AutoHtml.add_filter(:vimeo_image).with(:size => :medium) do |text, options|
|
5
|
+
text.gsub(/http:\/\/(www.)?vimeo\.com\/([A-Za-z0-9._%-]*)((\?|#)\S+)?/) do
|
6
|
+
vimeo_id = $2
|
7
|
+
size = options[:size].to_s
|
8
|
+
vimeo_xml = open("http://vimeo.com/api/v2/video/#{vimeo_id}.xml") { |v| Hpricot.XML(v) }
|
9
|
+
|
10
|
+
(vimeo_xml/"thumbnail_#{size}").inner_html.blank? ? (vimeo_xml/"thumbnail_medium").inner_html : (vimeo_xml/"thumbnail_#{size}").inner_html
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
@@ -0,0 +1,12 @@
|
|
1
|
+
AutoHtml.add_filter(:youtube).with(:width => 390, :height => 250, :frameborder => 0) do |text, options|
|
2
|
+
regex = /http:\/\/(www.)?youtube\.com\/watch\?v=([A-Za-z0-9._%-]*)(\&\S+)?/
|
3
|
+
text.gsub(regex) do
|
4
|
+
youtube_id = $2
|
5
|
+
width = options[:width]
|
6
|
+
height = options[:height]
|
7
|
+
frameborder = options[:frameborder]
|
8
|
+
%{<iframe class="youtube-player" type="text/html" width="#{width}" height="#{height}" src="http://www.youtube.com/embed/#{youtube_id}" frameborder="#{frameborder}">
|
9
|
+
</iframe>}
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
@@ -0,0 +1,6 @@
|
|
1
|
+
AutoHtml.add_filter(:youtube_js_api).with(:width => 390, :height => 250) do |text, options|
|
2
|
+
text.gsub(/http:\/\/(www.)?youtube\.com\/watch\?v=([A-Za-z0-9._%-]*)(\&\S+)?/) do
|
3
|
+
youtube_id = $2
|
4
|
+
%{<object width="#{options[:width]}" height="#{options[:height]}"><param name="movie" value="http://www.youtube.com/v/#{youtube_id}?enablejsapi=1&playerapiid=ytplayer"></param><param name="wmode" value="transparent"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/#{youtube_id}?enablejsapi=1&playerapiid=ytplayer" type="application/x-shockwave-flash" wmode="transparent" width="#{options[:width]}" height="#{options[:height]}" allowscriptaccess="always"></embed></object>}
|
5
|
+
end
|
6
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
module FixtureSetup
|
2
|
+
def setup
|
3
|
+
fixtures_dir = File.dirname(__FILE__) + '/fixtures'
|
4
|
+
connections = YAML.load_file("#{fixtures_dir}/database.yml")
|
5
|
+
ActiveRecord::Base.establish_connection(connections['sqlite3'])
|
6
|
+
ActiveRecord::Migration.verbose = false
|
7
|
+
load "#{fixtures_dir}/schema.rb"
|
8
|
+
end
|
9
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
ActiveRecord::Schema.define do
|
2
|
+
create_table "articles", :force => true do |t|
|
3
|
+
t.column "title", :string
|
4
|
+
t.column "body", :text
|
5
|
+
t.column "body_source", :text
|
6
|
+
t.column "body_image", :text
|
7
|
+
t.column "created_at", :datetime
|
8
|
+
t.column "updated_at", :datetime
|
9
|
+
end
|
10
|
+
|
11
|
+
create_table "users", :force => true do |t|
|
12
|
+
t.column "name", :string
|
13
|
+
t.column "bio", :text
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require File.expand_path('../../test_helper', __FILE__)
|
2
|
+
require File.expand_path('../../fixture_setup', __FILE__)
|
3
|
+
|
4
|
+
class Article < ActiveRecord::Base
|
5
|
+
auto_html_for :body => "_htmlized" do
|
6
|
+
simple_format
|
7
|
+
end
|
8
|
+
|
9
|
+
auto_html_for :body => "_to_html" do
|
10
|
+
youtube
|
11
|
+
vimeo
|
12
|
+
end
|
13
|
+
|
14
|
+
auto_html_for :body => "_to_image" do
|
15
|
+
youtube_image
|
16
|
+
vimeo_image
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
class AutoHtmlForOptionsTest < Test::Unit::TestCase
|
21
|
+
include FixtureSetup
|
22
|
+
|
23
|
+
def test_transform_after_save
|
24
|
+
@article = Article.new(:body => 'Yo!')
|
25
|
+
assert_equal '<p>Yo!</p>', @article.body_htmlized
|
26
|
+
@article.save!
|
27
|
+
assert_equal '<p>Yo!</p>', @article.body_htmlized
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_create_thumb_for_youtube_in_a_new_field
|
31
|
+
@article = Article.new(:body => 'http://www.youtube.com/watch?v=O7aQtpVXYK4&feature=grec_index')
|
32
|
+
@article.save!
|
33
|
+
|
34
|
+
assert_equal %'<iframe class=\"youtube-player\" type=\"text/html\" width=\"390\" height=\"250\" src=\"http://www.youtube.com/embed/O7aQtpVXYK4\" frameborder=\"0\">\n</iframe>', @article.body_to_html
|
35
|
+
assert_equal 'http://i1.ytimg.com/vi/O7aQtpVXYK4/default.jpg', @article.body_to_image
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_create_thumb_for_vimeo_in_a_new_field
|
39
|
+
@article = Article.new(:body => 'http://vimeo.com/14074949')
|
40
|
+
@article.save!
|
41
|
+
|
42
|
+
assert_equal %'<iframe src=\"http://player.vimeo.com/video/14074949?title=0&byline=0&portrait=0\" width=\"440\" height=\"248\" frameborder=\"0\"></iframe>', @article.body_to_html
|
43
|
+
assert_equal 'http://b.vimeocdn.com/ts/937/359/93735969_200.jpg', @article.body_to_image
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require File.expand_path('../../test_helper', __FILE__)
|
2
|
+
require File.expand_path('../../fixture_setup', __FILE__)
|
3
|
+
|
4
|
+
class Article < ActiveRecord::Base
|
5
|
+
auto_html_for :body do
|
6
|
+
html_escape
|
7
|
+
youtube(:width => 400, :height => 250)
|
8
|
+
image
|
9
|
+
link(:target => "_blank", :rel => "nofollow")
|
10
|
+
simple_format
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class AutoHtmlForTest < Test::Unit::TestCase
|
15
|
+
include FixtureSetup
|
16
|
+
|
17
|
+
def test_transform_on_initialization
|
18
|
+
@article = Article.new(:body => 'Yo!')
|
19
|
+
assert_equal '<p>Yo!</p>', @article.body_html
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_transform_after_save
|
23
|
+
@article = Article.new(:body => 'Yo!')
|
24
|
+
@article.save!
|
25
|
+
assert_equal '<p>Yo!</p>', @article.body_html
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_transform_of_nil
|
29
|
+
@article = Article.new(:body => nil)
|
30
|
+
@article.save!
|
31
|
+
assert_equal '', @article.body_html
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_transform_after_update
|
35
|
+
@article = Article.create!(:body => 'Yo!')
|
36
|
+
@article.update_attributes(:body => 'http://vukajlija.com')
|
37
|
+
@article.save!
|
38
|
+
assert_equal '<p><a href="http://vukajlija.com" rel="nofollow" target="_blank">http://vukajlija.com</a></p>', @article.body_html
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require File.expand_path('../../test_helper', __FILE__)
|
2
|
+
require File.expand_path('../../fixture_setup', __FILE__)
|
3
|
+
|
4
|
+
AutoHtml.add_filter(:foo) do |text|
|
5
|
+
nil
|
6
|
+
end
|
7
|
+
|
8
|
+
AutoHtml.add_filter(:bar) do |text|
|
9
|
+
"bar"
|
10
|
+
end
|
11
|
+
|
12
|
+
class User < ActiveRecord::Base
|
13
|
+
auto_html_for :bio do
|
14
|
+
foo
|
15
|
+
foo
|
16
|
+
bar
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
class FilterTest < Test::Unit::TestCase
|
21
|
+
include FixtureSetup
|
22
|
+
|
23
|
+
def test_transform_after_save
|
24
|
+
@article = User.new(:bio => 'in progress...')
|
25
|
+
assert_equal 'bar', @article.bio_html
|
26
|
+
end
|
27
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
|
3
|
+
rails_version = ENV['RAILS_VERSION']
|
4
|
+
if rails_version
|
5
|
+
gem "activerecord", rails_version
|
6
|
+
gem "actionpack", rails_version
|
7
|
+
end
|
8
|
+
|
9
|
+
require 'test/unit'
|
10
|
+
require 'active_record'
|
11
|
+
require 'active_support/core_ext/class'
|
12
|
+
|
13
|
+
require File.dirname(__FILE__) + '/../init'
|
14
|
+
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require File.expand_path('../unit_test_helper', __FILE__)
|
2
|
+
|
3
|
+
|
4
|
+
class AutoHtmlTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
def test_should_be_nil_no_filters_provided
|
7
|
+
result = auto_html("Hey check out my blog => http://rors.org") { }
|
8
|
+
assert_nil result
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_should_apply_simple_format_filter
|
12
|
+
result = auto_html("Hey check out my blog => http://rors.org") { simple_format }
|
13
|
+
assert_equal "<p>Hey check out my blog => http://rors.org</p>", result
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_should_apply_simple_format_and_image_filter
|
17
|
+
result = auto_html("Check the logo: http://rors.org/images/rails.png") { simple_format; image }
|
18
|
+
assert_equal '<p>Check the logo: <img src="http://rors.org/images/rails.png" alt=""/></p>', result
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_should_apply_simple_format_image_and_link_filter
|
22
|
+
result = auto_html("Check the logo: http://rors.org/images/rails.png. Visit: http://rubyonrails.org") { simple_format; image; link }
|
23
|
+
assert_equal '<p>Check the logo: <img src="http://rors.org/images/rails.png" alt=""/>. Visit: <a href="http://rubyonrails.org">http://rubyonrails.org</a></p>', result
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_should_return_blank_if_input_is_blank
|
27
|
+
result = auto_html("") { simple_format; image; link }
|
28
|
+
assert_equal "", result
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_should_not_apply_simple_format_if_input_is_nil
|
32
|
+
result = auto_html(nil) { simple_format; image; link }
|
33
|
+
assert_equal "", result
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require File.expand_path('../../unit_test_helper', __FILE__)
|
2
|
+
|
3
|
+
class DailyMotionTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_transform
|
6
|
+
result = auto_html("http://www.dailymotion.com/en/featured/video/xag4p2_tempsmorttv-episode-01_shortfilms") { dailymotion }
|
7
|
+
assert_equal '<object type="application/x-shockwave-flash" data="http://www.dailymotion.com/swf/xag4p2_tempsmorttv-episode-01_shortfilms&related=0" width="480" height="360"><param name="movie" value="http://www.dailymotion.com/swf/xag4p2_tempsmorttv-episode-01_shortfilms&related=0"></param><param name="allowFullScreen" value="true"></param><param name="allowScriptAccess" value="always"></param><a href="http://www.dailymotion.com/video/xag4p2_tempsmorttv-episode-01_shortfilms?embed=1"><img src="http://www.dailymotion.com/thumbnail/video/xag4p2_tempsmorttv-episode-01_shortfilms" width="480" height="360"/></a></object>', result
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_transform_with_tweaked_width
|
11
|
+
result = auto_html("http://www.dailymotion.com/related/x9cyf6/video/x9tinl_happy-tree-friends-as-you-wish-part_fun") { dailymotion :width => 500 }
|
12
|
+
assert_equal '<object type="application/x-shockwave-flash" data="http://www.dailymotion.com/swf/x9tinl_happy-tree-friends-as-you-wish-part_fun&related=0" width="500" height="360"><param name="movie" value="http://www.dailymotion.com/swf/x9tinl_happy-tree-friends-as-you-wish-part_fun&related=0"></param><param name="allowFullScreen" value="true"></param><param name="allowScriptAccess" value="always"></param><a href="http://www.dailymotion.com/video/x9tinl_happy-tree-friends-as-you-wish-part_fun?embed=1"><img src="http://www.dailymotion.com/thumbnail/video/x9tinl_happy-tree-friends-as-you-wish-part_fun" width="500" height="360"/></a></object>', result
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_transform_with_options
|
16
|
+
result = auto_html("http://www.dailymotion.com/video/xakv5i") { dailymotion(:width => 500, :height => 300) }
|
17
|
+
assert_equal '<object type="application/x-shockwave-flash" data="http://www.dailymotion.com/swf/xakv5i&related=0" width="500" height="300"><param name="movie" value="http://www.dailymotion.com/swf/xakv5i&related=0"></param><param name="allowFullScreen" value="true"></param><param name="allowScriptAccess" value="always"></param><a href="http://www.dailymotion.com/video/xakv5i?embed=1"><img src="http://www.dailymotion.com/thumbnail/video/xakv5i" width="500" height="300"/></a></object>', result
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require File.expand_path('../../unit_test_helper', __FILE__)
|
2
|
+
|
3
|
+
class GoogleVideoTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_transform
|
6
|
+
result = auto_html("http://video.google.com/videoplay?docid=-7533500868750350977&ei=Xin8Sc-KBYeg2ALXkKymAQ&q=office+space&emb=1") { google_video }
|
7
|
+
assert_equal '<object width="650" height="391"><param name="movie" value="http://video.google.com/googleplayer.swf?docid=-7533500868750350977&hl=en&fs=true"></param><param name="wmode" value="transparent"></param><embed src="http://video.google.com/googleplayer.swf?docid=-7533500868750350977" type="application/x-shockwave-flash" wmode="transparent" width="650" height="391"></embed></object>', result
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_transform_with_options
|
11
|
+
result = auto_html("http://video.google.com/videoplay?docid=7442132741322615356") { google_video(:width => 500, :height => 300) }
|
12
|
+
assert_equal '<object width="500" height="300"><param name="movie" value="http://video.google.com/googleplayer.swf?docid=7442132741322615356&hl=en&fs=true"></param><param name="wmode" value="transparent"></param><embed src="http://video.google.com/googleplayer.swf?docid=7442132741322615356" type="application/x-shockwave-flash" wmode="transparent" width="500" height="300"></embed></object>', result
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require File.expand_path('../../unit_test_helper', __FILE__)
|
2
|
+
|
3
|
+
class HtmlEscapeTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_trasform
|
6
|
+
result = auto_html("<script>alert(0)</script>") { html_escape }
|
7
|
+
assert_equal "<script>alert(0)</script>", result
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_trasform2
|
11
|
+
result = auto_html("<div>test</div>") { html_escape }
|
12
|
+
assert_equal "<div>test</div>", result
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require File.expand_path('../../unit_test_helper', __FILE__)
|
2
|
+
|
3
|
+
class ImageTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_transform
|
6
|
+
result = auto_html('http://rors.org/images/rails.png') { image }
|
7
|
+
assert_equal '<img src="http://rors.org/images/rails.png" alt=""/>', result
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_dont_transform
|
11
|
+
result = auto_html('http://blog.phusion.nl/2009/04/16/phusions-one-year-anniversary-gift-phusion-passenger-220/') { image }
|
12
|
+
assert_equal 'http://blog.phusion.nl/2009/04/16/phusions-one-year-anniversary-gift-phusion-passenger-220/', result
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_transform2
|
16
|
+
result = auto_html('http://farm4.static.flickr.com/3459/3270173112_5099d3d730.jpg') { image }
|
17
|
+
assert_equal '<img src="http://farm4.static.flickr.com/3459/3270173112_5099d3d730.jpg" alt=""/>', result
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_transform3
|
21
|
+
result = auto_html('http://www.lockhartfineart.com/images/Rio_Grande_Frost.JPG') { image }
|
22
|
+
assert_equal '<img src="http://www.lockhartfineart.com/images/Rio_Grande_Frost.JPG" alt=""/>', result
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_transform4
|
26
|
+
result = auto_html('http://www.lockhartfineart.com/images/Rio_Grande_Frost.JPG') { image }
|
27
|
+
assert_equal '<img src="http://www.lockhartfineart.com/images/Rio_Grande_Frost.JPG" alt=""/>', result
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_transform5
|
31
|
+
result = auto_html('http://farm4.static.flickr.com/3664/3512431377_71b8d002ef.jpg?v=0') { image }
|
32
|
+
assert_equal '<img src="http://farm4.static.flickr.com/3664/3512431377_71b8d002ef.jpg?v=0" alt=""/>', result
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_transform6
|
36
|
+
result = auto_html('Do you like this logo http://rors.org/images/rails.png? Yeah?') { image }
|
37
|
+
assert_equal 'Do you like this logo <img src="http://rors.org/images/rails.png" alt=""/>? Yeah?', result
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_transform7
|
41
|
+
result = auto_html('http://tbn3.google.com/images?q=tbn:vS-jtEi9Xc8K6M:http://upload.wikimedia.org/wikipedia/commons/b/ba/Potturinn.jpeg') { image }
|
42
|
+
assert_equal '<img src="http://tbn3.google.com/images?q=tbn:vS-jtEi9Xc8K6M:http://upload.wikimedia.org/wikipedia/commons/b/ba/Potturinn.jpeg" alt=""/>', result
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require File.expand_path('../../unit_test_helper', __FILE__)
|
2
|
+
|
3
|
+
class LinkTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_transform
|
6
|
+
result = auto_html("http://vukajlija.com") { link }
|
7
|
+
assert_equal '<a href="http://vukajlija.com">http://vukajlija.com</a>', result
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_transform_with_the_slash_at_the_end
|
11
|
+
result = auto_html("http://github.com/") { link }
|
12
|
+
assert_equal '<a href="http://github.com/">http://github.com/</a>', result
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_transform_with_param
|
16
|
+
result = auto_html('http://example.com/abc?query=ruby') { link }
|
17
|
+
assert_equal '<a href="http://example.com/abc?query=ruby">http://example.com/abc?query=ruby</a>', result
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_transform_with_param_and_trailing_dot
|
21
|
+
result = auto_html('http://example.com/abc?query=ruby.') { link }
|
22
|
+
assert_equal '<a href="http://example.com/abc?query=ruby">http://example.com/abc?query=ruby</a>.', result
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_transform_with_anchor_and_trailing_dot
|
26
|
+
result = auto_html('http://example.com/example#id=123.12.') { link }
|
27
|
+
assert_equal '<a href="http://example.com/example#id=123.12">http://example.com/example#id=123.12</a>.', result
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_transform_with_params
|
31
|
+
url = 'http://www.youtube.com/watch?v=s5C5Zk4kobo&feature=related'
|
32
|
+
# uses auto_link instead raw to support both Rails 2 & 3
|
33
|
+
assert_equal(ActionView::Base.new.auto_link(url), auto_html(url) { link })
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_transform_with_commas
|
37
|
+
result = auto_html('http://www.dw-world.de/dw/article/0,,4708386,00.html?maca=ser-rss-ser-all-1494-rdf') { link }
|
38
|
+
assert_equal '<a href="http://www.dw-world.de/dw/article/0,,4708386,00.html?maca=ser-rss-ser-all-1494-rdf">http://www.dw-world.de/dw/article/0,,4708386,00.html?maca=ser-rss-ser-all-1494-rdf</a>', result
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_transform_complex_url
|
42
|
+
url = 'http://www.google.com/#q=nikola+tesla&ct=tesla09&oi=ddle&fp=Xmf0jJ9P_V0'
|
43
|
+
# uses auto_link instead raw to support both Rails 2 & 3
|
44
|
+
assert_equal(ActionView::Base.new.auto_link(url), auto_html(url) { link })
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_transform_with_options
|
48
|
+
result = auto_html("http://rors.org") { link :rel => "nofollow", :target => "_blank" }
|
49
|
+
assert_equal '<a href="http://rors.org" rel="nofollow" target="_blank">http://rors.org</a>', result
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require File.expand_path('../../unit_test_helper', __FILE__)
|
2
|
+
|
3
|
+
class MetaCafeTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_transform
|
6
|
+
result = auto_html("http://www.metacafe.com/watch/5440707/exclusive_tron_evolution_dev_diary_the_art_design") { metacafe }
|
7
|
+
assert_equal '<div style="background:#000000;width:440px;height:272px"><embed flashVars="playerVars=showStats=no|autoPlay=no" src="http://www.metacafe.com/fplayer/5440707/exclusive_tron_evolution_dev_diary_the_art_design.swf" width="440" height="272" wmode="transparent" allowFullScreen="true" allowScriptAccess="always" name="Metacafe_5440707" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash"></embed></div>', result
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_transform_with_dimensions
|
11
|
+
result = auto_html("http://www.metacafe.com/watch/5440707/exclusive_tron_evolution_dev_diary_the_art_design") { metacafe(:width => 500, :height => 300) }
|
12
|
+
assert_equal '<div style="background:#000000;width:500px;height:300px"><embed flashVars="playerVars=showStats=no|autoPlay=no" src="http://www.metacafe.com/fplayer/5440707/exclusive_tron_evolution_dev_diary_the_art_design.swf" width="500" height="300" wmode="transparent" allowFullScreen="true" allowScriptAccess="always" name="Metacafe_5440707" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash"></embed></div>', result
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_transform_with_show_stats
|
16
|
+
result = auto_html("http://www.metacafe.com/watch/5440707/exclusive_tron_evolution_dev_diary_the_art_design") { metacafe(:width => 500, :height => 300, :show_stats => true) }
|
17
|
+
assert_equal '<div style="background:#000000;width:500px;height:300px"><embed flashVars="playerVars=showStats=yes|autoPlay=no" src="http://www.metacafe.com/fplayer/5440707/exclusive_tron_evolution_dev_diary_the_art_design.swf" width="500" height="300" wmode="transparent" allowFullScreen="true" allowScriptAccess="always" name="Metacafe_5440707" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash"></embed></div>', result
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_transform_with_autoplay
|
21
|
+
result = auto_html("http://www.metacafe.com/watch/5440707/exclusive_tron_evolution_dev_diary_the_art_design") { metacafe(:width => 500, :height => 300, :autoplay => true) }
|
22
|
+
assert_equal '<div style="background:#000000;width:500px;height:300px"><embed flashVars="playerVars=showStats=no|autoPlay=yes" src="http://www.metacafe.com/fplayer/5440707/exclusive_tron_evolution_dev_diary_the_art_design.swf" width="500" height="300" wmode="transparent" allowFullScreen="true" allowScriptAccess="always" name="Metacafe_5440707" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash"></embed></div>', result
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_transform_with_options
|
26
|
+
result = auto_html("http://www.metacafe.com/watch/5440707/exclusive_tron_evolution_dev_diary_the_art_design") { metacafe(:width => 500, :height => 300, :show_stats => true, :autoplay => true) }
|
27
|
+
assert_equal '<div style="background:#000000;width:500px;height:300px"><embed flashVars="playerVars=showStats=yes|autoPlay=yes" src="http://www.metacafe.com/fplayer/5440707/exclusive_tron_evolution_dev_diary_the_art_design.swf" width="500" height="300" wmode="transparent" allowFullScreen="true" allowScriptAccess="always" name="Metacafe_5440707" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash"></embed></div>', result
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require File.expand_path('../../unit_test_helper', __FILE__)
|
2
|
+
|
3
|
+
class SanitizeTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_trasform
|
6
|
+
result = auto_html("<script>alert(0)</script>") { sanitize }
|
7
|
+
assert_equal "", result
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_trasform2
|
11
|
+
result = auto_html("<div>test</div>") { sanitize }
|
12
|
+
assert_equal "<div>test</div>", result
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_trasform3
|
16
|
+
result = auto_html("<div>test</div>") { sanitize :tags => %w(div) }
|
17
|
+
assert_equal "<div>test</div>", result
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_trasform4
|
21
|
+
result = auto_html("<div>test</div>") { sanitize :tags => %w(p) }
|
22
|
+
assert_equal "test", result
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_trasform5
|
26
|
+
result = auto_html("<a rel='nofollow'>test</div>") { sanitize :tags => %w(a), :attributes => %w(href)}
|
27
|
+
assert_equal "<a>test", result
|
28
|
+
#
|
29
|
+
# from Rails doc:
|
30
|
+
#
|
31
|
+
# Please note that sanitizing user-provided text does not
|
32
|
+
# guarantee that the resulting markup is valid.
|
33
|
+
#
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require File.expand_path('../../unit_test_helper', __FILE__)
|
2
|
+
|
3
|
+
class SimpleFormatTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_transform
|
6
|
+
result = auto_html('Hey check out my blog => http://rors.org') { simple_format }
|
7
|
+
assert_equal '<p>Hey check out my blog => http://rors.org</p>', result
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_trasform2
|
11
|
+
result = auto_html('Hey check out my code => http://github.com/dejan') { simple_format }
|
12
|
+
assert_equal '<p>Hey check out my code => http://github.com/dejan</p>', result
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../unit_test_helper'
|
2
|
+
|
3
|
+
class VimeoImageTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_build_image_url
|
6
|
+
result = auto_html('http://www.vimeo.com/3300155') { vimeo_image }
|
7
|
+
assert_equal 'http://b.vimeocdn.com/ts/188/591/1885913_200.jpg', result
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_build_image_url_small
|
11
|
+
result = auto_html('http://vimeo.com/3300155') { vimeo_image(:size => :small) }
|
12
|
+
assert_equal 'http://b.vimeocdn.com/ts/188/591/1885913_100.jpg', result
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_build_image_url_medium
|
16
|
+
result = auto_html('http://www.vimeo.com/3300155') { vimeo_image(:size => :medium) }
|
17
|
+
assert_equal 'http://b.vimeocdn.com/ts/188/591/1885913_200.jpg', result
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_build_image_url_large
|
21
|
+
result = auto_html('http://www.vimeo.com/3300155') { vimeo_image(:size => :large) }
|
22
|
+
assert_equal 'http://b.vimeocdn.com/ts/188/591/1885913_640.jpg', result
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_build_image_url_wrong_size
|
26
|
+
result = auto_html('http://www.vimeo.com/3300155') { vimeo_image(:size => :wrong_size) }
|
27
|
+
assert_equal 'http://b.vimeocdn.com/ts/188/591/1885913_200.jpg', result
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require File.expand_path('../../unit_test_helper', __FILE__)
|
2
|
+
|
3
|
+
class VimeoTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_transform_url_with_www
|
6
|
+
result = auto_html('http://www.vimeo.com/3300155') { vimeo }
|
7
|
+
assert_equal '<iframe src="http://player.vimeo.com/video/3300155?title=0&byline=0&portrait=0" width="440" height="248" frameborder="0"></iframe>', result
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_transform_url_without_www
|
11
|
+
result = auto_html('http://vimeo.com/3300155') { vimeo }
|
12
|
+
assert_equal '<iframe src="http://player.vimeo.com/video/3300155?title=0&byline=0&portrait=0" width="440" height="248" frameborder="0"></iframe>', result
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_transform_url_with_params
|
16
|
+
result = auto_html('http://vimeo.com/3300155?pg=embed&sec=') { vimeo }
|
17
|
+
assert_equal '<iframe src="http://player.vimeo.com/video/3300155?title=0&byline=0&portrait=0" width="440" height="248" frameborder="0"></iframe>', result
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_transform_url_with_anchor
|
21
|
+
result = auto_html('http://vimeo.com/3300155#skirt') { vimeo }
|
22
|
+
assert_equal '<iframe src="http://player.vimeo.com/video/3300155?title=0&byline=0&portrait=0" width="440" height="248" frameborder="0"></iframe>', result
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_transform_with_options
|
26
|
+
result = auto_html("http://www.vimeo.com/3300155") { vimeo(:width => 300, :height => 250) }
|
27
|
+
assert_equal '<iframe src="http://player.vimeo.com/video/3300155?title=0&byline=0&portrait=0" width="300" height="250" frameborder="0"></iframe>', result
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_transform_with_title
|
31
|
+
result = auto_html("http://www.vimeo.com/3300155") { vimeo(:width => 300, :height => 250, :show_title => true) }
|
32
|
+
assert_equal '<iframe src="http://player.vimeo.com/video/3300155?byline=0&portrait=0" width="300" height="250" frameborder="0"></iframe>', result
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_transform_with_byline
|
36
|
+
result = auto_html("http://www.vimeo.com/3300155") { vimeo(:width => 300, :height => 250, :show_byline => true) }
|
37
|
+
assert_equal '<iframe src="http://player.vimeo.com/video/3300155?title=0&portrait=0" width="300" height="250" frameborder="0"></iframe>', result
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_transform_with_portrait
|
41
|
+
result = auto_html("http://www.vimeo.com/3300155") { vimeo(:width => 300, :height => 250, :show_portrait => true) }
|
42
|
+
assert_equal '<iframe src="http://player.vimeo.com/video/3300155?title=0&byline=0" width="300" height="250" frameborder="0"></iframe>', result
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../unit_test_helper'
|
2
|
+
|
3
|
+
class YouTubeImageTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_transform
|
6
|
+
result = auto_html('http://www.youtube.com/watch?v=BwNrmYRiX_o') { youtube_image }
|
7
|
+
assert_equal 'http://i1.ytimg.com/vi/BwNrmYRiX_o/default.jpg', result
|
8
|
+
end
|
9
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require File.expand_path('../../unit_test_helper', __FILE__)
|
2
|
+
|
3
|
+
class YouTubeJsApiTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_transform
|
6
|
+
result = auto_html('http://www.youtube.com/watch?v=BwNrmYRiX_o') { youtube_js_api }
|
7
|
+
assert_equal '<object width="390" height="250"><param name="movie" value="http://www.youtube.com/v/BwNrmYRiX_o?enablejsapi=1&playerapiid=ytplayer"></param><param name="wmode" value="transparent"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/BwNrmYRiX_o?enablejsapi=1&playerapiid=ytplayer" type="application/x-shockwave-flash" wmode="transparent" width="390" height="250" allowscriptaccess="always"></embed></object>', result
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_transform2
|
11
|
+
result = auto_html('http://www.youtube.com/watch?v=3-ewi9saKg8&eurl=http%3A%2F%2Fvukajlija.com%2Fvideo%2Fklipovi%3Fstrana%3D6&feature=player_embedded') { youtube_js_api }
|
12
|
+
assert_equal '<object width="390" height="250"><param name="movie" value="http://www.youtube.com/v/3-ewi9saKg8?enablejsapi=1&playerapiid=ytplayer"></param><param name="wmode" value="transparent"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/3-ewi9saKg8?enablejsapi=1&playerapiid=ytplayer" type="application/x-shockwave-flash" wmode="transparent" width="390" height="250" allowscriptaccess="always"></embed></object>', result
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_transform3
|
16
|
+
result = auto_html('http://www.youtube.com/watch?v=Mw8KJ29qph0&feature=related') { youtube_js_api }
|
17
|
+
assert_equal '<object width="390" height="250"><param name="movie" value="http://www.youtube.com/v/Mw8KJ29qph0?enablejsapi=1&playerapiid=ytplayer"></param><param name="wmode" value="transparent"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/Mw8KJ29qph0?enablejsapi=1&playerapiid=ytplayer" type="application/x-shockwave-flash" wmode="transparent" width="390" height="250" allowscriptaccess="always"></embed></object>', result
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_transform_url_without_www
|
21
|
+
result = auto_html('http://youtube.com/watch?v=BwNrmYRiX_o') { youtube_js_api }
|
22
|
+
assert_equal '<object width="390" height="250"><param name="movie" value="http://www.youtube.com/v/BwNrmYRiX_o?enablejsapi=1&playerapiid=ytplayer"></param><param name="wmode" value="transparent"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/BwNrmYRiX_o?enablejsapi=1&playerapiid=ytplayer" type="application/x-shockwave-flash" wmode="transparent" width="390" height="250" allowscriptaccess="always"></embed></object>', result
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_transform_with_options
|
26
|
+
result = auto_html('http://www.youtube.com/watch?v=ZA1NoOOoaNw') { youtube_js_api(:width => 300, :height => 250) }
|
27
|
+
assert_equal '<object width="300" height="250"><param name="movie" value="http://www.youtube.com/v/ZA1NoOOoaNw?enablejsapi=1&playerapiid=ytplayer"></param><param name="wmode" value="transparent"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/ZA1NoOOoaNw?enablejsapi=1&playerapiid=ytplayer" type="application/x-shockwave-flash" wmode="transparent" width="300" height="250" allowscriptaccess="always"></embed></object>', result
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require File.expand_path('../../unit_test_helper', __FILE__)
|
2
|
+
|
3
|
+
class YouTubeTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_transform
|
6
|
+
result = auto_html('http://www.youtube.com/watch?v=BwNrmYRiX_o') { youtube }
|
7
|
+
assert_equal '<iframe class="youtube-player" type="text/html" width="390" height="250" src="http://www.youtube.com/embed/BwNrmYRiX_o" frameborder="0">
|
8
|
+
</iframe>', result
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_transform2
|
12
|
+
result = auto_html('http://www.youtube.com/watch?v=BwNrmYRiX_o&eurl=http%3A%2F%2Fvukajlija.com%2Fvideo%2Fklipovi%3Fstrana%3D6&feature=player_embedded') { youtube }
|
13
|
+
assert_equal '<iframe class="youtube-player" type="text/html" width="390" height="250" src="http://www.youtube.com/embed/BwNrmYRiX_o" frameborder="0">
|
14
|
+
</iframe>', result
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_transform3
|
18
|
+
result = auto_html('http://www.youtube.com/watch?v=BwNrmYRiX_o&feature=related') { youtube }
|
19
|
+
assert_equal '<iframe class="youtube-player" type="text/html" width="390" height="250" src="http://www.youtube.com/embed/BwNrmYRiX_o" frameborder="0">
|
20
|
+
</iframe>', result
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_transform_url_without_www
|
24
|
+
result = auto_html('http://youtube.com/watch?v=BwNrmYRiX_o') { youtube }
|
25
|
+
assert_equal '<iframe class="youtube-player" type="text/html" width="390" height="250" src="http://www.youtube.com/embed/BwNrmYRiX_o" frameborder="0">
|
26
|
+
</iframe>', result
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_transform_with_options
|
30
|
+
result = auto_html('http://www.youtube.com/watch?v=BwNrmYRiX_o') { youtube(:width => 300, :height => 255, :frameborder => 1) }
|
31
|
+
assert_equal '<iframe class="youtube-player" type="text/html" width="300" height="255" src="http://www.youtube.com/embed/BwNrmYRiX_o" frameborder="1">
|
32
|
+
</iframe>', result
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
metadata
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: marcosinger-auto_html
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 19
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 3
|
9
|
+
- 4
|
10
|
+
version: 1.3.4
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- "Marco Ant\xC3\xB4nio Singer"
|
14
|
+
- Dejan Simic
|
15
|
+
autorequire:
|
16
|
+
bindir: bin
|
17
|
+
cert_chain: []
|
18
|
+
|
19
|
+
date: 2011-04-07 00:00:00 -03:00
|
20
|
+
default_executable:
|
21
|
+
dependencies: []
|
22
|
+
|
23
|
+
description: Automatically transforms URIs (via domain) and includes the destination resource (Vimeo, YouTube movie, image, ...) in your document
|
24
|
+
email:
|
25
|
+
- markaum@gmail.com
|
26
|
+
- desimic@gmail.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files: []
|
32
|
+
|
33
|
+
files:
|
34
|
+
- Rakefile
|
35
|
+
- lib/auto_html/auto_html_for.rb
|
36
|
+
- lib/auto_html/base.rb
|
37
|
+
- lib/auto_html/builder.rb
|
38
|
+
- lib/auto_html/filter.rb
|
39
|
+
- lib/auto_html/filters/dailymotion.rb
|
40
|
+
- lib/auto_html/filters/google_video.rb
|
41
|
+
- lib/auto_html/filters/html_escape.rb
|
42
|
+
- lib/auto_html/filters/image.rb
|
43
|
+
- lib/auto_html/filters/link.rb
|
44
|
+
- lib/auto_html/filters/metacafe.rb
|
45
|
+
- lib/auto_html/filters/sanitize.rb
|
46
|
+
- lib/auto_html/filters/simple_format.rb
|
47
|
+
- lib/auto_html/filters/vimeo.rb
|
48
|
+
- lib/auto_html/filters/vimeo_image.rb
|
49
|
+
- lib/auto_html/filters/youtube.rb
|
50
|
+
- lib/auto_html/filters/youtube_image.rb
|
51
|
+
- lib/auto_html/filters/youtube_js_api.rb
|
52
|
+
- lib/auto_html.rb
|
53
|
+
- test/fixture_setup.rb
|
54
|
+
- test/fixtures/database.yml
|
55
|
+
- test/fixtures/schema.rb
|
56
|
+
- test/functional/auto_html_for_options_test.rb
|
57
|
+
- test/functional/auto_html_for_test.rb
|
58
|
+
- test/functional/filter_test.rb
|
59
|
+
- test/test_helper.rb
|
60
|
+
- test/unit/auto_html_test.rb
|
61
|
+
- test/unit/filters/dailymotion_test.rb
|
62
|
+
- test/unit/filters/google_video_test.rb
|
63
|
+
- test/unit/filters/html_escape_test.rb
|
64
|
+
- test/unit/filters/image_test.rb
|
65
|
+
- test/unit/filters/link_test.rb
|
66
|
+
- test/unit/filters/metacafe_test.rb
|
67
|
+
- test/unit/filters/sanitize_test.rb
|
68
|
+
- test/unit/filters/simple_format_test.rb
|
69
|
+
- test/unit/filters/vimeo_image_test.rb
|
70
|
+
- test/unit/filters/vimeo_test.rb
|
71
|
+
- test/unit/filters/youtube_image_test.rb
|
72
|
+
- test/unit/filters/youtube_js_api_test.rb
|
73
|
+
- test/unit/filters/youtube_test.rb
|
74
|
+
- test/unit/unit_test_helper.rb
|
75
|
+
- README.md
|
76
|
+
has_rdoc: true
|
77
|
+
homepage: http://github.com/marcosinger/auto_html
|
78
|
+
licenses: []
|
79
|
+
|
80
|
+
post_install_message:
|
81
|
+
rdoc_options: []
|
82
|
+
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
none: false
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
hash: 3
|
91
|
+
segments:
|
92
|
+
- 0
|
93
|
+
version: "0"
|
94
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
|
+
none: false
|
96
|
+
requirements:
|
97
|
+
- - ">="
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
hash: 3
|
100
|
+
segments:
|
101
|
+
- 0
|
102
|
+
version: "0"
|
103
|
+
requirements: []
|
104
|
+
|
105
|
+
rubyforge_project:
|
106
|
+
rubygems_version: 1.6.2
|
107
|
+
signing_key:
|
108
|
+
specification_version: 3
|
109
|
+
summary: Transform URIs to appropriate markup
|
110
|
+
test_files: []
|
111
|
+
|