2013-06-10 15:33:37 -04:00
|
|
|
#
|
|
|
|
# HTML emails don't support CSS, so we can use nokogiri to inline attributes based on
|
|
|
|
# matchers.
|
|
|
|
#
|
|
|
|
module Email
|
|
|
|
class Styles
|
|
|
|
|
|
|
|
def initialize(html)
|
|
|
|
@html = html
|
2013-06-13 12:15:05 -04:00
|
|
|
@fragment = Nokogiri::HTML.fragment(@html)
|
2013-06-10 15:33:37 -04:00
|
|
|
end
|
|
|
|
|
2013-06-13 12:15:05 -04:00
|
|
|
def format_basic
|
|
|
|
@fragment.css('img').each do |img|
|
2013-06-10 15:33:37 -04:00
|
|
|
|
2013-06-13 12:15:05 -04:00
|
|
|
if img['src'] =~ /\/assets\/emoji\//
|
|
|
|
img['style'] = "width: 20px; height: 20px;"
|
|
|
|
else
|
|
|
|
img['style'] = "max-width: 694px;"
|
|
|
|
end
|
|
|
|
|
|
|
|
if img['src'][0] == "/"
|
|
|
|
img['src'] = "#{Discourse.base_url}#{img['src']}"
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def format_html
|
|
|
|
@fragment.css('h3').each do |h3|
|
2013-06-10 15:33:37 -04:00
|
|
|
h3['style'] = 'margin: 15px 0 20px 0; border-bottom: 1px solid #ddd;'
|
|
|
|
end
|
|
|
|
|
2013-06-13 12:15:05 -04:00
|
|
|
@fragment.css('hr').each do |hr|
|
2013-06-10 15:33:37 -04:00
|
|
|
hr['style'] = 'background-color: #ddd; height: 1px; border: 1px;'
|
|
|
|
end
|
|
|
|
|
2013-06-11 12:28:50 -04:00
|
|
|
|
2013-06-13 12:15:05 -04:00
|
|
|
@fragment.css('a').each do |a|
|
2013-06-10 15:33:37 -04:00
|
|
|
a['style'] = 'text-decoration: none; font-weight: bold; font-size: 15px; color: #006699;'
|
|
|
|
end
|
|
|
|
|
2013-06-13 12:15:05 -04:00
|
|
|
@fragment.css('ul').each do |ul|
|
2013-06-10 15:33:37 -04:00
|
|
|
ul['style'] = 'margin: 0 0 0 10px; padding: 0 0 0 20px;'
|
|
|
|
end
|
|
|
|
|
2013-06-13 12:15:05 -04:00
|
|
|
@fragment.css('li').each do |li|
|
2013-06-10 15:33:37 -04:00
|
|
|
li['style'] = 'padding-bottom: 10px'
|
|
|
|
end
|
|
|
|
|
2013-06-13 12:15:05 -04:00
|
|
|
@fragment.css('pre').each do |pre|
|
2013-06-10 15:33:37 -04:00
|
|
|
pre.replace(pre.text)
|
|
|
|
end
|
|
|
|
|
2013-06-13 12:15:05 -04:00
|
|
|
@fragment.css('div.digest-post').each do |div|
|
2013-06-11 12:27:11 -04:00
|
|
|
div['style'] = 'margin-left: 15px; margin-top: 20px; max-width: 694px;'
|
|
|
|
end
|
2013-06-13 12:15:05 -04:00
|
|
|
end
|
2013-06-11 12:27:11 -04:00
|
|
|
|
2013-06-13 12:15:05 -04:00
|
|
|
def to_html
|
|
|
|
@fragment.to_html
|
2013-06-10 15:33:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|