Merge pull request #2678 from techAPJ/patch-2

FIX: lazyYT youtube links not getting included in email
This commit is contained in:
Robin Ward 2014-08-22 14:54:41 -04:00
commit b1eb44bd0b
2 changed files with 30 additions and 0 deletions

View File

@ -4,12 +4,17 @@
# #
module Email module Email
class Styles class Styles
@@plugin_callbacks = []
def initialize(html) def initialize(html)
@html = html @html = html
@fragment = Nokogiri::HTML.fragment(@html) @fragment = Nokogiri::HTML.fragment(@html)
end end
def self.register_plugin_style(&block)
@@plugin_callbacks.push(block)
end
def add_styles(node, new_styles) def add_styles(node, new_styles)
existing = node['style'] existing = node['style']
if existing.present? if existing.present?
@ -60,6 +65,7 @@ module Email
correct_footer_style correct_footer_style
reset_tables reset_tables
onebox_styles onebox_styles
plugin_styles
end end
def onebox_styles def onebox_styles
@ -113,6 +119,12 @@ module Email
style('.featured-topic a', 'text-decoration: none; font-weight: bold; color: #006699; margin-right: 5px') style('.featured-topic a', 'text-decoration: none; font-weight: bold; color: #006699; margin-right: 5px')
onebox_styles onebox_styles
plugin_styles
end
# this method is reserved for styles specific to plugin
def plugin_styles
@@plugin_callbacks.each { |block| block.call(@fragment) }
end end
def to_html def to_html

View File

@ -43,3 +43,21 @@ class Onebox::Engine::YoutubeOnebox
result result
end end
end end
after_initialize do
Email::Styles.register_plugin_style do |fragment|
# YouTube onebox can't go in emails, so replace them with clickable links
fragment.css('.lazyYT').each do |i|
begin
src = "https://www.youtube.com/embed/#{i['data-youtube-id']}?autoplay=1&#{i['data-parameters']}"
src_uri = URI(src)
display_src = "https://#{src_uri.host}#{src_uri.path}"
i.replace "<p><a href='#{src_uri.to_s}'>#{display_src}</a><p>"
rescue URI::InvalidURIError
# If the URL is weird, remove it
i.remove
end
end
end
end