Skip to content

Commit 2467d51

Browse files
committed
Add more filters
1 parent 73c2b27 commit 2467d51

22 files changed

+454
-54
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/*.gem
12
/.bundle/
23
/.yardoc
34
/Gemfile.lock

.rubocop.yml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Metrics/MethodLength:
2+
Enabled: false
3+
4+
Metrics/LineLength:
5+
Enabled: false

Rakefile

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
require "bundler/gem_tasks"
2-
require "rspec/core/rake_task"
1+
require 'bundler/gem_tasks'
2+
require 'rspec/core/rake_task'
3+
require 'rubocop/rake_task'
34

45
RSpec::Core::RakeTask.new(:spec)
6+
RuboCop::RakeTask.new
57

6-
task :default => :spec
8+
task default: [:rubocop, :spec]

auto_html-contrib.gemspec

+17-24
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,26 @@
11
# coding: utf-8
2-
lib = File.expand_path('../lib', __FILE__)
3-
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4-
require 'auto_html/contrib/version'
52

63
Gem::Specification.new do |spec|
7-
spec.name = "auto_html-contrib"
8-
spec.version = AutoHtml::Contrib::VERSION
9-
spec.authors = ["Dejan Simic"]
10-
spec.email = ["[email protected]"]
4+
spec.name = 'auto_html-contrib'
5+
spec.version = '0.0.1'
6+
spec.authors = ['Dejan Simic']
7+
spec.email = ['[email protected]']
118

12-
spec.summary = %q{Filters for `auto_html` gem}
13-
spec.description = %q{Filters for `auto_html` gem}
14-
spec.homepage = "https://github.com/dejan/auto_html-contrib"
15-
spec.license = "MIT"
16-
17-
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18-
# delete this section to allow pushing this gem to any host.
19-
if spec.respond_to?(:metadata)
20-
spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
21-
else
22-
raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23-
end
9+
spec.summary = 'Filters for `auto_html` gem'
10+
spec.description = 'Filters for `auto_html` gem'
11+
spec.homepage = 'https://github.com/dejan/auto_html-contrib'
12+
spec.license = 'MIT'
2413

2514
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26-
spec.bindir = "exe"
15+
spec.bindir = 'exe'
2716
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28-
spec.require_paths = ["lib"]
17+
spec.require_paths = ['lib']
18+
19+
spec.add_dependency 'tag_helper', '~> 0.5'
2920

30-
spec.add_development_dependency "bundler", "~> 1.10"
31-
spec.add_development_dependency "rake", "~> 10.0"
32-
spec.add_development_dependency "rspec"
21+
spec.add_development_dependency 'bundler', '~> 1.10'
22+
spec.add_development_dependency 'rake', '~> 10.0'
23+
spec.add_development_dependency 'rspec', '~> 3.3'
24+
spec.add_development_dependency 'rubocop', '~> 0.33'
25+
spec.add_development_dependency 'fakeweb', '~> 1.3'
3326
end

bin/console

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env ruby
22

3-
require "bundler/setup"
4-
require "auto_html/contrib"
3+
require 'bundler/setup'
4+
require 'auto_html/contrib'
55

66
# You can add fixtures and/or initialization code here to make experimenting
77
# with your gem easier. You can also use a different console, if you like.
@@ -10,5 +10,5 @@ require "auto_html/contrib"
1010
# require "pry"
1111
# Pry.start
1212

13-
require "irb"
13+
require 'irb'
1414
IRB.start

lib/auto_html.rb

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Filters for AutoHtml
2+
module AutoHtml
3+
autoload :Gist, 'auto_html/gist'
4+
autoload :GoogleMaps, 'auto_html/google_maps'
5+
autoload :Instagram, 'auto_html/instagram'
6+
autoload :Vimeo, 'auto_html/vimeo'
7+
autoload :YouTube, 'auto_html/youtube'
8+
autoload :Twitter, 'auto_html/twitter'
9+
end

lib/auto_html/contrib.rb

-7
This file was deleted.

lib/auto_html/contrib/version.rb

-5
This file was deleted.

lib/auto_html/gist.rb

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
require 'tag_helper'
2+
3+
module AutoHtml
4+
# Gist filter
5+
class Gist
6+
include TagHelper
7+
8+
def call(text)
9+
regex = %r{https?://gist\.github\.com/(\w+/)?(\d+)}
10+
text.gsub(regex) do
11+
gist_id = Regexp.last_match(2)
12+
tag(:script, type: 'text/javascript', src: gist_url(gist_id)) { '' }
13+
end
14+
end
15+
16+
private
17+
18+
def gist_url(id)
19+
"https://gist.github.com/#{id}.js"
20+
end
21+
end
22+
end

lib/auto_html/google_maps.rb

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
require 'tag_helper'
2+
3+
module AutoHtml
4+
# Google Maps filter
5+
class GoogleMaps
6+
include TagHelper
7+
8+
def initialize(width: 420, height: 315, type: :normal, zoom: 18, show_info: false)
9+
@width = width
10+
@height = height
11+
@type = type
12+
@zoom = zoom
13+
@show_info = show_info
14+
end
15+
16+
def call(text)
17+
regex = %r{(https?)://maps\.google\.([a-z\.]+)/maps\?(.*)}
18+
text.gsub(regex) do
19+
country = Regexp.last_match(2)
20+
path = "//maps.google.#{country}/maps"
21+
query = Regexp.last_match(3)
22+
link = link_tag(path, query)
23+
iframe_tag(path, query) << tag(:br) << tag(:small) { link }
24+
end
25+
end
26+
27+
private
28+
29+
def iframe_tag(path, map_query)
30+
params = ['f=q', 'source=s_q', map_query, 'output=embed'] + map_options
31+
tag(
32+
:iframe,
33+
width: @width,
34+
height: @height,
35+
frameborder: 0,
36+
scrolling: 'no',
37+
marginheight: 0,
38+
marginwidth: 0,
39+
src: path + '?' + params.join('&amp;')) { '' }
40+
end
41+
42+
def link_tag(path, map_query)
43+
params = ['f=q', 'source=embed', map_query]
44+
tag(
45+
:a,
46+
href: path + '?' + params.join('&amp;'),
47+
style: 'color:#000;text-align:left') do
48+
'View Larger Map'
49+
end
50+
end
51+
52+
def map_options
53+
map_options = []
54+
map_options << 'iwloc=near' if @show_info
55+
map_options << map_types[@type]
56+
map_options << "z=#{@zoom}"
57+
end
58+
59+
def map_types
60+
@map_types ||= {
61+
normal: 't=m',
62+
satellite: 't=k',
63+
terrain: 't=p',
64+
hibrid: 't=h'
65+
}
66+
end
67+
end
68+
end

lib/auto_html/instagram.rb

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
require 'tag_helper'
2+
3+
module AutoHtml
4+
# Instagram filter
5+
class Instagram
6+
include TagHelper
7+
8+
def initialize(width: 616, height: 714)
9+
@width = width
10+
@height = height
11+
end
12+
13+
def call(text)
14+
text << '/' unless text.end_with?('/')
15+
text.gsub(instagram_pattern) do
16+
tag(
17+
:iframe,
18+
src: "#{text}embed",
19+
height: @height,
20+
width: @width,
21+
frameborder: 0,
22+
scrolling: 'no') { '' }
23+
end
24+
end
25+
26+
private
27+
28+
def instagram_pattern
29+
@instagram_pattern ||= %r{https?:\/\/(www.)?instagr(am\.com|\.am)/p/.+}
30+
end
31+
end
32+
end

lib/auto_html/twitter.rb

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
require 'uri'
2+
require 'net/http'
3+
require 'json'
4+
5+
module AutoHtml
6+
# Twitter filter
7+
class Twitter
8+
def call(text)
9+
text.gsub(twitter_pattern) do |match|
10+
uri = URI('https://api.twitter.com/1/statuses/oembed.json')
11+
uri.query = URI.encode_www_form(url: match)
12+
http = Net::HTTP.new(uri.host, uri.port)
13+
http.use_ssl = true
14+
response = JSON.parse(http.get(uri.request_uri).body)
15+
response['html']
16+
end
17+
end
18+
19+
private
20+
21+
def twitter_pattern
22+
@twitter_pattern ||= %r{(?<!href=")https://twitter\.com(/#!)?/[A-Za-z0-9_]{1,15}/status(es)?/\d+}
23+
end
24+
end
25+
end

lib/auto_html/vimeo.rb

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
require 'tag_helper'
2+
3+
module AutoHtml
4+
# Vimeo filter
5+
class Vimeo
6+
include TagHelper
7+
8+
def initialize(width: 420, height: 315, allow_fullscreen: true)
9+
@width = width
10+
@height = height
11+
@allow_fullscreen = allow_fullscreen
12+
end
13+
14+
def call(text)
15+
text.gsub(vimeo_pattern) do
16+
vimeo_id = Regexp.last_match(2)
17+
src = src_url(vimeo_id)
18+
tag(:iframe, { src: src }.merge(iframe_attributes)) { '' }
19+
end
20+
end
21+
22+
private
23+
24+
def vimeo_pattern
25+
@vimeo_pattern ||= %r{https?://(www.)?vimeo\.com/([A-Za-z0-9._%-]*)((\?|#)\S+)?}
26+
end
27+
28+
def src_url(vimeo_id)
29+
"//player.vimeo.com/video/#{vimeo_id}"
30+
end
31+
32+
def iframe_attributes
33+
{}.tap do |attrs|
34+
attrs[:width] = @width
35+
attrs[:height] = @height
36+
attrs[:frameborder] = 0
37+
attrs.merge!(fullscreen_attributes) if @allow_fullscreen
38+
end
39+
end
40+
41+
def fullscreen_attributes
42+
{
43+
webkitallowfullscreen: 'yes',
44+
mozallowfullscreen: 'yes',
45+
allowfullscreen: 'yes'
46+
}
47+
end
48+
end
49+
end

lib/auto_html/youtube.rb

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
require 'tag_helper'
2+
3+
module AutoHtml
4+
# YouTube filter
5+
class YouTube
6+
include TagHelper
7+
8+
def initialize(width: 420, height: 315)
9+
@width = width
10+
@height = height
11+
end
12+
13+
def call(text)
14+
text.gsub(youtube_pattern) do
15+
youtube_id = Regexp.last_match(4)
16+
tag(:div, class: 'video youtube') do
17+
tag(:iframe, iframe_attributes(youtube_id)) { '' }
18+
end
19+
end
20+
end
21+
22+
private
23+
24+
def youtube_pattern
25+
@youtube_pattern ||=
26+
%r{
27+
(https?://)?
28+
(www.)?
29+
(
30+
youtube\.com/watch\?v=|
31+
youtu\.be/|
32+
youtube\.com/watch\?feature=player_embedded&v=
33+
)
34+
([A-Za-z0-9_-]*)(\&\S+)?(\?\S+)?
35+
}x
36+
end
37+
38+
def iframe_attributes(youtube_id)
39+
src = "//www.youtube.com/embed/#{youtube_id}"
40+
{
41+
width: @width,
42+
height: @height,
43+
src: src,
44+
frameborder: 0,
45+
allowfullscreen: 'yes'
46+
}
47+
end
48+
end
49+
end

spec/auto_html/contrib_spec.rb

-11
This file was deleted.

spec/auto_html/gist_spec.rb

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
require 'spec_helper'
2+
3+
RSpec.describe AutoHtml::Gist do
4+
it 'converst gist URL to gist script tag' do
5+
expect(subject.call('https://gist.github.com/171027')).to eq(
6+
'<script type="text/javascript" src="https://gist.github.com/171027.js">'\
7+
'</script>')
8+
end
9+
10+
it 'converst gist URL with username to gist script tag' do
11+
expect(subject.call('https://gist.github.com/toctan/654784')).to eq(
12+
'<script type="text/javascript" src="https://gist.github.com/654784.js">'\
13+
'</script>')
14+
end
15+
end

0 commit comments

Comments
 (0)