Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Youtube: autoplay and hide related #58

Merged
merged 2 commits into from
Jan 3, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ You'll probably have user input stored in model, so it's a good place to automat
auto_html_for :body do
html_escape
image
youtube(:width => 400, :height => 250)
youtube(:width => 400, :height => 250, :autoplay => true)
link :target => "_blank", :rel => "nofollow"
simple_format
end
Expand Down Expand Up @@ -120,4 +120,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 changes: 9 additions & 3 deletions lib/auto_html/filters/youtube.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
AutoHtml.add_filter(:youtube).with(:width => 420, :height => 315, :frameborder => 0, :wmode => nil) do |text, options|
AutoHtml.add_filter(:youtube).with(:width => 420, :height => 315, :frameborder => 0, :wmode => nil, :autoplay => false, :hide_related => false) do |text, options|
regex = /https?:\/\/(www.)?(youtube\.com\/watch\?v=|youtu\.be\/|youtube\.com\/watch\?feature=player_embedded&v=)([A-Za-z0-9_-]*)(\&\S+)?(\S)*/
text.gsub(regex) do
youtube_id = $3
width = options[:width]
height = options[:height]
frameborder = options[:frameborder]
wmode = options[:wmode]
src = "//www.youtube.com/embed/#{youtube_id}"
src += "?wmode=#{wmode}" if wmode
autoplay = options[:autoplay]
hide_related = options[:hide_related]
src = "//www.youtube.com/embed/#{youtube_id}?"
params = []
params << "wmode=#{wmode}" if wmode
params << "autoplay=1" if autoplay
params << "rel=0" if hide_related
src += params.join '&' unless params.empty?
%{<iframe width="#{width}" height="#{height}" src="#{src}" frameborder="#{frameborder}" allowfullscreen></iframe>}
end
end
Expand Down