DEV: Refactor excluding audio/video URLs from search result blurbs

Followup to 580a4a82
This commit is contained in:
Penar Musaraj 2019-10-31 09:13:24 -04:00
parent c5e67726fd
commit f8b72d9835
3 changed files with 11 additions and 5 deletions

View File

@ -6,6 +6,8 @@ Dir["#{Rails.root}/lib/onebox/engine/*_onebox.rb"].sort.each { |f| require f }
module Oneboxer
ONEBOX_CSS_CLASS = "onebox"
AUDIO_REGEX = /^\.(mp3|og[ga]|opus|wav|m4[abpr]|aac|flac)$/i
VIDEO_REGEX = /^\.(mov|mp4|m4v|webm|ogv|3gp)$/i
# keep reloaders happy
unless defined? Oneboxer::Result
@ -171,9 +173,9 @@ module Oneboxer
def self.local_upload_html(url)
case File.extname(URI(url).path || "")
when /^\.(mov|mp4|m4v|webm|ogv|3gp)$/i
when VIDEO_REGEX
"<video width='100%' height='100%' controls><source src='#{url}'><a href='#{url}'>#{url}</a></video>"
when /^\.(mp3|og[ga]|opus|wav|m4[abpr]|aac|flac)$/i
when AUDIO_REGEX
"<audio controls><source src='#{url}'><a href='#{url}'>#{url}</a></audio>"
end
end

View File

@ -80,8 +80,12 @@ class Search
cooked.scan(URI.regexp(%w{http https})) { urls << $& }
urls.each do |url|
cooked.gsub!(url, I18n.t("search.video")) if url.match(/.(mov|mp4|webm|ogv)/)
cooked.gsub!(url, I18n.t("search.audio")) if url.match(/.(mp3|ogg|wav|m4a)/)
case File.extname(URI(url).path || "")
when Oneboxer::VIDEO_REGEX
cooked.gsub!(url, I18n.t("search.video"))
when Oneboxer::AUDIO_REGEX
cooked.gsub!(url, I18n.t("search.audio"))
end
end
if term

View File

@ -22,7 +22,7 @@ describe Search do
link to an audio file: https://somesite.com/content/file123.m4a
link to a video file: https://somesite.com/content/somethingelse.mov
link to a video file: https://somesite.com/content/somethingelse.MOV
RAW
result = Search::GroupedSearchResults.blurb_for(cooked)
expect(result).to eq("link to an external page: https://google.com/?u=bar link to an audio file: #{I18n.t("search.audio")} link to a video file: #{I18n.t("search.video")}")