├── .rspec ├── spec ├── spec_helper.rb └── html │ └── pipeline │ └── youtube_filter_spec.rb ├── Gemfile ├── html-pipeline-youtube-0.0.1.gem ├── Rakefile ├── bin ├── setup └── console ├── lib └── html │ └── pipeline │ ├── youtube │ ├── version.rb │ └── youtube_filter.rb │ └── youtube.rb ├── .gitignore ├── .travis.yml ├── Guardfile ├── html-pipeline-youtube.gemspec └── README.md /.rspec: -------------------------------------------------------------------------------- 1 | --format documentation 2 | --color 3 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__) 2 | require 'html/pipeline/youtube' 3 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | group :test do 4 | gem "guard-rspec" 5 | gem "guard" 6 | end 7 | gemspec 8 | -------------------------------------------------------------------------------- /html-pipeline-youtube-0.0.1.gem: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/polydice/html-pipeline-youtube/master/html-pipeline-youtube-0.0.1.gem -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require "bundler/gem_tasks" 2 | require 'rspec/core/rake_task' 3 | 4 | RSpec::Core::RakeTask.new(:spec) 5 | 6 | task :default => :spec 7 | -------------------------------------------------------------------------------- /bin/setup: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -euo pipefail 3 | IFS=$'\n\t' 4 | 5 | bundle install 6 | 7 | # Do any other automated setup that you need to do here 8 | -------------------------------------------------------------------------------- /lib/html/pipeline/youtube/version.rb: -------------------------------------------------------------------------------- 1 | module HTML 2 | class Pipeline 3 | module Youtube 4 | VERSION = '0.1.4' 5 | end 6 | end 7 | end 8 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /.bundle/ 2 | /.yardoc 3 | /Gemfile.lock 4 | /_yardoc/ 5 | /coverage/ 6 | /doc/ 7 | /pkg/ 8 | /spec/reports/ 9 | /tmp/ 10 | **.gem 11 | .ruby-version 12 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: ruby 2 | sudo: false 3 | cache: bundler 4 | rvm: 5 | - 2.3.4 6 | - 2.4.1 7 | - ruby-head 8 | 9 | notifications: 10 | email: 11 | recipients: 12 | - stan001212@gmail.com 13 | on_failure: change 14 | on_success: never 15 | -------------------------------------------------------------------------------- /Guardfile: -------------------------------------------------------------------------------- 1 | guard 'rspec', cmd: "bundle exec rspec" do 2 | # watch /lib/ files 3 | watch(%r{^lib/(.+).rb$}) do |m| 4 | "spec/#{m[1]}_spec.rb" 5 | end 6 | 7 | # watch /spec/ files 8 | watch(%r{^spec/(.+).rb$}) do |m| 9 | "spec/#{m[1]}.rb" 10 | end 11 | end 12 | -------------------------------------------------------------------------------- /lib/html/pipeline/youtube.rb: -------------------------------------------------------------------------------- 1 | require "html/pipeline" 2 | require "html/pipeline/youtube/version" 3 | require "html/pipeline/youtube/youtube_filter.rb" 4 | 5 | module HTML 6 | class Pipeline 7 | autoload :YoutubeFilter, 'html/pipeline/youtube/youtube_filter' 8 | end 9 | end 10 | -------------------------------------------------------------------------------- /bin/console: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | require "bundler/setup" 4 | require "html/pipeline/youtube" 5 | 6 | # You can add fixtures and/or initialization code here to make experimenting 7 | # with your gem easier. You can also use a different console, if you like. 8 | 9 | # (If you use this, don't forget to add pry to your Gemfile!) 10 | # require "pry" 11 | # Pry.start 12 | 13 | require "irb" 14 | IRB.start 15 | -------------------------------------------------------------------------------- /html-pipeline-youtube.gemspec: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | lib = File.expand_path('../lib', __FILE__) 3 | $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) 4 | require 'html/pipeline/youtube/version' 5 | 6 | Gem::Specification.new do |spec| 7 | spec.name = "html-pipeline-youtube" 8 | spec.version = HTML::Pipeline::Youtube::VERSION 9 | spec.authors = ["Stan Luo"] 10 | spec.email = ["stan001212@gmail.com"] 11 | 12 | spec.summary = %q{Youtube filter for html-pipeline} 13 | spec.description = %q{A html-pipeline filter that converts youtube url into iframe} 14 | spec.homepage = "https://github.com/st0012/html-pipeline-youtube" 15 | 16 | spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) } 17 | spec.bindir = "exe" 18 | spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } 19 | spec.require_paths = ["lib"] 20 | 21 | spec.add_dependency "html-pipeline", "~> 2.0" 22 | spec.add_development_dependency "rake", "~> 10.0" 23 | end 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Build Status](https://travis-ci.org/st0012/html-pipeline-youtube.svg?branch=master)](https://travis-ci.org/st0012/html-pipeline-youtube) 2 | 3 | # HTML::Pipeline Youtube Gem 4 | A html-pipeline filter that converts youtube url into iframe 5 | 6 | ## Installation 7 | 8 | Add this line to your application's Gemfile: 9 | 10 | ```ruby 11 | gem 'html-pipeline' 12 | gem 'html-pipeline-youtube' 13 | ``` 14 | 15 | And then execute: 16 | 17 | $ bundle 18 | 19 | Or install it yourself as: 20 | 21 | $ gem install html-pipeline-youtube 22 | 23 | ## Usage 24 | 25 | See [html-pipeline](https://github.com/jch/html-pipeline) 26 | 27 | 28 | ## Development 29 | 30 | After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment. 31 | 32 | To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org). 33 | 34 | ## Contributing 35 | 36 | 1. [Fork it]( https://github.com/st0012/html-pipeline-youtube/fork ) 37 | 2. Create your feature branch (`git checkout -b my-new-feature`) 38 | 3. Commit your changes (`git commit -am 'Add some feature'`) 39 | 4. Push to the branch (`git push origin my-new-feature`) 40 | 5. Create a new Pull Request 41 | -------------------------------------------------------------------------------- /lib/html/pipeline/youtube/youtube_filter.rb: -------------------------------------------------------------------------------- 1 | module HTML 2 | class Pipeline 3 | class YoutubeFilter < TextFilter 4 | def call 5 | # This filter converts youtube video's url into youtube iframe. 6 | # 7 | # Context options: 8 | # :video_width - integer, sets iframe's width 9 | # :video_height - integer, sets iframe's height 10 | # :video_frame_border - integer, sets iframe border's width 11 | # :video_wmode - string, sets iframe's wmode option 12 | # :video_autoplay - boolean, whether video should autoplay 13 | # :video_hide_related - boolean, whether shows related videos 14 | regex = /(?<=^|\s|
|
)https?:\/\/(?:www.)?(?:youtube\.com\/(?:embed\/|watch\?(?:feature=player_embedded&)?v=)|youtu\.be\/)([A-Za-z0-9_-]*)[&?\w-]*/ 15 | @text.gsub(regex) do 16 | youtube_id = $1 17 | width = context[:video_width] || 420 18 | height = context[:video_height] || 315 19 | frameborder = context[:video_frameborder] || 0 20 | wmode = context[:video_wmode] 21 | autoplay = context[:video_autoplay] || false 22 | hide_related = context[:video_hide_related] || false 23 | src = "//www.youtube.com/embed/#{youtube_id}" 24 | params = [] 25 | params << "wmode=#{wmode}" if wmode 26 | params << "autoplay=1" if autoplay 27 | params << "rel=0" if hide_related 28 | src += "?#{params.join '&'}" unless params.empty? 29 | 30 | # Prefix with two "\n" for compatibility with markup such as Markdown: 31 | %{\n\n
} 32 | end 33 | end 34 | end 35 | end 36 | end 37 | -------------------------------------------------------------------------------- /spec/html/pipeline/youtube_filter_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe HTML::Pipeline::YoutubeFilter do 4 | subject { HTML::Pipeline::YoutubeFilter } 5 | let(:youtube_url) { "https://www.youtube.com/watch?v=Kg4aWWIsszw" } 6 | 7 | it 'has a version number' do 8 | expect(HTML::Pipeline::Youtube::VERSION).not_to be nil 9 | end 10 | 11 | context "With other filter's result" do 12 | it "doesn't affect links in markdown" do 13 | markdown_link = "[Video](https://www.youtube.com/watch?v=Kg4aWWIsszw)" 14 | 15 | expect(subject.to_html(markdown_link)).to eq(markdown_link) 16 | end 17 | it "doesn't affect links in html tag" do 18 | hyper_link = %(Video) 19 | 20 | expect(subject.to_html(hyper_link)).to eq(hyper_link) 21 | end 22 | it "does affect links in a div" do 23 | hyper_link = %(
https://www.youtube.com/watch?v=Kg4aWWIsszw
) 24 | 25 | expect(subject.to_html(hyper_link)).to eq( 26 | %(
\n\n
) 27 | ) 28 | end 29 | it "does affect links after a br" do 30 | hyper_link = %(
https://www.youtube.com/watch?v=Kg4aWWIsszw) 31 | 32 | expect(subject.to_html(hyper_link)).to eq( 33 | %(
\n\n
) 34 | ) 35 | end 36 | it "does not consume whitespace" do 37 | source = 'Check out https://www.youtube.com/watch?v=Kg4aWWIsszw now' 38 | expect(subject.to_html(source)).to eq( 39 | %(Check out \n\n
now) 40 | ) 41 | end 42 | it "supports /embed URLs" do 43 | source = 'https://www.youtube.com/embed/Kg4aWWIsszw' 44 | expect(subject.to_html(source)).to eq( 45 | %(\n\n
) 46 | ) 47 | end 48 | end 49 | 50 | context "With no options" do 51 | it "generates iframe with default setting" do 52 | expect(subject.to_html(youtube_url)).to eq( 53 | %(\n\n
) 54 | ) 55 | end 56 | end 57 | 58 | context "With options" do 59 | it "generated iframe with custom option" do 60 | result = subject.to_html( 61 | youtube_url, 62 | video_width: 500, 63 | video_height: 100, 64 | video_frameborder: 5, 65 | video_autoplay: true, 66 | video_hide_related: true 67 | ) 68 | 69 | expect(result).to eq( 70 | %(\n\n
) 71 | ) 72 | end 73 | end 74 | end 75 | --------------------------------------------------------------------------------