make video urls have correct href

This commit is contained in:
Raphael Sofaer 2011-02-22 18:51:40 -08:00
parent 17f41f1291
commit 74b8921c09
2 changed files with 32 additions and 16 deletions

View file

@ -10,10 +10,6 @@ module ApplicationHelper
content_tag(:abbr, time.to_s, options.merge(:title => time.iso8601)) if time
end
def modern_browser?
false
end
def page_title text=nil
title = ""
if text.blank?
@ -68,7 +64,7 @@ module ApplicationHelper
param_string << "#{k}=#{v}"
end
end
"<li>
"<li>
<a href='/aspects/#{aspect.id}#{param_string}'>
#{aspect.name}
</a>
@ -210,25 +206,29 @@ module ApplicationHelper
def process_youtube(message, youtube_maps)
regex = /( |^)(http:\/\/)?www\.youtube\.com\/watch[^ ]*v=([A-Za-z0-9_\-]+)(&[^ ]*|)/
while youtube = message.match(regex)
video_id = youtube[3]
processed_message = message.gsub(regex) do |matched_string|
match_data = matched_string.match(regex)
video_id = match_data[3]
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
message.gsub!(youtube[0], '<a class="video-link" data-host="youtube.com" data-video-id="' + video_id + '" href="#video">Youtube: ' + title + '</a>')
'<a class="video-link" data-host="youtube.com" data-video-id="' + video_id + '" href="'+ match_data[0].strip + '">Youtube: ' + title + '</a>'
end
return message
return processed_message
end
def process_autolinks(message)
message.gsub!(/( |^)(www\.[^\s]+\.[^\s])/, '\1http://\2')
message.gsub!(/(<a target="\\?_blank" href=")?(https|http|ftp):\/\/([^\s]+)/) do |m|
if !$1.nil?
captures = [$1,$2,$3]
if !captures[0].nil?
m
elsif m.match(/(youtube|vimeo)/)
m.gsub(/(\*|_)/) { |m| "\\#{$1}" } #remove markers on markdown chars to not markdown inside links
else
res = %{<a target="_blank" href="#{$2}://#{$3}">#{$3}</a>}
res = %{<a target="_blank" href="#{captures[1]}://#{captures[2]}">#{captures[2]}</a>}
res.gsub!(/(\*|_)/) { |m| "\\#{$1}" }
res
end
@ -253,23 +253,23 @@ module ApplicationHelper
def process_vimeo(message, vimeo_maps)
regex = /https?:\/\/(?:w{3}\.)?vimeo.com\/(\d{6,})/
while vimeo = message.match(regex)
video_id = vimeo[1]
processed_message = message.gsub(regex) do |matched_string|
match_data = message.match(regex)
video_id = match_data[1]
if vimeo_maps && vimeo_maps[video_id]
title = h(CGI::unescape(vimeo_maps[video_id]))
else
title = I18n.t 'application.helper.video_title.unknown'
end
message.gsub!(vimeo[0], '<a class="video-link" data-host="vimeo.com" data-video-id="' + video_id + '" href="#video">Vimeo: ' + title + '</a>')
'<a class="video-link" data-host="vimeo.com" data-video-id="' + video_id + '" href="' + match_data[0] + '">Vimeo: ' + title + '</a>'
end
return message
return processed_message
end
def info_text(text)
image_tag 'icons/monotone_question.png', :class => 'what_is_this', :title => text
end
def get_javascript_strings_for(language)
defaults = I18n.t('javascripts', :locale => DEFAULT_LANGUAGE)

View file

@ -106,6 +106,22 @@ describe ApplicationHelper do
res.should =~ /data-host="youtube.com"/
res.should =~ /data-video-id="#{video_id}"/
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
url = start_url + "&a=GxdCwVVULXdvEBKmx_f5ywvZ0zZHHHDU&list=ML&playnext=1"
res = markdownify(url)
res.should =~ /href=[\S]+v=#{video_id}/
end
it 'does not autolink inside the link' do
video_id = "ABYnqp-bxvg"
start_url ="http://www.youtube.com/watch?v=" + video_id
url = start_url + "&a=GxdCwVVULXdvEBKmx_f5ywvZ0zZHHHDU&list=ML&playnext=1"
res = markdownify(url)
pp res
res.match(/href="<a/).should be_nil
end
end
it "recognizes multiple links of different types" do