This commit is contained in:
MrZYX 2011-04-29 23:35:21 +02:00
parent 62696c3e42
commit 6623dbb1f1
3 changed files with 27 additions and 4 deletions

View file

@ -216,16 +216,18 @@ module ApplicationHelper
end
def process_youtube(message, youtube_maps)
regex = /( |^)(https?:\/\/)?www\.youtube\.com\/watch[^ ]*v=([A-Za-z0-9_\-]+)(&[^ ]*|)/
regex = /( |^)(https?:\/\/)?www\.youtube\.com\/watch[^ ]*v=([A-Za-z0-9_\-]+)(&[^ ]*)?(#[^ ]+)?/
processed_message = message.gsub(regex) do |matched_string|
match_data = matched_string.match(regex)
video_id = match_data[3]
anchor = match_data[5]
anchor ||= ''
if youtube_maps && youtube_maps[video_id]
title = h(CGI::unescape(youtube_maps[video_id]))
else
title = I18n.t 'application.helper.video_title.unknown'
end
' <a class="video-link" data-host="youtube.com" data-video-id="' + video_id + '" href="'+ match_data[0].strip + '" target="_blank">Youtube: ' + title + '</a>'
' <a class="video-link" data-host="youtube.com" data-video-id="' + video_id + '" data-anchor="' + anchor + '" href="'+ match_data[0].strip + '" target="_blank">Youtube: ' + title + '</a>'
end
return processed_message
end

View file

@ -65,8 +65,8 @@
var watchVideoOn = Diaspora.widgets.i18n.t("videos.watch");
this.register("youtube.com",
'<a href="//www.youtube.com/watch?v={{video-id}}" target="_blank">' + $.mustache(watchVideoOn, { provider: "YouTube" }) + '</a><br />' +
'<iframe class="youtube-player" type="text/html" src="http://www.youtube.com/embed/{{video-id}}?wmode=opaque"></iframe>');
'<a href="//www.youtube.com/watch?v={{video-id}}{{anchor}}" target="_blank">' + $.mustache(watchVideoOn, { provider: "YouTube" }) + '</a><br />' +
'<iframe class="youtube-player" type="text/html" src="http://www.youtube.com/embed/{{video-id}}?wmode=opaque{{anchor}}"></iframe>');
this.register("vimeo.com",
'<a href="http://vimeo.com/{{video-id}}">' + $.mustache(watchVideoOn, { provider: "Vimeo" }) + '</a><br />' +

View file

@ -101,6 +101,27 @@ describe ApplicationHelper do
res.should =~ /data-video-id="#{video_id}"/
end
it "keeps anchors" do
anchor = "#t=11m34"
video_id = "DHRoHuv3I8E"
url = "http://www.youtube.com/watch?v=" + video_id + anchor
res = markdownify(url)
res.should =~ /Youtube:/
res.should =~ /data-host="youtube.com"/
res.should =~ /data-video-id="#{video_id}"/
res.should =~ /data-anchor="#{anchor}"/
end
it "has an empty data-anchor attribute if there is no anchor" do
video_id = "DHRoHuv3I8E"
url = "http://www.youtube.com/watch?v=" + video_id
res = markdownify(url)
res.should =~ /Youtube:/
res.should =~ /data-host="youtube.com"/
res.should =~ /data-video-id="#{video_id}"/
res.should =~ /data-anchor=""/
end
it "leaves the links in the href of the #a tag" do
video_id = "ABYnqp-bxvg"
start_url ="http://www.youtube.com/watch?v=" + video_id